Passing variables between windows forms in VS 2010

前端 未结 3 475
你的背包
你的背包 2021-01-14 03:34

I have two forms. Form 1 allows the user to pick an employee from a dropdown combo box. That employee is then passed onto Form 2 where the user enters additional information

相关标签:
3条回答
  • 2021-01-14 04:10

    Change your code in Form2.vb for the New sub to this:

    Public Sub New(ByVal Employee As String)
    
        ' This call is required by the designer.
    
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
    
        MsgBox(Employee)
    
    End Sub
    

    If you don't call InitializeComponent(), your complete GUI is not going to render.

    0 讨论(0)
  • 2021-01-14 04:13

    I am late but I think this answer can help.

    For example, Form1 named "menu" opens and passes variable to Form2 named "ordine". The variable to pass is "hotel"

    In menu on button_click

    Dim ordinef As New Ordine()
    
    If ordinef Is Nothing Then
        'control that form is not opened yet if open close before
        ordinef = New Ordine()
    Else
        ordinef.Close()
        ordinef = New Ordine()
    End If
    
    ordinef.hotel = hotel
    
    ordinef.Show()
    

    In Form2 (Ordine):

    Private Sub Ordine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    public hotel as string
    
    msgbox hotel
    

    That's it!!

    0 讨论(0)
  • 2021-01-14 04:20

    You don't even have to use the InitializeComponent or New functions.

    I have made an example to show how easily this can be done.

    enter image description here

    Clicking "Show Form" results in the below:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Form2.Show()
    End Sub
    

    which is simply used to display the second form.

    By clicking "Pass Data" results in the following code:

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Form2.Label1.Text = TextBox1.Text
    End Sub
    

    As shown above you can pass the data directly from control to control. The same idea can be used with variables too.

    0 讨论(0)
提交回复
热议问题