在VB.Net中处理动态按钮和文本框 [英] Handling Dynamic Button and Textbox in VB.Net

查看:121
本文介绍了在VB.Net中处理动态按钮和文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



需要你的建议来纠正我的代码。



我有动态按钮并创建了文本框。



每次点击相应按钮时,我需要将文本框值加1。



的设计模板



按钮文本框按钮



+(text) -





点击+值应逐一增加。 (每次点击+)



以下是我的代码,它指的是我上一个文本框。请建议。



  Dim  i 作为 整数 
对于 i = 1 ListBox1.Items.Count
但= 按钮
but.Location = 点( 500 150 +(i * 25 ))
but.Name = 按钮& i
but.Size = buttonSize
but.Text = -
Me .Controls.Add(but)
AddHandler 但是。点击, AddressOf but_click
Next i
Dim lbl 作为标签
Dim j 正如 整数
对于 j = 1 ListBox1.Items.Count
对于 每个 listboxitem ListBox1.Items
lbl = 标签
lbl.Location = 点( 300 150 +(j * 25 ))
lbl.Name = 标签& j
lbl.Size = labelSize
lbl.Text = ListBox1.GetItemText(listboxitem)
Me .Controls.Add(lbl)
j = j + 1
下一页 listboxitem
下一步 j
Dim but2 As Button

对于 .k = 1 ListBox1.Items.Count
but2 = 按钮
but2.Location = 点( 590 150 +(k * 25 ))
but2.Name = 按钮& k
but2.Size = buttonSize
but2.Text = +
.Controls.Add(but2)
下一步 k
< span class =code-keyword> Me .k = 1

对于 Me .l = 1 To ListBox1.Items.Count
txt = TextBox
txt.Location = Point( 545 150 +(l * 25 ))
txt.Name = TextBox& l
txt.Size = textSize
txt.Text =
.Controls.Add(txt)
' AddHandler txt.Click,AddressOf txt_click
Next l

Dim supbut As 按钮

对于 .si = 1 ListBox1.Items.Count
supbut = 按钮
supbut.Location = 点( 670 150 +(si * 25 ))
supbut.Name = 按钮& si
supbut.Size = buttonSize
supbut.Text = -
.Controls.Add(supbut)
下一页 si

Dim supbut2 As 按钮
Dim sk As 整数
For sk = 1 ListBox1.Items.Count
supbut2 = 按钮
supbut2.Location = 点( 760 150 +(sk * 25 ))
supbut2.Name = 按钮& sk
supbut2.Size = buttonSize
supbut2.Text = +
.Controls.Add(supbut2)
下一页 sk
< span class =code-keyword> Dim suptxt As TextBox
Dim sl 作为 整数
对于 sl = 1 ListBox1.Items.Count
suptxt = TextBox
suptxt.Location = 点( 715 150 +(sl * 25 ))
suptxt.Name = TextBox& sl
suptxt.Size = textSize
suptxt.Text = TextBox& ; sl
.Controls.Add(suptxt)
下一步 sl

Dim abbut As 按钮
Dim ai As 整数
对于 ai = 1 ListBox1.Items.Count
abbut = < span class =code-keyword>新按钮
abbut.Location = 点( 840 150 +(ai * 25 ))
abbut.Name = 按钮& ai
abbut.Size = buttonSize
abbut.Text = -
.Controls.Add(abbut)
下一页 ai
< span class =code-keyword> Dim abbut2 As 按钮
Dim ak 作为 整数
对于 ak = 1 ListBox1.Items.Count
abbut2 = 按钮
abbut2.Location = 点( 930 150 +(ak * 25 ))
abbut2.Name = 按钮& ak
abbut2.Size = buttonSize
abbut2.Text = +
.Controls.Add(abbut2)
下一页 ak
< span class =code-keyword> Dim abtxt As TextBox
Dim al 作为 整数
对于 al = 1 ListBox1.Items.Count
abtxt = TextBox
abtxt.Location = 点( 885 150 +(al * 25 ))
abtxt.Name = TextBox& al
abtxt.Size = textSize
abtxt.Text = TextBox& ; al
Me .Controls.Add(abtxt)
Next al

Dim inbut As 按钮
Dim ii As 整数
对于 ii = 1 ListBox1.Items.Count
inbut = < span class =code-keyword>新按钮
inbut.Location = 点( 1010 150 +(ii * 25 ))
inbut.Name = 按钮& ii
inbut.Size = buttonSize
inbut.Text = -
.Controls.Add(inbut)
下一步 ii

Dim inbut2 As 按钮
Dim ik As 整数
For ik = 1 ListBox1.Items.Count
inbut2 = 按钮
inbut2.Location = 点( 1095 150 +(ik * 25 ))
inbut2.Name = 按钮& ik
inbut2.Size = buttonSize
inbut2.Text = +
.Controls.Add(inbut2)
下一页 ik

Dim intxt As TextBox
Dim il 作为 整数
对于 il = 1 ListBox1.Items.Count
intxt = TextBox
intxt.Location = 点( 1055 150 +(il * 25 ))
intxt.Name = TextBox& il
intxt.Size = textSize
intxt.Text = TextBox& ; il
.Controls.Add(intxt)
下一步 il
结束 Sub

私有 Sub but_click(sender As Object ,e As EventArgs)



这里缺少怎么办?

当我在按钮上使用msgbox时,它给我的消息是我点击按钮但是如何将值添加到适当的文本框? :(请帮助

结束Sub

解决方案

首先,你的代码没有优化。我只能问你一个问题:如果每次迭代 LisBox1.Items 的集合,你想要使用多少 for - next 循环?



其次,使用使用 - End With 块为一个控件设置许多属性。



三,使用简单的技巧:

 公共  Form1 

WithEvents oBtnAdd As 按钮
WithEvents oBtnSubst 作为按钮


私有 Sub CreateDynamicControls()
Dim As Button = Nothing
Dim txt As TextBox = Nothing

Dim i 作为 整数

对于 i = 1 10
但= = 按钮
使用
.Location = < span class =code-keyword>新点( 500 150 +(i * 25 ))
.Tag = i.ToString
.Name = ButtonSubst& i
.Width = 24
.Text = -
.Parent =
AddHandler but.Click, AddressOf oBtnSubst_Click
End 使用

txt = TextBox
使用 txt
.Location = 点( 525 150 +(i * 25 ))
.Name = TextBox& i
.Tag = i.ToString
.Width = 45
.Text = 0
.Enabled = 错误
.Parent =
结束 使用

但= 按钮
使用
.Location = 点( 571 150 +(i * 25 ))
.Tag = i.ToString
.Name = ButtonAdd& i
.Width = 24
.Text = +
.Parent =
AddHandler but.Click, AddressOf oBtnAdd_Click
结束 使用


下一步 i
结束 Sub

公共 Sub oBtnAdd_Click( ByVal sender As Object ByVal e As System.EventArgs)句柄 oBtnAdd.Click
Dim btn 作为按钮=发件人
Dim txt As TextBox = CType .Controls( TextBox& btn.Tag),TextBox)
Dim count As 整数 = 整数 .Parse(txt.Text)
txt.Text = count + 1
结束 Sub

公共 Sub oBtnSubst_Click( ByVal sender As 对象 ByVal e 作为 System.EventArgs)句柄 oBtnSubst.Click
Dim btn < span class =code-keyword> As Button = sender
Dim txt As TextBox = CType Me .Controls( TextBox& btn.Tag),TextBox)
Dim count As 整数 = 整数 .Parse(txt.Text)
txt.Text = count - 1
结束 Sub

公开 Sub ()

' Windows窗体设计器需要此调用。
InitializeComponent()

' 在InitializeComponent()调用后添加任何初始化。
CreateDynamicControls ()
结束 Sub
结束


Hi All

Need your advice in getting my code corrected.

I have dynamic buttons and textbox created.

I need the textbox value to be incremented by 1 for every click of the corresponding button.

for design template

Button Textbox Button

+ ( text ) -


On clicking + value should increase one by one. ( for every click on + )

below is my code which is pointing to my last textbox alone. Please Advice.

  Dim i As Integer
    For i = 1 To ListBox1.Items.Count
        but = New Button
        but.Location = New Point(500, 150 + (i * 25))
        but.Name = "Button" & i
        but.Size = buttonSize
        but.Text = "-"
        Me.Controls.Add(but)
        AddHandler but.Click, AddressOf but_click
    Next i
Dim lbl As Label
Dim j As Integer
    For j = 1 To ListBox1.Items.Count
        For Each listboxitem In ListBox1.Items
            lbl = New Label
            lbl.Location = New Point(300, 150 + (j * 25))
            lbl.Name = "Label" & j
            lbl.Size = labelSize
            lbl.Text = ListBox1.GetItemText(listboxitem)
            Me.Controls.Add(lbl)
            j = j + 1
        Next listboxitem
    Next j
Dim but2 As Button

    For Me.k = 1 To ListBox1.Items.Count
        but2 = New Button
        but2.Location = New Point(590, 150 + (k * 25))
        but2.Name = "Button" & k
        but2.Size = buttonSize
        but2.Text = "+"
        Me.Controls.Add(but2)
    Next k
    Me.k = 1

    For Me.l = 1 To ListBox1.Items.Count
        txt = New TextBox
        txt.Location = New Point(545, 150 + (l * 25))
        txt.Name = "TextBox" & l
        txt.Size = textSize
        txt.Text = ""
        Me.Controls.Add(txt)
        'AddHandler txt.Click, AddressOf txt_click
    Next l

Dim supbut As Button

    For Me.si = 1 To ListBox1.Items.Count
        supbut = New Button
        supbut.Location = New Point(670, 150 + (si * 25))
        supbut.Name = "Button" & si
        supbut.Size = buttonSize
        supbut.Text = "-"
        Me.Controls.Add(supbut)
    Next si

Dim supbut2 As Button
Dim sk As Integer
    For sk = 1 To ListBox1.Items.Count
        supbut2 = New Button
        supbut2.Location = New Point(760, 150 + (sk * 25))
        supbut2.Name = "Button" & sk
        supbut2.Size = buttonSize
        supbut2.Text = "+"
        Me.Controls.Add(supbut2)
    Next sk
Dim suptxt As TextBox
Dim sl As Integer
    For sl = 1 To ListBox1.Items.Count
        suptxt = New TextBox
        suptxt.Location = New Point(715, 150 + (sl * 25))
        suptxt.Name = "TextBox" & sl
        suptxt.Size = textSize
        suptxt.Text = "TextBox" & sl
        Me.Controls.Add(suptxt)
    Next sl

Dim abbut As Button
Dim ai As Integer
    For ai = 1 To ListBox1.Items.Count
        abbut = New Button
        abbut.Location = New Point(840, 150 + (ai * 25))
        abbut.Name = "Button" & ai
        abbut.Size = buttonSize
        abbut.Text = "-"
        Me.Controls.Add(abbut)
    Next ai
Dim abbut2 As Button
Dim ak As Integer
    For ak = 1 To ListBox1.Items.Count
        abbut2 = New Button
        abbut2.Location = New Point(930, 150 + (ak * 25))
        abbut2.Name = "Button" & ak
        abbut2.Size = buttonSize
        abbut2.Text = "+"
        Me.Controls.Add(abbut2)
    Next ak
Dim abtxt As TextBox
Dim al As Integer
    For al = 1 To ListBox1.Items.Count
        abtxt = New TextBox
        abtxt.Location = New Point(885, 150 + (al * 25))
        abtxt.Name = "TextBox" & al
        abtxt.Size = textSize
        abtxt.Text = "TextBox" & al
        Me.Controls.Add(abtxt)
    Next al

Dim inbut As Button
Dim ii As Integer
    For ii = 1 To ListBox1.Items.Count
        inbut = New Button
        inbut.Location = New Point(1010, 150 + (ii * 25))
        inbut.Name = "Button" & ii
        inbut.Size = buttonSize
        inbut.Text = "-"
        Me.Controls.Add(inbut)
    Next ii

Dim inbut2 As Button
Dim ik As Integer
    For ik = 1 To ListBox1.Items.Count
        inbut2 = New Button
        inbut2.Location = New Point(1095, 150 + (ik * 25))
        inbut2.Name = "Button" & ik
        inbut2.Size = buttonSize
        inbut2.Text = "+"
        Me.Controls.Add(inbut2)
    Next ik

Dim intxt As TextBox
Dim il As Integer
    For il = 1 To ListBox1.Items.Count
        intxt = New TextBox
        intxt.Location = New Point(1055, 150 + (il * 25))
        intxt.Name = "TextBox" & il
        intxt.Size = textSize
        intxt.Text = "TextBox" & il
        Me.Controls.Add(intxt)
    Next il
End Sub

Private Sub but_click(sender As Object, e As EventArgs)


missing here what to do??
when i use msgbox on button it gives me message that button i click but how to add the value to appropriate text box? :( please help
End Sub

解决方案

First of all, your code is not optimized. And i can only ask you one question: How many for - next loops you want to use, if every time you iterate through the collection of LisBox1.Items?

Second, use With - End With block to set many properties for one control.

Third, use simple trick:

Public Class Form1

    WithEvents oBtnAdd As Button
    WithEvents oBtnSubst As Button


    Private Sub CreateDynamicControls()
        Dim but As Button = Nothing
        Dim txt As TextBox = Nothing

        Dim i As Integer

        For i = 1 To 10
            but = New Button
            With but
                .Location = New Point(500, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "ButtonSubst" & i
                .Width = 24
                .Text = "-"
                .Parent = Me
                AddHandler but.Click, AddressOf oBtnSubst_Click
            End With

            txt = New TextBox
            With txt
                .Location = New Point(525, 150 + (i * 25))
                .Name = "TextBox" & i
                .Tag = i.ToString
                .Width = 45
                .Text = 0
                .Enabled = False
                .Parent = Me
            End With

            but = New Button
            With but
                .Location = New Point(571, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "ButtonAdd" & i
                .Width = 24
                .Text = "+"
                .Parent = Me
                AddHandler but.Click, AddressOf oBtnAdd_Click
            End With


        Next i
    End Sub

    Public Sub oBtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtnAdd.Click
        Dim btn As Button = sender
        Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
        Dim count As Integer = Integer.Parse(txt.Text)
        txt.Text = count + 1
    End Sub

    Public Sub oBtnSubst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtnSubst.Click
        Dim btn As Button = sender
        Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
        Dim count As Integer = Integer.Parse(txt.Text)
        txt.Text = count - 1
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        CreateDynamicControls()
    End Sub
End Class


这篇关于在VB.Net中处理动态按钮和文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆