 |
| VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 12th, 2005, 10:07 AM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Combo Box -1 Selection
I am trying to create a combo box that will allow a user to select from a string in the dropdown list, however, I would also like the user to be able to enter a string of their own in the default blank index of the combobox. The code below works for the strings in the dropdown list, but it does not work for the user input.
How can I get this combobox to accept a string input by the user?
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strNucleotide As String
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim i As Integer = ComboBox1.SelectedIndex
If i = -1 Then
strNucleotide = ComboBox1.SelectedIndex
ElseIf i = 0 Then
strNucleotide = "TATATTAT"
ElseIf i = 1 Then
strNucleotide = "GAGAGAGAGA"
End If
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
MessageBox.Show(strNucleotide, "Selected Nucleotide", MessageBoxButtons.AbortRetryIgnore)
End Sub
End Class
|
|

June 13th, 2005, 09:36 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Set the combobox DropDownStyle to 'DropDown'
If the user types their own text, the SelectedIndexChanged event will not fire.
What you will have to do is either check the Leave or the TextChanged event.
* Within whichever event you choose, check the SelectedIndex property.
* If this is -1, you know you've got some user-specified text.
* Get the text by looking at the Text property.
* Remember that if you use the TextChanged event, the user may not have finished typing.
|
|

June 13th, 2005, 08:15 PM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you Jaucourt,
Tell me if you agree with the change that I have made. The reason I ask is that since I am new to programming I always question whether I am writing bad code.
The highlighted code below represents the code that I have added and it does allow me to access any string the user may input. Have I just short-circuited the the IF/Then decision above in the combo box?
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
If ComboBox1.SelectedIndex = -1 Then
strNucleotide = ComboBox1.Text
End If
MessageBox.Show(strNucleotide, "Selected Nucleotide", MessageBoxButtons.AbortRetryIgnore)
|
|

June 14th, 2005, 01:34 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I think if you modify your code a little, you may get the effect you are looking for. One question though... When you show the drop-down list, are you actually showing the "TATATTAT" and "GAGAGAGAGA" or are you displaying something else on the screen for the user? I ask because instead of setting strNucleotide to static text within your code, you can just set it to whatever the actual text display from the combo box is...
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strNucleotide As String
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs), _
Handles ComboBox1.SelectedIndexChanged
strNucleotide = CombBox1.Items(ComboBox1.SelectedIndex)
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs), _
Handles btnCalculate.Click
If ComboBox1.SelectedIndex = -1 Then
strNucleotide = ComboBox1.Text
End If
MessageBox.Show(strNucleotide, "Selected Nucleotide", MessageBoxButtons.AbortRetryIgnore)
End Sub
End Class
Also, if you've got a button that's displaying the value, why bother working off the combobox update anyways?
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strNucleotide As String
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs), _
Handles btnCalculate.Click
strNucleotide = ComboBox1.Text
MessageBox.Show(strNucleotide, "Selected Nucleotide", MessageBoxButtons.AbortRetryIgnore)
End Sub
End Class
If you need the button and you need to determine the value of strNucleotide from within the program try, and I highly suggest SELECT CASE, as it will allow you more entries while keeping the code block looking clean:
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strNucleotide As String
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles, _
btnCalculate.Click
Select Case ComboBox1.SelectedIndex
Case -1
strNucleotide = ComboBox1.Text
Case 0
strNucleotide = "TATATTAT"
Case 1
strNucleotide = "GAGAGAGAGA"
End Select
MessageBox.Show(strNucleotide, "Selected Nucleotide", MessageBoxButtons.AbortRetryIgnore)
End Sub
End Class
|
|

June 14th, 2005, 05:57 PM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Cweb,
Thanks for your insightful review. When I show the drop down list a list of genes is presented, within the code I have the actual nucleic acid sequence for each gene in order to do some simple calculations such as molecular weight, melting temperature etc...
So I do need the Select Case statement as you wisely presented and I trigger the start of the calculations through the button.
Another Question?
Situation
I have written some code that limits the user input to only four letters A, C, G, T (The four nucleic bases), that is should they choose to input a nucleic acid sequence rather than pick a gene from the list. I used a regular expression routine to accomplish this.
Question:
Where would I place the code to conduct the string manipulations? The reason I ask is that to some extent I will have to make calculations for any string that would be presented in the drop down list as well. Would I place the same method call within each select case position and place the method outside the select case structure? The thing that is troubling me is that for nucleic acid sequences selected from the drop-down list I would not have to parse through and remove all inappropriate characters, I could just go straight to the calculations, whereas for the sequences input I would have to first parse and then go to the same calculations. Two different methods? One for input sequences and one for selected?
|
|

June 14th, 2005, 06:16 PM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here's my code at this point:
Imports System.Text.RegularExpressions
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strNucleotide As String
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim i As Integer = ComboBox1.SelectedIndex
Select Case ComboBox1.SelectedIndex
Case -1
strNucleotide = ComboBox1.SelectedText
Case 0
strNucleotide = "TAGTAGATAC"
Case 1
strNucleotide = "GCGCATAGGG"
End Select
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
If ComboBox1.SelectedIndex = -1 Then
strNucleotide = ComboBox1.Text
Dim output, newString As String
Dim myMatch As Match
'create regular expression
Dim expression As Regex = New Regex("[ACGT]")
newString = StrConv(strNucleotide, VbStrConv.UpperCase)
'match regular expression to string and
'print out all matches
For Each myMatch In expression.Matches(newString)
output &= myMatch.ToString()
Next
MessageBox.Show(output, "Using Class Regex", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class
|
|
 |