[英]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. 我创建了一个带有几个文本框,带遮罩的文本框,组合框,一个复选框和3个按钮的用户控件。
I have created a form (Form1) with a tab control (TabControl1) that has 1 tab page on it (TabPage1). 我创建了一个带有选项卡控件(TabControl1)的窗体(Form1),该控件上具有1个选项卡页(TabPage1)。 I have added my user control to TabPage1 and the control assumes the name ContactTab1.
我已将用户控件添加到TabPage1,并且该控件采用名称ContactTab1。 This was done through the VB.net form design, not by code.
这是通过VB.net表单设计完成的,而不是通过代码完成的。
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. 我的问题是,当我单击编辑按钮时,我需要在单击按钮的选项卡上设置
ckbDeleteContact.Checked = False
和ckbDeleteContact.Visible = False
。 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. 当我单击删除按钮时,我需要在单击按钮的选项卡上设置
ckbDeleteContact.Checked = True
和ckbDeleteContact.Visible = True
。 I can access the check box on the first tab without a problem with the statement ContactTab1.ckbDeleteContact.Checked = False
. 我可以访问第一个选项卡上的复选框,而不会出现语句
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:
下面是我的Form1代码,我已经注释掉了我需要进行的工作:
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. 正如@HansPassant所说,您只需要向用户控件添加属性即可访问其中的控件。 I'm not a vb.net guy, but I think this is going to help you:
我不是vb.net的人,但是我认为这将为您提供帮助:
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.
我只需要知道如何获取父控件的名称,在这种情况下,该控件是用户定义的名为ContactTabX的控件,其中X到1到n次按下添加按钮n次时添加的n个控件。 I could always access them by saying something like
ContactTab5.ckbDeleteContact.Visible = True
or whatever. 我总是可以通过说出诸如
ContactTab5.ckbDeleteContact.Visible = True
或类似内容来访问它们。 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). 我花了几个小时试图弄清楚,这里是我在发布问题后大约10分钟就能弄清楚的东西(去计算)。 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. 再次感谢您的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.