【问题标题】:vbnet dynamic button that when is erased it will erase the text from textboxvb net 动态按钮,当被擦除时,它将擦除文本框中的文本
【发布时间】:2014-07-21 18:57:07
【问题描述】:

我的项目是在 vb net 中实现的。我有一个主窗体,它有 button1 和一个 flowoutpanel。
当我按下按钮时,会动态显示一个按钮并将其放置在 flowoutpanel 内。
同时,它从 combobox1 中获取选定的文本并将其显示在动态按钮属性上。
它还保存在另一个组合框中,该组合框中存储了所有字符串,以便以后使用,然后按保存以显示在文本框上。
如果我单击动态按钮,它会关闭并消失。
这就是我卡住的地方。
我想知道当我删除动态按钮时是否有任何方法,也可以删除从存储字符串的组合框中显示的文本属性。
我想这样做是因为当您从组合框1 中选择一个字符串值并添加许多动态按钮时,您可能希望删除第一个。
但在这种情况下,它不会从存储它们的组合框中删除正确的字符串值。
换句话说,我正在寻找一种方法来添加到动态 button_click 子(从我的代码中可以看到),这会从组合框中删除第一个动态按钮的文本值..
我尝试过类似 :Me.FlowLayoutPanel4.Controls.RemoveAt(0) 但它会擦除位置 0 处的值。
提前谢谢你,我在下面附上我的代码。

     Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    MessageBox.Show("Dynamic Button is clicked.")
    Me.FlowLayoutPanel5.Controls.RemoveAt(0)

End Sub

以上是点击按钮时触发的代码。

下面是动态创建按钮的代码:

     Sub CreateDynamicButton()
    ' Create a Button object 
    Dim dynamicButton As New Button

    ' Set Button properties

    dynamicButton.Location = New Point(584, 90)
    dynamicButton.Height = 20
    dynamicButton.Width = 52
    ' Set background and foreground
    dynamicButton.BackColor = Color.Green
    dynamicButton.ForeColor = Color.Blue
    dynamicButton.Text = ComboBox6.Text
    dynamicButton.Name = "DynamicButton"
    dynamicButton.Font = New Font("Georgia", 8)
    AddHandler dynamicButton.Click, AddressOf DynamicButton_Click
    ' Add Button to the Form. Placement of the Button
    ' will be based on the Location and Size of button

    Me.FlowLayoutPanel1.Controls.Add(dynamicButton)

End Sub

或者如果有办法做这样的事情:

            textbox.text=dynamicbutton.text

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您可以创建一个子程序来为您完成这项工作,这样应该可以工作,它使用ComboBox.ObjectCollection.Contains 方法来检查项目是否存在,然后它使用 ComboBox.ObjectCollection.IndexOf 获取项目的索引位置,以便您可以使用ComboBox.ObjectCollection.RemoveAt 方法将其删除。

    Public Sub RemoveItem(item As String, values As ComboBox.ObjectCollection)
        If values.Contains(item) Then
            values.RemoveAt(values.IndexOf(item))
        End If
    End Sub
    

    用法:

    RemoveItem("your string value here", ComboBox1.Items)
    

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      相关资源
      最近更新 更多