将Text值填充到Listbox [英] Populating the Text values to Listbox
问题描述
Hi Team,
我需要在按钮点击时将文本框的值移动到Listbutton。(Buton和Listbox是手动创建的。
为了您的信息,我在表格运行中动态创建了文本框。我需要将这些值发送到列表框。
我的代码在下面
Dim 但是作为按钮= 没什么
Dim but2 As Button = Nothing
Dim but3 As Button = 没什么
' Dim txt As TextBox = Nothing
< span class =code-keyword> Dim supbut As Button = Nothing
Dim supbut2 作为按钮= 没什么
Dim suptxt As TextBox = Nothing
Dim abbut As Button = Nothing
Dim abbut2 作为按钮= 没什么
Dim abtxt As TextBox = Nothing
Dim inbut As Button = Nothing
Dim inbut2 As Button = Nothing
Dim intxt As TextBox = Nothing
Dim lbl As Label = Nothing
对于 我 .i = 1 ListBox1.Items.Count
但= 新按钮
使用但
.Location = 新点( 500 , 150 +(i * 25 ))
.Tag = i.ToString
.Name = 按钮& i
.Width = 24
.Text = +
.Parent = 我
AddHandler but.Click, AddressOf oBtn_Click
End 使用
txt = 新 TextBox
使用 txt
.Location = 新点( 545 , 150 +(i * 25 ))
.Tag = i.ToString
.Name = TextBox& i
.Width = 24
.Parent = 我
< span class =code-keyword>结束 使用
but2 = 新按钮
使用但是
.Location = 新点(< span class =code-digit> 590 , 150 +(i * 25 ) )
.Tag = i.ToString
.Name = 按钮& ; i
.Width = 24
.Text = -
.Parent = 我
AddHandler but2.Click, AddressOf oBtn2_Click
结束 使用
下一步 i
对于 我 .j = 1 ListBox1.Items.Count
supbut = 新按钮
使用 supbut
.Location = 新点( 670 , 150 +(j * <跨越ss =code-digit> 25 ))
.Tag = j.ToString
.Name = 按钮& j
.Width = 24
.Text = +
.Parent = 我
AddHandler supbut.Click, AddressOf osupBtn_Click
结束 使用
suptxt = 新 TextBox
使用 suptxt
.Location = 新点( 715 , 150 +(j * 25 ))
.Tag = j.ToString
.Name = SupTextBox& j
.Width = 24
.Parent = 我
< span class =code-keyword>结束 使用
supbut2 = 新按钮
使用 supbut2
.Location = 新点(< span class =code-digit> 760 , 150 +(j * 25 ) )
.Tag = j.ToString
.Name = 按钮& ; j
.Width = 24
.Text = -
.Parent = 我
AddHandler supbut2.Click, AddressOf osupBtn2_Click
结束 使用
下一步 j
对于 < span class =code-keyword> Me .k = 1 ListBox1.Items .Count
abbut = 新按钮
使用 abbut
。位置= 新点( 840 , 150 + (k * 25 ))
.Tag = k.ToString
.Name = < span class =code-string>按钮& k
.Width = 24
.Text = +
.Parent = 我
' AddHandler abbut.Click,AddressOf oabBtn_Click
End 使用
abtxt = 新 TextBox
使用 abtxt
.Location = 新点( 885 , 150 +(k * 25 ))
.Name = TextBox& k
.Width = 24
.Tag = k.ToString
.Parent = 我
结束 使用
abbut2 = < span class =code-keyword>新按钮
使用 abbut2
.Location = 新点( 930 , 150 +(k * 25 ))
.Tag = k.ToString
.Name = 按钮& k
.Width = 24
.Text = -
.Parent = 我
' AddHandler abbut2.Click,AddressOf oabBtn2_Click
结束 使用
下一步 k
用于 l = 1 ListBox1.Items.Count
inbut = 新按钮
使用 inbut
.Location = 新点( 1010 , 150 +(l * 25 < /跨度>))
.Tag = l.ToString
.Name = 按钮& l
.Width = 24
.Text = +
.Parent = 我
' AddHandler inbut.Click,AddressOf oinBtn_Click
End 使用
intxt = 新 TextBox
使用 intxt
.Location = 新点( 1055 , 150 +(l * 25 ))
.Name = TextBox& l
.Width = 24
.Tag = l.ToString
.Parent = 我
结束 使用
inbut2 = < span class =code-keyword>新按钮
使用 inbut2
.Location = 新点( 1095 , 150 +(l * 25 ))
.Tag = l.ToString
.Name = 按钮& l
.Width = 24
.Text = -
.Parent = 我
' AddHandler inbut2.Click,AddressOf oinBtn2_Click
End 使用
下一步 l
结束 Sub
列表框显示片段
< pre lang =vb> Dim ctl As Control = 我 .GetNextControl(我, True )
执行
如果 ctl txt 那么
ListBox2.Items.Add( ctl.Text)
结束 如果
ctl = 我 .GetNextControl(ctl, True )
循环直到ctl txt
ListBox2.Items.Add(txt.Text)
列表框代码段有效,但ts将所有文本框值都设置为列表框。我只需要将一组特定文本框的值添加到一个列表框中,而将其他文本框添加到单独的列表框中。
Listbox2 Listbox3
但文字但是文字但是
但是文字但文字但是
但是文字但文字但是
但是文本,但文字但
好的,所以按钮点击的事件处理程序将如下所示:
私有 Sub oBtn_Click(发件人 As System。 Object ,e As System.EventArgs)
结束 Sub
您可以通过测试每个按钮来为所有动态创建的按钮使用此事件处理程序,例如所以:
如果 typeof (发件人) 按钮然后
Dim thisButton As Button = DirectCast (发件人,按钮)
如果 thisButton.Name = 将您的名字命名为 然后
' 您的代码
结束 如果
结束 如果
当您枚举表单上的所有控件时,您可以以相同的方式查找文本框。按照上面按钮的方式测试文本框,然后在将其转换为本地文本框控件后检查它的名称。当您找到要查找的名称时,将其文本复制到您想要的列表框中。如果要创建多个文本框,请将它们命名为abtxt1,abtxt2,ect等,然后测试名称的子字符串。当你找到一个abtxt文本框时,你知道你找到了一个你想要写入列表框的价值...
希望这会有所帮助。
- Pete
哦,我必须这样说 - 你应该使用描述性名称和骆驼案例。可读性就是一切。不要害怕为你的对象使用长名称...而不是abtxt,你应该使用像aboutUhisTextBox这样的东西。这样,当您明年再次使用此代码时,或者甚至更长时间,您将能够轻松阅读并了解您正在做什么。如果你发现自己和团队合作,他们会感谢你。
我解决了。谢谢你的时间。
Hi Team,
I need the values of the textbox to be moved to Listbutton on button click.( Buton and Listbox are created manually).
For your information i have created textboxes dynamically on form run. I need those values to be sent to listbox.
My code is below
Dim but As Button = Nothing
Dim but2 As Button = Nothing
Dim but3 As Button = Nothing
'Dim txt As TextBox = Nothing
Dim supbut As Button = Nothing
Dim supbut2 As Button = Nothing
Dim suptxt As TextBox = Nothing
Dim abbut As Button = Nothing
Dim abbut2 As Button = Nothing
Dim abtxt As TextBox = Nothing
Dim inbut As Button = Nothing
Dim inbut2 As Button = Nothing
Dim intxt As TextBox = Nothing
Dim lbl As Label = Nothing
For Me.i = 1 To ListBox1.Items.Count
but = New Button
With but
.Location = New Point(500, 150 + (i * 25))
.Tag = i.ToString
.Name = "Button" & i
.Width = 24
.Text = "+"
.Parent = Me
AddHandler but.Click, AddressOf oBtn_Click
End With
txt = New TextBox
With txt
.Location = New Point(545, 150 + (i * 25))
.Tag = i.ToString
.Name = "TextBox" & i
.Width = 24
.Parent = Me
End With
but2 = New Button
With but2
.Location = New Point(590, 150 + (i * 25))
.Tag = i.ToString
.Name = "Button" & i
.Width = 24
.Text = "-"
.Parent = Me
AddHandler but2.Click, AddressOf oBtn2_Click
End With
Next i
For Me.j = 1 To ListBox1.Items.Count
supbut = New Button
With supbut
.Location = New Point(670, 150 + (j * 25))
.Tag = j.ToString
.Name = "Button" & j
.Width = 24
.Text = "+"
.Parent = Me
AddHandler supbut.Click, AddressOf osupBtn_Click
End With
suptxt = New TextBox
With suptxt
.Location = New Point(715, 150 + (j * 25))
.Tag = j.ToString
.Name = "SupTextBox" & j
.Width = 24
.Parent = Me
End With
supbut2 = New Button
With supbut2
.Location = New Point(760, 150 + (j * 25))
.Tag = j.ToString
.Name = "Button" & j
.Width = 24
.Text = "-"
.Parent = Me
AddHandler supbut2.Click, AddressOf osupBtn2_Click
End With
Next j
For Me.k = 1 To ListBox1.Items.Count
abbut = New Button
With abbut
.Location = New Point(840, 150 + (k * 25))
.Tag = k.ToString
.Name = "Button" & k
.Width = 24
.Text = "+"
.Parent = Me
' AddHandler abbut.Click, AddressOf oabBtn_Click
End With
abtxt = New TextBox
With abtxt
.Location = New Point(885, 150 + (k * 25))
.Name = "TextBox" & k
.Width = 24
.Tag = k.ToString
.Parent = Me
End With
abbut2 = New Button
With abbut2
.Location = New Point(930, 150 + (k * 25))
.Tag = k.ToString
.Name = "Button" & k
.Width = 24
.Text = "-"
.Parent = Me
'AddHandler abbut2.Click, AddressOf oabBtn2_Click
End With
Next k
For l = 1 To ListBox1.Items.Count
inbut = New Button
With inbut
.Location = New Point(1010, 150 + (l * 25))
.Tag = l.ToString
.Name = "Button" & l
.Width = 24
.Text = "+"
.Parent = Me
' AddHandler inbut.Click, AddressOf oinBtn_Click
End With
intxt = New TextBox
With intxt
.Location = New Point(1055, 150 + (l * 25))
.Name = "TextBox" & l
.Width = 24
.Tag = l.ToString
.Parent = Me
End With
inbut2 = New Button
With inbut2
.Location = New Point(1095, 150 + (l * 25))
.Tag = l.ToString
.Name = "Button" & l
.Width = 24
.Text = "-"
.Parent = Me
' AddHandler inbut2.Click, AddressOf oinBtn2_Click
End With
Next l
End Sub
listbox display snippet
Dim ctl As Control = Me.GetNextControl(Me, True)
Do
If ctl Is txt Then
ListBox2.Items.Add(ctl.Text)
End If
ctl = Me.GetNextControl(ctl, True)
Loop Until ctl Is txt
ListBox2.Items.Add(txt.Text)
the listbox snippet works, but ts getting all text box values to listbox. i need to add only the particular set of textbox''s value to one listbox and others in separate listbox.
Listbox2 Listbox3
but text but but text but
but text but but text but
but text but but text but
but text but but text but
Ok, So your event handler for a button click will look something like this:
Private Sub oBtn_Click(sender As System.Object, e As System.EventArgs) End Sub
You can use this event handler for all your dynamically created buttons by testing for each button, like so:
If typeof(sender) Is Button Then Dim thisButton As Button = DirectCast(sender, Button) If thisButton.Name = "the name your looking for" then ' Your code here End If End If
While your enumerating all the controls on your form, you can look for your text boxes the same way. Test for a text box the way I did for a button above, and then check it''s name once you cast it to a local textbox control. When you find the name your looking for, copy it''s text to the listbox your want. If you are creating multiple text boxes, name them something like abtxt1, abtxt2, ect - and then test for substrings of the names. When you find an "abtxt" text box, then you know you''ve found one who''s value you want to write to the list box...
Hope this helps.
- Pete
Oh, And I HAVE to say this - you should be using descriptive names and camel case. Readability is everything. Don''t be afraid to use long names for your objects... instead of abtxt, you should be using something like aboutThisUserTextBox. This way when you come back to this code next year, or even longer you will be able to read it easily and understand what you were doing. If you ever find yourself working with a team, they will thank you for it.
I got it resolved. Thanks for your time.
这篇关于将Text值填充到Listbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!