Problem with simple ComboBox

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 27
Reputation: edgar5 is an unknown quantity at this point 
Solved Threads: 2
edgar5 edgar5 is offline Offline
Light Poster

Problem with simple ComboBox

 
0
  #1
Jan 4th, 2009
With this code:
  1. Imports System.Text
  2.  
  3. Public Class Form1
  4. Public quoteStr As String = Global.Microsoft.VisualBasic.ChrW(34)
  5. Public illegalChar As Boolean = False
  6. Public asteriskReplacement As New StringBuilder(" ", 4)
  7. Public asteriskComboIndex As Integer = 0
  8.  
  9. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  10. Dim keyboardCharacters As String() = {" single space", "- hyphen", _
  11. " - <single space>hyphen<single space>", "` open single quote",_
  12. "' close single quote", "_ underscore", "=", "~", "!", "@", "#", "$",_
  13. "%", "^", "&", "(", ")", "+", "[", "{", "}", ";", quoteStr, ",", ".", _
  14. "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d",_
  15. "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",_
  16. "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",_
  17. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",_
  18. "U", "V", "W", "X", "Y", "Z"} ComboBox1.Items.AddRange(keyboardCharacters)
  19. ComboBox1.SelectedIndex = asteriskComboIndex
  20. End Sub
  21.  
  22. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)_
  23. Handles ComboBox1.SelectedIndexChanged
  24. Dim indexS As String = ComboBox1.SelectedIndex
  25. MsgBox("SelectedIndex _" + indexS + "_")
  26. Dim index As Integer = ComboBox1.SelectedIndex
  27. asteriskComboIndex = index
  28. WhichChar(ComboBox1, asteriskReplacement, index)
  29. MsgBox("SelectedIndexChanged SelectedText _" + ComboBox1.SelectedText + "_")
  30. End Sub
  31.  
  32. Public Sub WhichChar(ByVal CBox As ComboBox, ByRef retVal As StringBuilder, ByVal index As Integer)
  33. MsgBox("WhichChar CBox.SelectedText _" + CBox.SelectedText + "_")
  34. Dim selectedItem As New Object
  35. selectedItem = CBox.SelectedItem
  36. ClearStrBuilder(retVal)
  37. MsgBox("timing")
  38. Select Case index
  39. Case 0 'single space
  40. retVal.Append(" ")
  41. Case 1 'hyphen
  42. retVal.Append("-")
  43. Case 2 'single space>hyphen<single space>
  44. retVal.Append(" - ")
  45. Case 3 'open single quote
  46. retVal.Append("`")
  47. Case 4 ' close single quote
  48. retVal.Append("'")
  49. Case 5 'underscore
  50. retVal.Append("_")
  51. Case Else 'everything else
  52. retVal.Append(CBox.SelectedText)
  53. End Select
  54. End Sub
  55.  
  56. Private Sub ClearStrBuilder(ByRef str As StringBuilder)
  57. Dim holdStr As String = str.ToString()
  58. str = str.Remove(0, holdStr.Length)
  59. str.Length = 0
  60. End Sub
  61.  
  62. Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)_
  63. Handles MyBase.FormClosed
  64. MsgBox("Form1_FormClosed asteriskReplacement _" + asteriskReplacement.ToString() + "_")
  65. End Sub
  66.  
  67. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  68. MsgBox("Button1_Click asteriskReplacement _" + asteriskReplacement.ToString() + "_")
  69. MsgBox("Button1_Click ComboBox1.SelectedText _" + ComboBox1.SelectedText + "_")
  70. End Sub
  71.  
  72. Private Sub ComboBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave
  73. Dim indexS As String = ComboBox1.SelectedIndex
  74. MsgBox("ComboBox1 SelectedIndex _" + indexS + "_")
  75. Dim index As Integer = ComboBox1.SelectedIndex
  76. asteriskComboIndex = index
  77. WhichChar(ComboBox1, asteriskReplacement, index)
  78. End Sub
  79. End Class
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!
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Problem with simple ComboBox

 
0
  #2
Jan 4th, 2009
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
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 27
Reputation: edgar5 is an unknown quantity at this point 
Solved Threads: 2
edgar5 edgar5 is offline Offline
Light Poster

Re: Problem with simple ComboBox

 
0
  #3
Jan 4th, 2009
Originally Posted by ddanbe View Post
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
Not quite what I am looking for. The "a" is already on the list of items, I do not want to add it. Think of the ComboBox in a web shopping cart--you are asked to fill in your credit card info...there are two comboBoxes to enter Expiration date--month and year. The month box has "1 (Jan)", "2 (Feb)", "3 (Mar)...etc. as possible items to select (I see this in lots of non-web Windows apps as well.)

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Problem with simple ComboBox

 
0
  #4
Jan 4th, 2009
I think you need to check out the DropDownStyle property.
Set it to DropDownList.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Problem with simple ComboBox

 
0
  #5
Jan 4th, 2009
The SelectedIndexchanged() event only fires when you actually click an item to "change" the contents of the combobox.... perhaps "click" or "keypress" would be the right choice?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC