Draw rectangle around Textbox inside a Groupbox - vb.net

I want to add a custom border around a TextBox control which is in a GroupBox.
Since I'm new to this Graphic stuff I'm having a hard time figuring out the problem.
This is the code i'm using:
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim _g As Graphics = Me.GroupBox1.CreateGraphics
Dim pen As New Pen(Color.Red, 2.0)
_g.DrawRectangle(pen, New Rectangle(TextBox1.Location, TextBox1.Size))
pen.Dispose()
End Sub
This form is a secondary form that shows when I click on a button from the Main form. The Red Border appears for a second when the form loads and then disappears.

You need to handle the GroupBox paint event, not the form.
Private Sub HandleGroupBox1Paint(sender As Object, e As PaintEventArgs) Handles GroupBox1.Paint
Using p As New Pen(Color.Red, 2.0)
e.Graphics.DrawRectangle(p, Me.TextBox1.bound)
End Using
End Sub

Related

Not able to clear drawings in PictureBox using VB.NET

I use the following code to make simple free-hand (brush) drawings over PictureBox1. Drawing is fine, but not able to clear the drawings I made permanently. If I click Button1 the drawings will be cleared, but once I move over PictureBox1 all old drawings (and PictureBox1 image) appear again. Any suggestions?
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
mousePath.StartFigure()
End If
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
'// slide annotations
If e.Button = MouseButtons.Left Then
Try
mousePath.AddLine(e.X, e.Y, e.X, e.Y) 'Add mouse coordiantes to mousePath
Catch
End Try
End If
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
'// slide annotations
Try
'// drwaing options
myUserColor = System.Drawing.Color.Red
myAlpha = 255
myPenWidth = 3
CurrentPen = New Pen(myUserColor, myPenWidth)
e.Graphics.DrawPath(CurrentPen, mousePath)
Catch
End Try
End Sub
Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
Dim g As Graphics
g = PictureBox1.CreateGraphics()
g.Clear(PictureBox1.BackColor)
g.Dispose()
End Sub
NEVER call CreateGraphics. ALWAYS do ALL your drawing in the Paint event handler. Your are creating a Graphics object in your Click event handler and clearing that, but what use is that when you do the drawing again in Paint event handler the next time that event is raised?
What you need to do is store all the data that represents your drawing in one or more fields, update that data whenever you want to change the drawing and draw using that data in the Paint event handler. If you want to clear the drawing, you clear that data and then force a repaint by calling Invalidate. In your Paint event handler you are drawing a GraphicsPath stored in the mousePath field. That means that, in your Click event handler, you need to clear that GraphicsPath and then call Invalidate. That will then prompt a Paint event that will first clear the existing drawing and then do the new. As there is no new to do, it will remain clear.

Display an image in picturebox from specific position

I have a picturebox that "works" as button. I have load an image map as background image, to use it for buttons conditions (click, hover etc).
As default, background image shows it's top left position, the first icon. Let's say, how can I move (x) to 32px and (y) to 64? Something like css styles background-position: 32px 64px; for example.
If you need to reposition the image then I wouldn't use a PictureBox, just a Panel or draw the image on the surface of the form.
It is possible though with the following code. Notice that it removes the PictureBox's Image so you are losing the functionality of a PictureBox.
Public Class Form1
Private _moveIt As Boolean = False
Private _coyote As Image
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_coyote = PictureBox1.Image
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_moveIt = True
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If _moveIt = True Then
PictureBox1.Image = Nothing
e.Graphics.DrawImage(_coyote, New Rectangle(New Point(32, 64), _
New Size(_coyote.Width, _coyote.Height)))
End If
End Sub
End Class
To keep the PcitureBox's functionality (to use its Image property) you would have to create a new image which is a transformed version of the original image.

Drawing rectangle on picture box from another form's button

I have a form with a picturebox on it, and another form with a button on it. How would I picturebox1_paint on the first form once I press the button on the other form?
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawRectangle(Pens.Blue, x.text, y.text, width.text, height.text)
End Sub
this is what I have, what would I need to alter to get it to work?
At Form1, add
Sub PaintImage(rectangle As Rectangle)
' create image in size of PictureBox1
Dim image As New Bitmap(PictureBox1.Width, PictureBox1.Height)
' paint on created image
Using gr As Graphics = Graphics.FromImage(image)
gr.DrawRectangle(New Pen(Color.Red), rectangle)
End Using
' display finished image in PictureBox1
PictureBox1.Image = image
End Sub
and call it from the form with button:
Sub Button1_Click(sender as Object, e as EventArgs) Handles Button1.Click
Form1.PaintImage(New Rectangle(x, y, width, height)) ' assume we already have x, y, w, h
End Sub
You can delete your Paint() event handler shown in the question. Its purpose is to repaint PictureBox1.Image if its size or visibility is changed and in most cases you can leave it as it is.

Why does a Panel automatically scroll up when a different TextBox.Text is updated?

I've created a form and displayed it inside the panel but sadly the form can't fit. So I need to use Panel.Autoscroll = True in order to navigate the whole form.
When I click the textbox1.text in the lowest part of the form, the panel automatically scrolls up and the textbox can't be seen. Even if I scroll down it continuously scrolls up automatically.
How can I stop it from scrolling up?
Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.TopLevel = False
Me.Panel1.Controls.Clear()
Me.Panel1.Controls.Add(Form2)
Form2.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label2.Text = Date.Now.ToString("dd/MMM/yyyy ddddddddd")
Label1.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
I realized the panel scrolls up when the date text is set to the label. Is there a way to prevent this?
You can resolve your problem by using your own panel and overriding the ScrollToControl function:
Public Class PanelEx
Inherits Panel
Protected Overrides Function ScrollToControl(c As Control) As Point
Return Me.DisplayRectangle.Location
End Function
End Class
Replace your Panel1 panel with this new one in your ToolBox after rebuilding your solution.

Graphics.DrawRectangle not working in control events

First of all thank you for taking the time out of your busy schedule to assist me.
I am developing a project (Win Application) with a Form and 3 textboxes (TextBox1, TextBox2 and TextBox3).
I need draw a rectangle around the textbox when focused this.
The code is:
Private Sub TextBox123_Enter(sender As Object, e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter
Using g As Graphics = Me.CreateGraphics
Dim r As Rectangle = sender.Bounds
r.Inflate(4, 4)
g.DrawRectangle(Pens.Blue, r)
End Using
End Sub
The problem is the following:
The first time the textbox1 gains focus rectangle is not drawn.
The first time the textbox2 gains focus rectangle is not drawn.
Why not the rectangle is drawn when the first two events enter are fired?
Drawing with CreateGraphics is almost always not the correct approach. If you notice also, when you move from one box to another, the old rectangle is not being erased. You need to use the Form_Paint event to get it to work right. Or...perhaps simpler would be to create a UserControls which is 1-2 pixels larger than a child TextBox and set the backcolor of the UserControl canvas, draw your rectangle when the control gets the focus.
For form paint:
Public Class Form1
Private HotControl As Control
If you are only going to do TextBoxes, you can declare it As TextBox. This way it allows you to do the same for other control types. Set/clear the tracker:
Private Sub TextBox3_Enter(sender As Object, e As EventArgs) Handles TextBox3.Enter,
TextBox2.Enter, TextBox1.Enter
HotControl = CType(sender, TextBox)
Me.Invalidate()
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave,
TextBox2.Leave, TextBox3.Leave
HotControl = Nothing
Me.Invalidate()
End Sub
The Me.Invalidate tells the form to redraw itself, which happens in Paint:
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If HotControl IsNot Nothing Then
Dim r As Rectangle = HotControl.Bounds
r.Inflate(4, 4)
e.Graphics.DrawRectangle(Pens.Blue, r)
End If
End Sub
You should also turn on Option Strict.
Try this in the click event handler
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
Using g As Graphics = Me.CreateGraphics()
Dim rectangle As New Rectangle(TextBox1.Location.X - 1, _
TextBox1.Location.Y - 1, _
TextBox1.Size.Width + 1, _
TextBox1.Size.Height + 1)
g.DrawRectangle(Pens.Blue, rectangle)
End Using
End Sub