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
 

Addition – Subtraction of height – width of rectangles using Drawing.SizeF class vb.net – C#.net.

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

In this simple article you will learn that how we can add and subtract height and width of rectangles using Drawing.SizeF class in VB and C# .net.

 

Height and width of rectangle can be stored in sizef object and addition, subtraction operations can be performed on sizef objects.
Addition:
Object of sizef class stores height and width as a floating point values and these values can be used for the drawing of rectangle. Sizef class provide us a facility to add the objects of sizef class. Basically this adds the height value of one object with height value of other object and does same for width values. For this purpose sizef class provides us a static function named Add (). This function can be called with class name because this is static. It takes two objects of sizef class as a parameter and returns an object of sizef class which stores the result after addition.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_Addtion_Click(object sender, EventArgs e)
        {
            System.Drawing.SizeF s1 = new SizeF(20.5f, 20.5f);
            System.Drawing.SizeF s2 = new SizeF(30.5f, 25.5f);
            System.Drawing.SizeF ans = SizeF.Add(s1, s2);
            float heigth = ans.Height;
            float width = ans.Width;
            MessageBox.Show("Result: \n" + "Heigth :" + heigth.ToString()+ "Width : " + width.ToString());
        }
VB 
    
Private Sub btn_Addtion_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim s1 As System.Drawing.SizeF = New SizeF(20.5!, 20.5!)
        
Dim s2 As System.Drawing.SizeF = New SizeF(30.5!, 25.5!)
        
Dim ans As System.Drawing.SizeF SizeF.Add(s1, s2)
        
Dim heigth As Single = ans.Height
        
Dim width As Single = ans.Width
        MessageBox.Show((
"Result: "& vbLf + ("Heigth :"  _
                        + (heigth.ToString + (
"Width : " + width.ToString)))))
    
End Sub
This is simple code to add height and width of two objects of sizef class.
Subtraction:
Sizef class provide us a facility to subtract the objects of sizef class. Basically this subtracts the height value of one object with height value of other object and does same for width values. For this purpose sizef class provides us a static function named Subtract (). This function can be called with class name because this is static. It takes two objects of sizef class as a parameter and returns an object of sizef class which stores the result after subtraction.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_subtraction_Click(object sender, EventArgs e)
        {
            System.Drawing.SizeF s1 = new SizeF(20.5f, 20.5f);
            System.Drawing.SizeF s2 = new SizeF(30.5f, 25.5f);
            System.Drawing.SizeF ans = SizeF.Subtract(s1, s2);
            float heigth = ans.Height;
            float width = ans.Width;
            MessageBox.Show("Result: \n" + "Heigth :" + heigth.ToString() + "Width : " + width.ToString());
        }
VB 
    
Private Sub btn_subtraction_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim s1 As System.Drawing.SizeF = New SizeF(20.5!, 20.5!)
        
Dim s2 As System.Drawing.SizeF = New SizeF(30.5!, 25.5!)
        
Dim ans As System.Drawing.SizeF SizeF.Subtract(s1, s2)
        
Dim heigth As Single = ans.Height
        
Dim width As Single = ans.Width
        MessageBox.Show((
"Result: "& vbLf + ("Heigth :"  _
                        + (heigth.ToString + (
"Width : " + width.ToString)))))
    
End Sub
This is simple code to subtract height and width of two objects of sizef class.
Convert SizeF to Size:
To convert the sizef class object to size class we have a function named ToSize(). This function takes nothing but returns object of size 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_tosize_Click(object sender, EventArgs e)
        {
            Size s = new Size();
            s = F.ToSize();
            MessageBox.Show("successfully converted");
        }
VB
Private Sub btn_tosize_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As Size = New Size
        s 
F.ToSize
        MessageBox.Show(
"successfully converted")
    
End Sub
This is simple code to convert the sizef into size.
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 add and subtract height and width of rectangles using Drawing.SizeF class in VB and C# .net.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Convert height – width of rectangle into point using Drawing.SizeF class vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net