Wrox Programmer Forums
|
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
 
Old June 12th, 2005, 10:07 AM
Authorized User
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old June 13th, 2005, 09:36 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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.

 
Old June 13th, 2005, 08:15 PM
Authorized User
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)

 
Old June 14th, 2005, 01:34 PM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old June 14th, 2005, 05:57 PM
Authorized User
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old June 14th, 2005, 06:16 PM
Authorized User
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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







Similar Threads
Thread Thread Starter Forum Replies Last Post
add drop down / combo box for selection mamuco Classic ASP Components 0 May 30th, 2007 06:34 PM
Query based on combo box selection help Elain Access 1 January 3rd, 2006 11:33 PM
Form's combo text box selection drives pull of a s pkaptein1 Access 3 May 5th, 2005 06:24 AM
Combo box doesnot accept the selection shoakat Classic ASP Databases 2 August 13th, 2004 06:19 PM
Populate List Box by Combo Box Selection mmcdonal Access 2 June 15th, 2004 12:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.