Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在文本框中从DropDownList选择多个项目,无重复项_Asp.net_Vb.net_Web Applications_Drop Down Menu - Fatal编程技术网

Asp.net 在文本框中从DropDownList选择多个项目,无重复项

Asp.net 在文本框中从DropDownList选择多个项目,无重复项,asp.net,vb.net,web-applications,drop-down-menu,Asp.net,Vb.net,Web Applications,Drop Down Menu,我使用以下技术从DropDownList中选择多个项目到文本框中,没有重复项,但是我不认为这是最合适的方法,没有任何想法 再次按,并选择相同的值 从DDL中选择另一个值并按下按钮 这是我的密码 Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click If TextBox2.Text.Contains(DropDownList1.Sel

我使用以下技术从DropDownList中选择多个项目到文本框中,没有重复项,但是我不认为这是最合适的方法,没有任何想法

再次按,并选择相同的值

从DDL中选择另一个值并按下按钮

这是我的密码

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox2.Text.Contains(DropDownList1.SelectedItem.Text) Then
        Dim m As New Label
        m.Text = "duplicate !"
        Me.form1.Controls.Add(m)
        Exit Sub

    End If
    If TextBox2.Text = "" Then
        TextBox2.Text = DropDownList1.SelectedItem.Text
    Else
        TextBox2.Text = TextBox2.Text + " , " + DropDownList1.SelectedItem.Text
    End If
End Sub

我觉得这个逻辑很正确。我唯一会做的不同的事情是:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
   If TextBox2.Text.Contains(DropDownList1.SelectedItem.Text) Then
     Dim m As New Label
     m.Text = "duplicate !"
     Me.form1.Controls.Add(m)
   Else If TextBox2.Text = "" Then
     TextBox2.Text = DropDownList1.SelectedItem.Text
   Else
     TextBox2.Text = TextBox2.Text + " , " + DropDownList1.SelectedItem.Text
   End If
End Sub
我会进行构造并
If..else If..else
,避免过早返回。为清晰起见,最好只使用一个出口点对功能进行编程。在这种情况下,没有理由不这样做