Combo box entry

I placed a combobox on my windows form to present a list of strings that can be selected, however, I would like the user to have the ability to enter any string they may choose.

The attached code takes whatever is chosen and prints it in a messagebox. It works great except for the -1 index. How can I accept a string that is input?

PublicClass Form1

Inherits System.Windows.Forms.Form

Dim strNucleotideAsString

PrivateSub ComboBox1_SelectedIndexChanged(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles ComboBox1.SelectedIndexChanged

Dim iAsInteger = ComboBox1.SelectedIndex

If i = -1Then

strNucleotide = ComboBox1.Text

ElseIf i = 0Then

strNucleotide = "TAGTAGATAC"

ElseIf i = 1Then

strNucleotide = "GCGCATAGGG"

EndIf

EndSub

PrivateSub btnCalculate_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnCalculate.Click

MessageBox.Show(strNucleotide, "Selected Nucleotide", MessageBoxButtons.AbortRetryIgnore)

EndSub

EndClass

Any help would be appreciated!

[2706 byte] By [LittleShell] at [2008-2-7]
# 1

The problem is using the SelectedIndexChanged event. If they type something, the selected index doesn't change. How about usung LostFocus, and then the Text property of the ComboBox?

Private Sub ComboBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.LostFocus

strNucleotide = ComboBox1.Text

End Sub

S

BillSempf at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

This example uses objects in the combobox as the items can be more than just text - Form load sets up the items - notice the id doesn't have to be sequential as Item 'Spotty has an ID of 99'

The Button determines if the selected item as one of the predefined items and displays its ID and text

ELSE

It was a free typed text item and it will display the text from the combobox

It may be a little overkill but shows the principle.
Public
Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Me.ComboBox1.Items.Clear()

'//This adds user Defined Objects call ListboxItem

Dim i1 As New ListBoxItem(1, "Fred")
Dim i2 As New ListBoxItem(2, "Bill")
Dim i3 As New ListBoxItem(3, "Bob")
Dim i4 As New ListBoxItem(99, "Spotty")

Me.ComboBox1.Items.Add(i1)
Me.ComboBox1.Items.Add(i2)
Me.ComboBox1.Items.Add(i3)
Me.ComboBox1.Items.Add(i4)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'//As this is a combo the item may be a selected item in the combo or
'//freetyped text. So we check the Type if the selected item to see if the item
'// is of the ListboxItem we added for fixed items in the list - if it is we use the
'//selecteditem .text property otherwise it is a freetyped item and we simply get the
'//text from the combo box

If TypeOf (Me.ComboBox1.SelectedItem) Is ListBoxItem Then

MessageBox.Show("Item " & CType(CType(Me.ComboBox1.SelectedItem, ListBoxItem).ID, String) & " " & CType(Me.ComboBox1.SelectedItem, ListBoxItem).Text, "Selected Nucleotide")
Else
MessageBox.Show(Me.ComboBox1.Text, "Free Text Selected Nucleotide", MessageBoxButtons.AbortRetryIgnore)
End If

End Sub

End Class

Public Class ListBoxItem

'//Defines a Text/Value pair Object which can be used for listbox/combobox etc.
'//like the old itemdata in VB Classic

Private intID As Integer
Private strValue As String

Public Sub New(ByVal ID As Integer, ByVal Text As String)
Me.ID = ID
Me.Text = Text
End Sub

Public Overrides Function Tostring() As String
Return strValue
End Function

Public Property ID() As Integer
Get
Return intID
End Get
Set(ByVal value As Integer)
intID = value
End Set

End Property

Public Property Text() As String
Get
Return strValue
End Get
Set(ByVal value As String)
strValue = value
End Set

End Property

End Class

spotty at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...