vb.net 如何将新选项卡添加到 TabControl 并能够更新其中的控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7035847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add new tabs to a TabControl and be able to update controls in them?
提问by akiwiguy
I need to be able to programmatically create new tabs on a TabControl, add controls to them, and be able to update the controls in each tab from another function. I already have a function to add tabs to the control, and to add controls to those tabs when they are created, but I'm stuck as to update the controls after they have been created.
我需要能够以编程方式在 TabControl 上创建新选项卡,向它们添加控件,并能够从另一个功能更新每个选项卡中的控件。我已经有一个功能可以向控件添加选项卡,并在创建这些选项卡时将控件添加到这些选项卡中,但是我在创建控件后被困住了。
EDIT: This is what I have to make the tabs and add the controls:
编辑:这是我必须制作选项卡并添加控件的内容:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "(empty)"
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Parent = tabpage
textbox2.Parent = tabpage
textbox1.Location = New Point(10, 10)
textbox2.Location = New Point(10, 30)
TabControl1.TabPages.Add(tabpage)
End Sub
Urgh. I can't seem to get back into the account I used to post this question, so I have to post my follow-up to Tim's question in the comments for the previous answer as a new answer.
Debug.WriteLine(TabControl1.TabPages.Item(2).Controls.Find("textbox1", True).Count)
returns 0. The tab and the controls have been created prior.
呃。我似乎无法回到我用来发布此问题的帐户中,因此我必须在上一个答案的评论中发布我对 Tim 问题的跟进作为新答案。
Debug.WriteLine(TabControl1.TabPages.Item(2).Controls.Find("textbox1", True).Count)
返回 0。之前已经创建了选项卡和控件。
采纳答案by Tim
Ok - I'll give it a shot, but I'm real rusty with WinForms, slightly less rusty with VB.NET. You'll need to locate the control you want to update, and you should be able to do that through the Controls
collection of the appropriate container - in this case, most likely a TabPage
:
好的 - 我会试一试,但我对 WinForms 真的很生疏,对 VB.NET 的生疏程度略低。您需要找到要更新的控件,并且您应该能够通过Controls
适当容器的集合来做到这一点- 在这种情况下,很可能是TabPage
:
TextBox tb1 = CType(tabpage.Controls.Find("textBox1", false), TextBox)
tb1.Text = "I set the text!"
Syntax might be slightly off, but hopefully this will at least point you in the right direction.
语法可能略有偏差,但希望这至少会为您指明正确的方向。
See Control.ControlCollection.Find Method
参见Control.ControlCollection.Find 方法
UPDATED
更新
Hans Passant suggested that this isn't working because you didn't set the Name
property (I'm assuming he means the name of the controls, not the tab page). I did a little more reading on the ControlsCollection.Find
method, and MSDN says "Searches for controls by their Name property and builds an array of all the controls that match." You (and I) were trying to find the control by the instance name (textbox1, textbox2) - which were the instancenames for the two controls, not the control names.
Hans Passant 建议这不起作用,因为您没有设置该Name
属性(我假设他指的是控件的名称,而不是标签页)。我对该ControlsCollection.Find
方法进行了更多阅读,MSDN 说“按名称属性搜索控件并构建所有匹配控件的数组”。您(和我)试图通过实例名称 (textbox1, textbox2) 查找控件 - 这是两个控件的实例名称,而不是控件名称。
So try this instead:
所以试试这个:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "(empty)"
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Parent = tabpage
textbox2.Parent = tabpage
textbox1.Location = New Point(10, 10)
textbox2.Location = New Point(10, 30)
textbox1.Name = "textbox1"
textbox2.Name = "textbox2"
TabControl1.TabPages.Add(tabpage)
End Sub
Then you can find the control using:
然后您可以使用以下命令找到控件:
TextBox tb1 = CType(TabControl1.TabPages.Item(TabControl1.TabPages.Count - 1).Controls.Find("textbox1", True)(0), TextBox)
tb1.text = "Test"
Give that a try and see if it works for you. The key (and what I missed looking at your code last night as it was past my bedtime for me) was there was now way to identify the control in the Find
method.
试一试,看看它是否适合你。关键(我昨晚错过了看你的代码,因为它已经过了我的睡觉时间)是现在有办法识别Find
方法中的控件。
回答by ThePrivateGeek
I know this is old, but just an opinion, write a function that creates AND returns the control you want to add to the tabpage. In that case, you will have a reference to the control ready at hand. For example:
我知道这很旧,但只是一个意见,编写一个函数来创建并返回要添加到标签页的控件。在这种情况下,您将拥有对手头准备好的控件的引用。例如:
Public Function CreateNewListBoxInsideNewPageTab() As ListBox
Dim newTab As New TabPage()
newTab.Text = "Tab " & TabControl1.TabPages.Count + 1
Dim newLst As New ListBox
newLst.Dock = DockStyle.Fill
newTab.Controls.Add(newLst)
TabControl1.TabPages.Add(newTab)
TabControl1.SelectedTab = newTab
Return newLst
End Function
Now when I call this function, I'll have the new listbox as an object:
现在,当我调用这个函数时,我会将新的列表框作为一个对象:
Dim newListBox as ListBox = CreateNewListBoxInsideNewPageTab()
newListBox.Items.Add("This is a new listbox item!")