Vb listbox display in multiply forms?

2019-09-01 14:36发布

问题:

I'm using VB.Net and Visual Studio 2012.

I need to display the same list box in two different forms.

The Scenario

I'm creating a program that allows its users to create orders in a cafe. When the user has completed their order they press the button "save/ complete". When they press this button their order is added to a listbox on the side of the new order form.

On the other form (summary), there will be the same listbox displayed.

So my question is how to display the same listbox on multiple forms?

回答1:

This code will add ComboBox1 to a second form:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm2 As New Form2
    frm2.Controls.Add(ComboBox1)
    frm2.Show()
End Sub


回答2:

You can do this maybe by saving the combo items to a textfile, that the other form can read the same file? I think it's not that professional, but it is an option :)

Example creating a .txt file - http://www.homeandlearn.co.uk/NET/nets8p4.html

Example reading a .txt file into a control - https://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

The reading of the text file and placing it in the control need to be in the MyBase.Load event.

Hopefully I helped you a little bit!