VB.NET Parenting makes control invisible - vb.net

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

Related

How do I detect when any button is pressed without writing individual code for each button in VB.net?

I'm very new to coding and currently I am coding a VB.net Windows Form Hangman game. I have 26 letter buttons that when pressed I would like the text of them (A,B,C,Etc.) to be put into a Text box so the player knows what letter they have inputted and they can then submit their guess. However, so far the only way I have figured out how to detect when any of the buttons is pressed is by writing the code individually for each button which looks very messy and inefficient. I was wondering if it was possible to detect when any of the buttons is pressed (And know which one) without writing code for each individual button?
This is only for 4 of the buttons so I have to do this 22 more times for what I want to do and more for any additional buttons:
Current Code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox2.Text = Button3.Text
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox2.Text = Button4.Text
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
TextBox2.Text = Button5.Text
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox2.Text = Button6.Text
End Sub`
This is the game when run. When any of the letter buttons are pressed I would like them to be detected. The text box on the right shows the button that is pressed:(https://hi.stack.imgur.com/92sDy.png)
I hope someone can help,
thank you in advance,
Georgitzu
Here's the basic idea ...
' assign all button click events to the same routine
Private Sub buttonHandler(sender As Object, e As EventArgs) _
Handles Button1.Click, Button1.Click .... Button26.Click
' create a generic button object to handle the button clicked
Dim obtn as Button = CType(sender, Button)
' display the text
TextBox2.Text = obtn.Text
Hope this helps,
Mike

How to add an object to another form?

I am trying to define an object in one form, and then when the user clicks a button, the object will be added to a new form that shows.
Form 1
Public Shared label As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
label.Text = "test"
End Sub
Form 2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Controls.Add(Form1.label)
End Sub
Whenever I run this, it does not put the label on the second form.
Can anyone tell me what I am doing wrong, or show a better way of doing this?

How do I convert picturebox.click event into a button.click event

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

VB.NET Create a to a form attached Preview window

I want to create a little window which gets visible when I press a button and is attached to the main Form (like shwon below). I want to use this window to show preview of Images (I gonna have a Listbox and depending on which entry is selected, a picture is shown) How can I do this? And how can I be sure that the window will always be attached to the main Form (not depending on resolution). I tried to do it with a second form, but I can't fix it in the correct position.
regards
Assuming your preview form class is frmPreview and you open it this way:
Private mPreviewForm As frmPreview
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
mPreviewForm = New frmPreview
mPreviewForm.Show()
AttachPreviewForm()
End Sub
Then you must reposition it every time the main form is changing size or location:
Private Sub AttachPreviewForm()
If mPreviewForm IsNot Nothing Then
mPreviewForm.AttachForm(Me)
End If
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
AttachPreviewForm()
End Sub
Private Sub Form1_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
AttachPreviewForm()
End Sub
And in the frmPreview:
Public Sub AttachForm(parent As Form)
Location = New Point(parent.Left + parent.Width, parent.Top)
Size = New Size(200, parent.Height)
End Sub

Trying to enter a name in a textbox, hit ok, and have that form close and pass the info to another forms label

***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub