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.
pressing again with same value selected
choose another value from DDL and press button
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.
精彩评论