NuWin
NuWin

Reputation: 276

Passing Values from Multiple UserControls

Note: Similar Question can be found here.

How can I pass values from multiple User Controls? The question in the link above provides an answer however I find the answer very tedious in my situation and there is a delay in passing of the values. (I have to cycle to and from UserControl1 and UserControl2 multiple times while in UserControl1 committing a change of a textbox or label to see any passing of values in UserControl2.)

Either way, since I have multiple UserControls in which each has many textboxes, labels, and comboboxes, I would very much not like having to create separate Sub Routines and EventHandlers for each and every control with a value that I would like to pass.

Is there a better way? I was thinking something like...

'In UserControl10
 Dim UserControl1 As New UserControl1
 Dim UserControl2 As New UserControl2
 Dim UserControl3 As New UserControl3

 UC10Label1.Text = UserControl1.Label1.Text
 UC10TextBox1.Value = UserControl2.TextBox1.Value
 UC10ComboBox1.Text = UserControl3.ComboBox1.SelectedItem

The code above obviously does not work the way I would imagine, how can I achieve something similar with the least amount of code?

Edited: I have multiple custom UserControls in which I use as 'views'. In each UserControl there are labels, textboxes, & comboboxes. I have a Panel1 in which on a triggered event, will display a UserControl(1-9) in the panel; each UserControl is displayed one at a time and is contingent on an event. I want to be able to pass values from each UserControl(1-9) to UserControl10's labels, textboxs, or comboboxs etc..

Upvotes: 0

Views: 222

Answers (1)

David Wilson
David Wilson

Reputation: 4439

I'm guessing that the last three lines aren't in a Sub. They're probably not working because they're executing before the form has been shown.

If you want to update them automatically in your program, you should put them in a sub, but suspend the form layout while they're updating and then resume layout when the code has finished. Like this

Private Sub UpdateUserControls()
    Me.SuspendLayout()
    UC10Label1.Text = UserControl1.Label1.Text
    UC10TextBox1.Value = UserControl2.TextBox1.Value
    UC10ComboBox1.Text = UserControl3.ComboBox1.SelectedItem
    Me.ResumeLayout()
End Sub

Depending on when you want to update these controls, you can do it each time the form is shown by placing the above Sub in the Form's .Shown event.

Or you could do it automatically every so often by placing it in a Timer's .tick event.

Or you could choose to update them at certain points in your program by placing the sub somewhere in your code.

Upvotes: 1

Related Questions