简体   繁体   中英

Accessing controls located in a dynamically created user control vb.net

I am a casual programmer with not a lot of experience. I am happy I have made it this far on my own (with help of course from this site and others like it). But now I need some help.

I have created a user control with several text boxes, masked text boxes, combo boxes, a check box and 3 buttons.

I have created a form (Form1) with a tab control (TabControl1) that has 1 tab page on it (TabPage1). I have added my user control to TabPage1 and the control assumes the name ContactTab1. This was done through the VB.net form design, not by code.

When I run my form I have code so that when I click on my add button, it adds another tab with my user control added to it (no matter which tab I may be on). It works great, I can add as many tabs as I want. When I click on my edit or delete button, they work great in the sense that I know which tab the button is on when it gets clicked. My problem is when I click the edit button I need to set ckbDeleteContact.Checked = False and ckbDeleteContact.Visible = False on the tab that the button was clicked. When I click the delete button I need to set ckbDeleteContact.Checked = True and ckbDeleteContact.Visible = True on the tab that the button was clicked. I can access the check box on the first tab without a problem with the statement ContactTab1.ckbDeleteContact.Checked = False .

So my question is, how do I access all these text boxes, masked text boxes, combo boxes, and my check box on these dynamically added controls? Below is my code for Form1 and I have commented out what I need working:

Public Class Form1
Private intTabPage As Integer = 1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TabPage1.Text = "Contact #" & intTabPage
    ContactTab1.ckbDeleteContact.Checked = False
    ContactTab1.ckbDeleteContact.Visible = False
    TabPage1.Name = "TabPage" & intTabPage
    intTabPage = intTabPage + 1
End Sub

Private Sub UC_btnAddContact_Click() Handles ContactTab1.UC_btnAddContact_Click
    AddNewTab()
End Sub

Private Sub UC_btnEditContact_Click() Handles ContactTab1.UC_btnEditContact_Click
    '**DEBUG: See which tab the button is on when clicked
    MessageBox.Show("The edit button from the following tab was clicked: " & TabControl1.SelectedTab.Name() & vbCrLf & "The edit button on the following contact tab was clicked: " & TabControl1.SelectedTab.Controls.Item(0).Name(), "Check", MessageBoxButtons.OK, MessageBoxIcon.Information)

    'This code is what needs to work. ContactTabObject would have naming convention "ContactTabX" where X = the tab # 1 through the highest tab #
    'ContactTabObject.ckbDeleteContact.Checked = False
    'ContactTabObject.ckbDeleteContact.Visible = False
End Sub

Private Sub UC_btnDeleteContact_Click() Handles ContactTab1.UC_btnDeleteContact_Click
    '**DEBUG: See which tab the button is on when clicked
    MessageBox.Show("The delete button from the following tab was clicked: " & TabControl1.SelectedTab.Name() & vbCrLf & "The delete button on the following contact tab was clicked: " & TabControl1.SelectedTab.Controls.Item(0).Name(), "Check", MessageBoxButtons.OK, MessageBoxIcon.Information)

    'This code is what needs to work. ContactTabObject would have naming convention "ContactTabX" where X = the tab # 1 through the highest tab #
    'ContactTabObject.ckbDeleteContact.Visible = True
    'ContactTabObject.ckbDeleteContact.Checked = True
End Sub

Function AddNewTab()
    Dim NewTab As New TabPage
    Dim NewContactTab As New ContactTab

    TabControl1.Controls.Add(NewTab)
    TabControl1.SelectTab(NewTab)
    NewTab.Text = "Contact #" & intTabPage
    NewTab.BackColor = System.Drawing.Color.Transparent
    NewTab.Controls.Add(NewContactTab)
    NewTab.Name = "TabPage" & intTabPage

    NewContactTab.Location = New System.Drawing.Point(6, 6)
    NewContactTab.BackColor = System.Drawing.Color.Transparent
    NewContactTab.ckbDeleteContact.Checked = False
    NewContactTab.ckbDeleteContact.Visible = False
    AddHandler (NewContactTab.btnAddContact.Click), AddressOf UC_btnAddContact_Click
    AddHandler (NewContactTab.btnEditContact.Click), AddressOf UC_btnEditContact_Click
    AddHandler (NewContactTab.btnDeleteContact.Click), AddressOf UC_btnDeleteContact_Click
    NewContactTab.Name = "ContactTab" & intTabPage

    intTabPage = intTabPage + 1
End Function

End Class

Once I get this figured out, I should be good to go and I should be able to get the rest on my own. In case you are wondering, I will also be filling in the options for my combo boxes with data from a database. I will then be using the form to take all the data in it and either adding, editing, or deleting the information from a database.

Thanks in advance.

As @HansPassant said you just need to add properties to your user control to get access to your controls in it. I'm not a vb.net guy, but I think this is going to help you:

Public Function MyTextbox() As System.Windows.Forms.TextBox
  Return Textbox1
End Function

You can write this in your user control code.

Ok, maybe I was not the clearest in my post or I just don't understand the encapsulation thing. I can access all my controls since they are standard controls. I just needed to know how I could get the name of the parent control, which in this case is the user defined control named ContactTabX where X = 1 through n controls that were added when I pressed my add button n times. I could always access them by saying something like ContactTab5.ckbDeleteContact.Visible = True or whatever. I did not want to hardcode since I would not be sure how many tabs were added so I wanted a way to know which tab I was on when the button was pressed that way I could change that check box property on that particular tab (since every tab is identical).

I spent hours trying to figure it out and well here is what I was able to figure out about 10 mins after posting the question (go figure). I hope this helps anyone else. And for you experts out there, any feedback is appreciated on my solution. I always like to learn :)

So replacing the subs I originally posted with these worked perfectly.

 Private Sub UC_btnEditContact_Click() Handles ContactTab1.UC_btnEditContact_Click
    '**DEBUG: See which tab the button is on when clicked
    'MessageBox.Show("The edit button from the following tab was clicked: " & TabControl1.SelectedTab.Name() & vbCrLf & "The edit button on the following contact tab was clicked: " & TabControl1.SelectedTab.Controls.Item(0).Name(), "Check", MessageBoxButtons.OK, MessageBoxIcon.Information)

    Dim Contact As ContactTab = TabControl1.SelectedTab.Controls.Item(0)
    Contact.Name = TabControl1.SelectedTab.Controls.Item(0).Name()
    Contact.ckbDeleteContact.Visible = False
    Contact.ckbDeleteContact.Checked = False
    Contact = Nothing
End Sub

Private Sub UC_btnDeleteContact_Click() Handles ContactTab1.UC_btnDeleteContact_Click
    '**DEBUG: See which tab the button is on when clicked
    ' MessageBox.Show("The delete button from the following tab was clicked: " & TabControl1.SelectedTab.Name() & vbCrLf & "The delete button on the following contact tab was clicked: " & TabControl1.SelectedTab.Controls.Item(0).Name(), "Check", MessageBoxButtons.OK, MessageBoxIcon.Information)

    Dim Contact As ContactTab = TabControl1.SelectedTab.Controls.Item(0)
    Contact.Name = TabControl1.SelectedTab.Controls.Item(0).Name()
    Contact.ckbDeleteContact.Visible = True
    Contact.ckbDeleteContact.Checked = True
    Contact = Nothing
End Sub

Thanks again for the input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM