Using multiple instances of a specific UserControl within an application to reference itself

411 views Asked by At

I got this class in Visual Basic:

class Dumbo
    Inherits System.Windows.Forms.UserControl

    Me.DataSet = New DataSet
    Me.BS = new System.Windows.Forms.BindingSource

Sub New()
    Me.BS.DataMember = "Table1"
    Me.DataSource = Me.DataSet
End Sub 

    'Other commands here for doing interactions with a SQL database

End Class

Using a tabcontrol on the Main Window, I have this user control working fine on the first tabpage so that operator can enter in data, or view previous records in the database, but now I need to add this to another panel on another tab control

So just doing this:

dim Dumbo1 = new Dumbo      'Usercontrol on the first tab
dim Dumbo2 = new Dumbo      'Usercontrol on the second tab

I know that simply writing Dumbo2 = Dumbo1 doesn't work at all. I could try and set the Dataset and the Bindingsource to each other, but isn't there a simpler way of just referencing to the class instead?

EDIT: I'm drawing from an experience in C++ where you use the address of a class (&foo) if you wanted to reference the same class.

so if you have this:

ClassFoo foo = new ClassFoo();
ClassFoo *foo1 = &foo;

Therefore I can use the same class fields, properties, and methods to read/write to the database. The data has to be the same datarow ON BOTH TABPAGES

I was hoping there was a cheap and dirty trick in VB.NET where I can reference the same Usercontrol, so when the user switches tabpages, it will be the same data, like be able to read the same data, and/or write and then commit the changes and it would reflect the modifications on the other page. Or similar to the ByRef (or Ref in C#) to reference THAT class ONLY.

I hope that clears it up a bit. :-)

1

There are 1 answers

0
the_lotus On

Your tabcontrol control could contain your New Dumbo variable. Each tab would reference that variable from the tabcontrol.

Inside a TabPage, you would have

CType(Me.Parent, CustomTabControl).Dumbo

An other option is when the TabControl initialize each tab, it would send out the variable.

In the TabControl you would have

someName = New Dumbo

For Each tab As TabPage In Me.TabPages
    tab.Tag = someName
Next