---
title: "how to adjust the size of drawn rectangle at runtime"  
description: "how to adjust the size of drawn rectangle at runtime"  
author: "Madhu Mitha"  
published: 2015-03-26  
updated: 2015-04-01  
canonical: https://www.mindstick.com/forum/23058/how-to-adjust-the-size-of-drawn-rectangle-at-runtime  
category: ".net"  
tags: ["vb.net"]  
reading_time: 3 minutes  

---

# how to adjust the size of drawn rectangle at runtime

hi,,\
\
it is possible to adjust the [size](https://www.mindstick.com/forum/159799/how-can-you-manage-the-size-of-mdf-and-ldf-files) of a rectangle which is drawn in a picturebox with drawrectangle function.. During run time i want to adjust my rectangle width using [numericupdown](https://www.mindstick.com/forum/442/validation-on-numericupdown-control-in-c-sharp-dot-net) control.. \
\
thank u

## Replies

### Reply by Madhu Mitha

hai,,\
\
thank u it worked well...

### Reply by Anonymous User

According to my understanding about your problem scenario, you want to increase height and width according to the numericupdown control on click

**Try this code:**

```
Private Sub numericUpDown1_ValueChanged(sender As Object, e As EventArgs)                Dim aa As Integer = Convert.ToInt32(numericUpDown1.Value.ToString())                pictureBox1.Width = aa                pictureBox1.Height = aaEnd Sub
```

![how to adjust the size of drawn rectangle at runtime](https://www.mindstick.com/mindstickforums/4641dce4-fef9-42e1-8b39-b51df63e1146/images/ff092902-8a19-40dc-9b5e-a7a1997b99be.png)

I hope it will work for u !! J

### Reply by Madhu Mitha

Hai..Kamlakar Singh,\
I tried your code but what i needed is that... When i press my numeric updown control the width of my rectangle should be increased..\
\
For Example..\
if i created a rectangle with code

```
Dim width=50 Dim height=25Dim rect As New Rectangle(0, 0, width,height) g.DrawRectangle(Pens.Black, rect)
```

Now, When I press NumericUpDown control the width of my rectangle should be increased as 51,52 53, etc..Is thr any possible way to do it..\
Thank U\

### Reply by Anonymous User

I have some code that might help you:\

' Rectangle to draw

```
Private Rct As New Rectangle(0, 0, 0, 0)Private offsetX As Integer = 0Private offsetY As Integer = 0 Sub Main() Handles MyBase.Load     ' Some image to
use   
MiniPictureBox.Image = My.Resources.P6130003   
MainPictureBox.Image = My.Resources.P6130003 End Sub Private Sub MiniPictureBox_MouseDown(sender As Object, e As
MouseEventArgs) Handles MiniPictureBox.MouseDown     If e.Button =
Windows.Forms.MouseButtons.Left Then         If Not
Rct.Contains(e.Location) Then            ' New
rectangle           
Rct.Location = New Point(e.X, e.Y)        Else            ' Moving a
rectangle            offsetX =
Rct.X - e.X            offsetY =
Rct.Y - e.Y        End If     ElseIf e.Button =
Windows.Forms.MouseButtons.Right Then         ' Clears the
screen of a rectangle        Rct = New
Rectangle(0, 0, 0, 0)       
MiniPictureBox.Invalidate()     End If End Sub Private Sub MiniPictureBox_MouseMove(sender As Object, e As
MouseEventArgs) Handles MiniPictureBox.MouseMove     ' Event handler to
update the picture of the rectangle    If e.Button =
Windows.Forms.MouseButtons.Left Then         If
Rct.Contains(e.Location) Then            ' Move the
box            Rct.X =
e.X + offsetX            Rct.Y =
e.Y + offsetY           
MainPictureBox.Invalidate()        Else            ' Update
the size of the box            Rct.Width
= e.X - Rct.X            Rct.Height
= e.Y - Rct.Y        End If        
MiniPictureBox.Invalidate()     End If  End Sub Private Sub MiniPictureBox_MouseUp(sender As Object, e As
MouseEventArgs) Handles MiniPictureBox.MouseUp     ' Event handler to
call the paint event for runtime display   
MiniPictureBox.Invalidate()   
MainPictureBox.Invalidate() End Sub Private Sub MiniPictureBox_Paint(sender As Object, e As
PaintEventArgs) Handles MiniPictureBox.Paint     Dim myPen As Pen =
New Pen(Brushes.Red, 2)   
e.Graphics.DrawRectangle(myPen, Rct) End Sub Private Sub MainPictureBox_Paint(sender As Object, e As
PaintEventArgs) Handles MainPictureBox.Paint     If Rct.Width >
0 Then        Dim biggerRec
As Rectangle = CalculateRectangle(MainPictureBox)         Dim myPen As
Pen = New Pen(Brushes.Red, 2)       
e.Graphics.DrawRectangle(myPen, biggerRec)    End If End Sub Private Function CalculateRectangle(currentPicture As
PictureBox) As Rectangle     Try        Dim newWidth
As Integer = (Rct.Width / MiniPictureBox.Width) * currentPicture.Image.Width        Dim newHeight
As Integer = (Rct.Height / MiniPictureBox.Height) * currentPicture.Image.Height        Dim newX As
Integer = (Rct.X / MiniPictureBox.Width) * currentPicture.Image.Width        Dim newY As
Integer = (Rct.Y / MiniPictureBox.Height) * currentPicture.Image.Height        Return New
Rectangle(newX, newY, newWidth, newHeight)    Catch ex As
Exception       
MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine +
ex.StackTrace)    End Try End Function
```

\

This code will allow you to create, move, and clear a rectangle. One point of caution is in the calculations for changing the size of the rectangle, you have to ensure exception handling is properly inserted for any arithmetic exceptions.\


---

Original Source: https://www.mindstick.com/forum/23058/how-to-adjust-the-size-of-drawn-rectangle-at-runtime

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
