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
 

Convert height – width of rectangle into point using Drawing.SizeF class vb.net – C#.net.

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

n this simple article you will learn that how we can convert height and width of rectangle in a pointF using Drawing.SizeF class in VB and C# .net.

 

We can get height and width of rectangle in object of sizef class.
SizeF class:
This class belongs to a system.drawing namespace. This class is used to store the height and width of a rectangle and to perform some operations on these height and width like convert to point, conversion from sizef to size etc.
Constructor:
This class has four overloaded constructors.
First constructor:
This is an empty constructor.
C#
System.Drawing.SizeF F = new SizeF();
VB
Dim As System.Drawing.SizeF = New SizeF
Second constructor:
This constructor takes an object of pointf class.
C#
PointF p = new PointF();
System.Drawing.SizeF F = new SizeF(p);
VB
Dim As PointF = New PointF
Dim As System.Drawing.SizeF = New SizeF(p)
Third constructor:
This constructor takes an object of sizef class.
C#
SizeF s = new SizeF();
System.Drawing.SizeF F = new SizeF(s);
VB
Dim As SizeF = New SizeF
Dim As System.Drawing.SizeF = New SizeF(s)
Fourth constructor:
This constructor takes two float values one is width and second is height.
C#
System.Drawing.SizeF F = new SizeF(20.5f,20.5f);
VB
Dim As System.Drawing.SizeF = New SizeF(20.5!, 20.5!)
Height:
To get or set the height we have a property of sizef class named height. This property takes and returns a floating point value.
SET:
To demonstrate make a window application. Drag text box and one button on form.
Now write the following code on Button click event:
C#
private void btn_Heigth_Click(object sender, EventArgs e)
        {
            if (txt_height.Text != string.Empty)
            {
                float Height = 0f;
                string str = txt_height.Text;
                Height = (float)Convert.ToSingle(str);
                F.Height = Height;
                MessageBox.Show("vlaue is set " + Height.ToString());
            }
            else
                MessageBox.Show("ENTER VALUE");
        }
VB
Private Sub btn_Heigth_Click(ByVal sender As ObjectByVal As EventArgs)
        
If (txt_height.Text <> string.Empty) Then
            Dim 
Height As Single = 0!
            
Dim str As String = txt_height.Text
            Height 
= CType(Convert.ToSingle(str),Single)
            F.Height 
Height
            MessageBox.Show((
"vlaue is set " + Height.ToString))
        
Else
            
MessageBox.Show("ENTER VALUE")
        
End If
    End Sub
This is simple code to set the height.
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_getheigth_Click(object sender, EventArgs e)
        {
           float heigth = F.Height;
            string str = heigth.ToString();
            MessageBox.Show("Height :" + str);
        }
VB 
    
Private Sub btn_getheigth_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim heigth As Single = F.Height
        
Dim str As String = heigth.ToString
        MessageBox.Show((
"Height :" str))
    
End Sub
This is simple code to get the height.
Width:
To get or set the width we have a property of sizef class named width. This property takes and returns a floating point value.
SET:
To demonstrate make a window application. Drag text box and one button on form.
Now write the following code on Button click event:
C#
private void btn_width_Click(object sender, EventArgs e)
        {
            if (txt_width.Text != string.Empty)
            {
                float width = 0f;
                string str = txt_width.Text;
              width = (float)Convert.ToSingle(str);
              F.Width = width;
                MessageBox.Show("vlaue is set " +width.ToString());
            }
            else
                MessageBox.Show("ENTER VALUE");
        }
VB
Private Sub btn_width_Click(ByVal sender As ObjectByVal As EventArgs)
        
If (txt_width.Text <> string.Empty) Then
            Dim 
width As Single = 0!
            
Dim str As String = txt_width.Text
            width 
= CType(Convert.ToSingle(str),Single)
            F.Width 
width
            MessageBox.Show((
"vlaue is set " + width.ToString))
        
Else
            
MessageBox.Show("ENTER VALUE")
        
End If
    End Sub
This is simple code to set the width.
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_getwidth_Click(object sender, EventArgs e)
        {
            float width = F.Width;
            string str = width.ToString();
            MessageBox.Show("Width :" + str);
        }
VB
 Private Sub btn_getwidth_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim width As Single = F.Width
        
Dim str As String = width.ToString
        MessageBox.Show((
"Width :" str))
    
End Sub
This is simple code to get the width.
Convert SizeF to Point:
To convert the sizef class object to pointf class we have a function named ToPointF(). This function takes nothing but returns object of pointf class.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_point_Click(object sender, EventArgs e)
        {
            PointF p = new PointF();
            p = F.ToPointF();
            MessageBox.Show("SUCCESSFULLY CONVERTED");
        }
VB
 Private Sub btn_point_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As PointF = New PointF
        p 
F.ToPointF
        MessageBox.Show(
"SUCCESSFULLY CONVERTED")
    
End Sub
This is simple code to convert the sizef into pointf.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp SizeF Application";             
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp SizeF Application" 
    End Sub
This simple article tells that how we can convert height and width of rectangle in a pointF using Drawing.SizeF class in VB and C# .net.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Draw a polygon on form using Graphics class in vb.net - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net