Can someone please tell me how to control a windows form from another form? I have one form (form2) which has a button. When this button is clicked, I need my other form (Form1) to hide a button. I know all the code except for how to make form2 access form1. It seems so simple, yet I cannot do it. Any help would be appreciated. Thanks

Recommended Answers

All 9 Replies

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New Form2
        frm.Show()
        Me.Hide()
    End Sub

is that what you mean?

Add a constructor to the form (call it Form2) that needs to hide the button on the first form (call it Form1). Like this:

Public Class Form2

   Private ButtonToHide As Button

   Public Sub New(ByVal ButtonToHide As Button)
      Me.InitializeComponent()
      Me.ButtonToHide = ButtonToHide
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      ButtonToHide.Hide()
   End Sub
End Class

Then in Form1, call the constructor like this:

Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim frm As New Form2(Button2)
      frm.Show()
   End Sub
End Class

This code might work. I will try and tell you if this is what I was looking for. thanks for the reply

It is giving me a syntaz error for the "Handles" command

can you give me a ss for the error?

Do you have a "Button1" on your form or did you name it something else?

Please post your code.

Supposedly you have two Forms.

Form1 Put this Code in a Button to show Form2
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Show()
End Sub

'Form2 code to hide a Button in Form1
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Button1.Hide()

End Sub

'Hope it helps you ^^

Thank you. The code that I needed was the simple

Form1.Button1.Hide()

I had no idea how to control the other form. But now I know. Thank you for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.