开发者

Select multiple items from DropDownList into TextBox, No duplicates

i used the following technique to Select multiple items f开发者_开发知识库rom DropDownList into TextBox with No duplicates, however i dont think it is the most proper way, any ideas.

Select multiple items from DropDownList into TextBox, No duplicates

pressing again with same value selected

Select multiple items from DropDownList into TextBox, No duplicates

choose another value from DDL and press button

Select multiple items from DropDownList into TextBox, No duplicates

here is my code

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


The logic looks pretty correct to me. The only thing I'd do different something like:

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

I'd make the construct and If..else if...else and avoid the premature return. It is prefered to program functions with only one exit point, for clarity. There's no reason why not to in this case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜