I'm trying to have two options to draw a border around a picture box.
I can click on the picturebox to highlight but now would like to be able to use a button to do the same thing.
Private Sub imgLabel_Click(sender As Object, e As EventArgs) Handles imgLabel.Click
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle BorderBounds.Inflate(-1, -1)
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics, BorderBounds, Color.Orange, ButtonBorderStyle.Solid)
If Not (HighLightededPictureBox Is Nothing) Then
HighLightededPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighLightededPictureBox = CType(sender, PictureBox)
When I try to add a button click I get the Exception Unhandled - System.windows.forms.button to type System.windows.forms.picturebox error.
I have tried to add the button click event after "Handles" which causes the above error.
Private Sub imgLabel_Click(sender As Object, e As EventArgs) Handles imgLabel.Click, button1.click
I'm pretty new to programming and my searching results are turning anything up of value. I don't fully understand Ctypes/Directcasts.
Any help is greatly appreciated.
Ok, so I ended up going with this...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim boxsize As New Size(192, 169)
Dim recpoint As New Point(0, 0)
Dim myrectangle As New Rectangle(recpoint, boxsize)
myrectangle.Inflate(-3, -3)
Dim G As Drawing.Graphics = PictureBox1.CreateGraphics
Dim Pen As New Pen(Color.Orange, 5)
G.DrawRectangle(Pen, myrectangle)
End Sub
Seems to be working ok, but requires a lot of manual entry of points. I have 6 more of these.
You could try
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
imgLabel_Click(imgLabel, nothing)
End Sub
Related
How Can I find the HEX color of a specific image pixel? I've Tried This:
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
PictureBox1.Image = Image.FromFile("c:\image.jpg")
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Dim b As Bitmap = Me.PictureBox1.Image
Dim ver As String = b.GetPixel(e.X, e.Y).ToString
Dim veri As String = Hex(b.GetPixel(e.X, e.Y).ToArgb)
End Sub
This doesn't work as expected:
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
PictureBox1.Image = My.Resources.MyImage
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Dim b As Bitmap = Me.PictureBox1.Image 'b is not streched..?
Dim ver As String = b.GetPixel(e.X, e.Y).ToString
Dim veri As String = Hex(b.GetPixel(e.X, e.Y).ToArgb)
End Sub
How can I fix this?
I'll post this as an answer because I want to post some code. You seem to be under the impression that setting the SizeMode of a PictureBox does/should affect the Image it contains. It doesn't and it shouldn't. That property is for the UI only, i.e. it affects what the user sees but has no effect on the data. To prove that to yourself, try creating a WinForms app with two PictureBoxes and two Buttons and add this code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Image = Image.FromFile("image file path here")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If PictureBox1.SizeMode = PictureBoxSizeMode.Normal Then
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Else
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox2.Image = PictureBox1.Image
MessageBox.Show(PictureBox2.Image.Size.ToString())
End Sub
You can set the image path to any file you want.
If you click Button2 you'll see the same image appear in PictureBox2 as in PictureBox1 and the dimensions will be displayed. If you click Button1 you'll see the image in PictureBox1 change dimensions to fit the control. If you click Button2 again, what you see in PictureBox2 won't change and neither will the dimensions displayed, because the Image object hasn't changed.
If you want to get the colour of the pixel under the mouse cursor then you'd need to call DrawToBitmap on the control itself to get a Bitmap of what's actually displayed on the screen.
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
Here is my code that does not work. What am I doing wrong?
You're looping over all the Controls contained in Me (probably the Form)
In that collection there is more than just the PictureBoxes so you need to filter to only get those :
(see OfType)
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls.OfType(Of PictureBox)
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
I have a Button and a DataGridView. When I press the button I want the DataGridview to be visible and when I press it again be invisible
This is what I have tried so far:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim buttonId As New Button
Dim dvg As New DataGridView
Try
dvg = DirectCast(sender, DataGridView)
dvg.Visible = True
Catch ex As Exception
End Try
End Sub
I know the question might sound very basic, but I am quite inexperienced, so the help will be very much appreciated
first off, i would have the datagridview object as a member of your class.
then i would turn it on and off like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If dgv.Visible Then
dgv.Hide()
Else
dgv.Show()
End If
End Sub
hope this helps
Not sure adding controls dynamically is a good idea, but...
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Try
Dim dgv As New DataGridView
dgv.Name = "dgvN"
dgv.Size = New Size(Me.ClientSize.Width - 20, 300)
dgv.Top = Me.ClientSize.Height / 2 - dgv.Height / 2
dgv.Left = Me.ClientSize.Width / 2 - dgv.Width / 2
dgv1.BringToFront()
Me.Controls.Add(dgv) ' use Controls() of desired container
Dim newButton As New Button
newButton.Text = "DGV On/Off"
newButton.Width = TextRenderer.MeasureText(newButton.Text, newButton.Font).Width + 20
newButton.Tag = "dgvN"
Me.Controls.Add(newButton) ' use Controls() of desired container
AddHandler newButton.Click, AddressOf DGVVisibleButtonClick
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub DGVVisibleButtonClick(sender As Object, e As EventArgs)
Dim dgv As DataGridView = Me.Controls(sender.tag)
dgv.Visible = Not dgv.Visible
End Sub
I've been having lots of problems getting the print dialog and page set up to work together to print text from a textbox, ive spent hours doing research and trying everything, I cannot get it to work, can someone please help me, and give me the code to print a document from a textbox and also get page set up to work with it (just like in notepad)
Here is my code thats not working
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If (PrintDialog1.ShowDialog() = DialogResult.OK) Then
PrintDocument1.Print()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PageSetupDialog1.ShowDialog()
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim textArea As New Rectangle()
e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, 100, 100)
End Sub
Thanks
If you use a PrintDocument you can draw text to a Rectangle and it will wrap naturally. You can also employ a StringFormat object for other formatting help.
Private Sub printDoc_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) Handles printDoc.PrintPage
Dim textArea As New Rectangle(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
e.Graphics.DrawString(textbox1.Text, New Font("Consolas", 10), Brushes.Navy, textArea)
'other stuff
End Sub
I have created a form.
I have put 2 panels on it, namely "Panel1" and "Panel2".
I put one button in the "Panel2".
Then I put another button to the form, namely "Button2".
A click on "Button2" executes the following code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Panel2.Parent = Me.Panel1
' Me.Panel1.Location = New Point(0, 0) this does not work either
End Sub
When I click "Button2", "Panel2" simply disappears.
I expected "Panel2" to be placed within "Panel1", but it is not shown there.
Does anybody see what I did wrong?
Thank you for the help!
you need to set the position of the other pannel:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Panel2.Parent = Me.Panel1
Me.Panel2.Location = new point(0,0)
End Sub