Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Get – Set Location and size of rectangle using Drawing.RectangleF class vb.net – C#.net.

Author: Shehzad Hemani
Download Source Code : 1036_source.zip

In this simple article you will learn that how we can get and set location and size of rectangle using Drawing.RectangleF class in VB and C# .net.

 

Location:
To get or set the location of the rectangle on the form, rectanglef class provides us a property named location. This property takes and returns the object of pointf class which stores the location.
SET:
To demonstrate make a window application. Drag two text box and one button on form.
Now write the following code on Button click event:
C#
private void btn_setlocation_Click(object sender, EventArgs e)
        {
            if (txt_xloc.Text != string.Empty && txt_yloc.Text != string.Empty)
            {
                float x = 0f;
                float y = 0f;
                string str=(txt_xloc.Text);
                x = (float)(Convert.ToSingle(str));
                str = (txt_yloc.Text);
                y = (float)(Convert.ToSingle(str));
                PointF p = new PointF(x, y);
                r.Location = p;
                MessageBox.Show("Location set");
            }
            else
                MessageBox.Show("ENTER VALUES PLEASE");           
        }
VB
Private Sub btn_setlocation_Click(ByVal sender As ObjectByVal As EventArgs)
        
If ((txt_xloc.Text <> string.Empty)  _
                    
AndAlso (txt_yloc.Text <> string.Empty)) Then
            Dim 
As Single = 0!
            
Dim As Single = 0!
            
Dim str As String
            
= CType(Convert.ToSingle(str),Single)
            y 
= CType(Convert.ToSingle(str),Single)
            
Dim As PointF = New PointF(x, y)
            r.Location 
p
            MessageBox.Show(
"Location set")
        
Else
            
MessageBox.Show("ENTER VALUES PLEASE")
        
End If
    End Sub
 
This is simple code to set the location of the rectangle.
GET:
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_getLoc_Click(object sender, EventArgs e)
        {
            PointF p = new PointF();
            p = r.Location;
            MessageBox.Show("current location \n" + "X:" + p.X.ToString() + "Y:"+ p.Y.ToString());
        }
VB 
    
Private Sub btn_getLoc_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As PointF = New PointF
        p 
r.Location
        MessageBox.Show((
"current location "& vbLf + ("X:"  _
                        + (p.X.ToString + (
"Y:" + p.Y.ToString)))))
    
End Sub
This is simple code to get the location of rectangle.
Size:
To get or set the size of the rectangle on the form rectanglef class provides us a property named size. This property takes and returns the object of sizef class which stores the location.
SET:
To demonstrate make a window application. Drag two text box and one button on form.
Now write the following code on Button click event:
C#
private void btn_setsize_Click(object sender, EventArgs e)
        {
            if (txt_height.Text!= string.Empty && txt_width.Text != string.Empty)
            {
                float h = 0f;
                float w = 0f;
                string str = (txt_height.Text);
                h = (float)(Convert.ToSingle(str));
                str = (txt_width.Text);
                w = (float)(Convert.ToSingle(str));
                SizeF sf = new SizeF(h, w);
                r.Size = sf;
                MessageBox.Show("Size set");
            }
            else
                MessageBox.Show("ENTER VALUES PLEASE"); 
        }
VB
Private Sub btn_setsize_Click(ByVal sender As ObjectByVal As EventArgs)
        
If ((txt_height.Text <> string.Empty)  _
                    
AndAlso (txt_width.Text <> string.Empty)) Then
            Dim 
As Single = 0!
            
Dim As Single = 0!
            
Dim str As String
            
= CType(Convert.ToSingle(str),Single)
            w 
= CType(Convert.ToSingle(str),Single)
            
Dim sf As SizeF = New SizeF(h, w)
            r.Size 
sf
            MessageBox.Show(
"Size set")
        
Else
            
MessageBox.Show("ENTER VALUES PLEASE")
        
End If
    End Sub
This is simple code to set the size of the rectangle.
GET:
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_getsize_Click(object sender, EventArgs e)
        {
            SizeF s = new SizeF();
            s = r.Size;
            MessageBox.Show("current size \n" + "Height:" + s.Height.ToString() + "Width:" + s.Width.ToString());
        }
VB 
    
Private Sub btn_getsize_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As SizeF = New SizeF
        s 
r.Size
        MessageBox.Show((
"current size "& vbLf + ("Height:"  _
                        + (s.Height.ToString + (
"Width:" + s.Width.ToString)))))
    
End Sub
This is simple code to get the size of rectangle.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Rectangle Application";             
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp Rectangle Application" 
    End Sub
This simple article tells that how we can get and set location and size of rectangle using Drawing.RectangleF class in VB and C# .net.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Addition – Subtraction of height – width of rectangles using Drawing.SizeF class vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net