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
 

Take union and adjust location of rectangles using Drawing.RectangleF class vb.net – C#.net.

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

In this simple article you will learn that how we can take union of two rectangles and how we can adjust location of rectangle using Drawing.RectangleF class in VB and C# .net.

 

Union:
To take union of rectangles means to create a third rectangle. This rectangle can contain both rectangles in it. To take union rectanglef class provides us a static function named union. This function takes two rectangles as a parameter and returns an object of rectanglef 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_union_Click(object sender, EventArgs e)
        {
           System.Drawing.RectangleF r = new RectangleF(20f, 20f, 20f, 20f);
            System.Drawing.RectangleF r1 = new RectangleF(21f, 21f, 21f, 21f);
            RectangleF ans = RectangleF.Union(r, r1);
            MessageBox.Show("Done");
        }
VB
Private Sub btn_union_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As System.Drawing.RectangleF = New RectangleF(20!, 20!, 20!, 20!)
        
Dim r1 As System.Drawing.RectangleF = New RectangleF(21!, 21!, 21!, 21!)
        
Dim ans As RectangleF RectangleF.Union(r, r1)
        MessageBox.Show(
"Done")
    
End Sub
This is simple code to take union of two rectangles.
Offset:
Offset means to adjust the location of the rectangle on form. For this purpose rectanglef class provides us a function named offset. This function has two overloaded methods.
First Method:
This method takes an object of pointf class which contains the values of x and y co-ordinates by which the location will adjust and returns nothing.
To demonstrate make a window application. Drag two text boxes and one button on form.
Now write the following code on Button click event:
C#
private void btn_adjust_Click(object sender, EventArgs e)
        {
            System.Drawing.RectangleF r = new RectangleF(20f, 20f, 20f, 20f);
            if (txt_x.Text != string.Empty && txt_y.Text != string.Empty)
            {
                float x = 0f;
                float y = 0f;
                string str = txt_x.Text;
                x = (float)Convert.ToSingle(str);
                 str = txt_y.Text;
                y = (float)Convert.ToSingle(str);
                PointF pf = new PointF(x,y);
                r.Offset(pf);
                MessageBox.Show("adjusted");
            }
            else
                MessageBox.Show("please enter values");
            }
VB 
    
Private Sub btn_adjust_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As System.Drawing.RectangleF = New RectangleF(20!, 20!, 20!, 20!)
        
If ((txt_x.Text <> string.Empty)  _
                    
AndAlso (txt_y.Text <> string.Empty)) Then
            Dim 
As Single = 0!
            
Dim As Single = 0!
            
Dim str As String = txt_x.Text
            x 
= CType(Convert.ToSingle(str),Single)
            
str = txt_y.Text
            y 
= CType(Convert.ToSingle(str),Single)
            
Dim pf As PointF = New PointF(x, y)
            r.Offset(pf)
            MessageBox.Show(
"adjusted")
        
Else
            
MessageBox.Show("please enter values")
        
End If
    End Sub
Second Method:
This method takes two float values, x and y co-ordinates, by which the location will adjust and returns nothing.
To demonstrate make a window application. Drag two text boxes and one button on form.
Now write the following code on Button click event:
C#
private void btn_adjust1_Click(object sender, EventArgs e)
        {
            System.Drawing.RectangleF r = new RectangleF(20f, 20f, 20f, 20f);
            if (txt_x1.Text != string.Empty && txt_y1.Text != string.Empty)
            {
                float x = 0f;
                float y = 0f;
                string str = txt_x.Text;
                x = (float)Convert.ToSingle(str);
                str = txt_y.Text;
                y = (float)Convert.ToSingle(str);
                r.Offset(x,y);
                MessageBox.Show("adjusted");
            }
            else
                MessageBox.Show("please enter values");
        }
VB
This is simple code to adjust location of rectangle on form.
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 take union of two rectangles and how we can adjust location of rectangle using Drawing.RectangleF class in VB and C# .net.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get – Set Location and size of rectangle using Drawing.RectangleF class vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net