Hack Forums
[.NET] VB.NET Help - Send Back Control To Main Form - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] VB.NET Help - Send Back Control To Main Form (/showthread.php?tid=5072983)



VB.NET Help - Send Back Control To Main Form - Diamondo - Nov 27, 2015

I'm using this code to bring one control from form1 to form2:

Code
Me.Controls.Add(Form1.TabControl1)
    Me.Panel1.Size = Form1.TabControl1.Size

    Form1.TabControl1.SelectedTab.Show()

    Form1.TabControl1.Location = Me.Panel1.Location
    Panel1.Hide()

Everything is fine. But the problem is that the tabcontrol permanently disappeared from form1 and I can't send back the tabcontrol. Something like this will work:

Code
Form1.Controls.Add(Me.TabControl1)

But the problem is that the TabControl1 don't exist in form2 until I send it to it.

I don't ask to use the same tabcontrol at the same time on two different times. I just want to send it back to it's original form.

I know that is a variable that keeps track of that tabcontrol. It is form1.tabcontrol, even if it is actually moved to form2 and I tried this code: (But it also doesn't worked...)

Code
Form1.Controls.Add(Form1.TabControl1)

Please help me! :(


* - Tedd - Nov 27, 2015

Just mirror the control on your second form and add a handler..

EG:

Form1.vb (One button)
Code
Public Class Form1
    Public Sub F2Button1_Click(sender As Object, e As EventArgs)
        Button1_Click(sender, e)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox("You clicked me!")
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Form2.Show()
    End Sub
End Class

Form2.vb (One button)
Code
Public Class Form2
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
        AddHandler Button1.Click, AddressOf Form1.F2Button1_Click
    End Sub
End Class



* - Diamondo - Nov 27, 2015

(Nov 27, 2015 11:39 AM)Tedd Wrote:  Just mirror the control on your second form and add a handler..

EG:

Form1.vb (One button)
Code
Public Class Form1
    Public Sub F2Button1_Click(sender As Object, e As EventArgs)
        Button1_Click(sender, e)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox("You clicked me!")
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Form2.Show()
    End Sub
End Class

Form2.vb (One button)
Code
Public Class Form2
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
        AddHandler Button1.Click, AddressOf Form1.F2Button1_Click
    End Sub
End Class

This is not a bad idea. Thank you!

Also I found myself another better way to do it:

Code
Dim Formx As New Form1

I jost got everything from there and it worked like a charm. 🤝