| | |
Problem with simple ComboBox
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Dec 2008
Posts: 27
Reputation:
Solved Threads: 2
With this code:
Create a default Windows Form Project with a default ComboBox and default Button in the default Form. Double-click the Form to open the code window, replace everything there with the above code. Run the code in the debugger – menu item Debug/Start Debugging . Close the spurious MsgBox.
Left click once in the ComboBox, press the <a> key (any valid file name character will do); left click the button. Note the two MsgBox messages report that no item was selected in the ComboBox (button click) and that nothing was entered into the asteriskReplacement StringBuilder (you may uncomment the “Timing” MsgBox in WhichChar() to verify that it is not entered). Note that in each message string I have wrapped the variable with a leading and trailing underscore so that null string variables look like __ <two underscores together> and a string containing a single space looks like _ _ <underscore><space><underscore>.
The problem is that when I enter a value via the keyboard (even adding a carriage return) the ComboBox does not enter the _SelectedIndexChanged() function. It does enter the _Leave() function (when I click the button), but note that by the time _Leave() is entered the .SelectedIndex value is no longer valid.
I have spent many hours trying to figure this out; it is obviously a very basic concept and everyone else uses this gadget with ease. I have no idea where I am going wrong. Any help would be greatly appreciated!
VB.NET Syntax (Toggle Plain Text)
Imports System.Text Public Class Form1 Public quoteStr As String = Global.Microsoft.VisualBasic.ChrW(34) Public illegalChar As Boolean = False Public asteriskReplacement As New StringBuilder(" ", 4) Public asteriskComboIndex As Integer = 0 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim keyboardCharacters As String() = {" single space", "- hyphen", _ " - <single space>hyphen<single space>", "` open single quote",_ "' close single quote", "_ underscore", "=", "~", "!", "@", "#", "$",_ "%", "^", "&", "(", ")", "+", "[", "{", "}", ";", quoteStr, ",", ".", _ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d",_ "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",_ "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",_ "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",_ "U", "V", "W", "X", "Y", "Z"} ComboBox1.Items.AddRange(keyboardCharacters) ComboBox1.SelectedIndex = asteriskComboIndex End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles ComboBox1.SelectedIndexChanged Dim indexS As String = ComboBox1.SelectedIndex MsgBox("SelectedIndex _" + indexS + "_") Dim index As Integer = ComboBox1.SelectedIndex asteriskComboIndex = index WhichChar(ComboBox1, asteriskReplacement, index) MsgBox("SelectedIndexChanged SelectedText _" + ComboBox1.SelectedText + "_") End Sub Public Sub WhichChar(ByVal CBox As ComboBox, ByRef retVal As StringBuilder, ByVal index As Integer) MsgBox("WhichChar CBox.SelectedText _" + CBox.SelectedText + "_") Dim selectedItem As New Object selectedItem = CBox.SelectedItem ClearStrBuilder(retVal) MsgBox("timing") Select Case index Case 0 'single space retVal.Append(" ") Case 1 'hyphen retVal.Append("-") Case 2 'single space>hyphen<single space> retVal.Append(" - ") Case 3 'open single quote retVal.Append("`") Case 4 ' close single quote retVal.Append("'") Case 5 'underscore retVal.Append("_") Case Else 'everything else retVal.Append(CBox.SelectedText) End Select End Sub Private Sub ClearStrBuilder(ByRef str As StringBuilder) Dim holdStr As String = str.ToString() str = str.Remove(0, holdStr.Length) str.Length = 0 End Sub Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)_ Handles MyBase.FormClosed MsgBox("Form1_FormClosed asteriskReplacement _" + asteriskReplacement.ToString() + "_") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox("Button1_Click asteriskReplacement _" + asteriskReplacement.ToString() + "_") MsgBox("Button1_Click ComboBox1.SelectedText _" + ComboBox1.SelectedText + "_") End Sub Private Sub ComboBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave Dim indexS As String = ComboBox1.SelectedIndex MsgBox("ComboBox1 SelectedIndex _" + indexS + "_") Dim index As Integer = ComboBox1.SelectedIndex asteriskComboIndex = index WhichChar(ComboBox1, asteriskReplacement, index) End Sub End Class
Left click once in the ComboBox, press the <a> key (any valid file name character will do); left click the button. Note the two MsgBox messages report that no item was selected in the ComboBox (button click) and that nothing was entered into the asteriskReplacement StringBuilder (you may uncomment the “Timing” MsgBox in WhichChar() to verify that it is not entered). Note that in each message string I have wrapped the variable with a leading and trailing underscore so that null string variables look like __ <two underscores together> and a string containing a single space looks like _ _ <underscore><space><underscore>.
The problem is that when I enter a value via the keyboard (even adding a carriage return) the ComboBox does not enter the _SelectedIndexChanged() function. It does enter the _Leave() function (when I click the button), but note that by the time _Leave() is entered the .SelectedIndex value is no longer valid.
I have spent many hours trying to figure this out; it is obviously a very basic concept and everyone else uses this gadget with ease. I have no idea where I am going wrong. Any help would be greatly appreciated!
Last edited by edgar5; Jan 4th, 2009 at 4:40 am.
-Ed
MS Vista Ultimate 64-bit; Visual Studio 2008 & 2010 beta; Visual Basic; Visual C++
Asus P5E3 Deluxe w/ Intel E6850, 8 GB OCZ DDR3 PC3-12800, SAPPHIRE HD3870 GPU
MS Vista Ultimate 64-bit; Visual Studio 2008 & 2010 beta; Visual Basic; Visual C++
Asus P5E3 Deluxe w/ Intel E6850, 8 GB OCZ DDR3 PC3-12800, SAPPHIRE HD3870 GPU
A ComboBox, as it's name implies is a combination of a TextBox and a ListControl.
If you type an 'a' into the "TextBox" you will see that the ComboBox.Text property contains an 'a'. To put that 'a' into the dropdownlist you have to do a ComboBox1.Items.Add or .Insert
If you type an 'a' into the "TextBox" you will see that the ComboBox.Text property contains an 'a'. To put that 'a' into the dropdownlist you have to do a ComboBox1.Items.Add or .Insert
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Dec 2008
Posts: 27
Reputation:
Solved Threads: 2
•
•
•
•
A ComboBox, as it's name implies is a combination of a TextBox and a ListControl.
If you type an 'a' into the "TextBox" you will see that the ComboBox.Text property contains an 'a'. To put that 'a' into the dropdownlist you have to do a ComboBox1.Items.Add or .Insert
What other tool would do this job?
-Ed
MS Vista Ultimate 64-bit; Visual Studio 2008 & 2010 beta; Visual Basic; Visual C++
Asus P5E3 Deluxe w/ Intel E6850, 8 GB OCZ DDR3 PC3-12800, SAPPHIRE HD3870 GPU
MS Vista Ultimate 64-bit; Visual Studio 2008 & 2010 beta; Visual Basic; Visual C++
Asus P5E3 Deluxe w/ Intel E6850, 8 GB OCZ DDR3 PC3-12800, SAPPHIRE HD3870 GPU
![]() |
Similar Threads
- Combo Box not populating (VB.NET)
- Manually or autoscanning DVB-T Channels using DirectshowLib-2005? (C#)
- How do I create a login form to open the next appropriate form? (C#)
- Oracle Help Vb.net application (VB.NET)
- combobox with mutiple columns (C#)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- problem with Simple style combobox (C#)
- Location Issue With Controls (VB.NET)
- Find Record (Visual Basic 4 / 5 / 6)
- combobox - remember last selection (VB.NET)
Other Threads in the VB.NET Forum
- Previous Thread: Data Adapter Update Command
- Next Thread: how to loop this code?
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account add application array basic beginner browser button buttons center check click code combo cpu crystalreport cuesent database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel exists fade file-dialog filter forms ftp generatetags html images input insert intel listview mobile module monitor mysql net number open output panel passingparameters picturebox picturebox2 port print printing printpreview problem regex reuse right-to-left searchvb.net select settings shutdown socket sqldatbase sqlserver storedprocedure survey tcp temperature textbox timespan transparency trim txttoxmlconverter user usercontol vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet vista visual visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode xml year






