943,816 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3589
  • VB.NET RSS
Jan 4th, 2009
0

Problem with simple ComboBox

Expand Post »
With this code:
VB.NET Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 18
Solved Threads: 2
Light Poster
edgar5 is offline Offline
30 posts
since Dec 2008
Jan 4th, 2009
0

Re: Problem with simple ComboBox

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
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Jan 4th, 2009
0

Re: Problem with simple ComboBox

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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?
Reputation Points: 18
Solved Threads: 2
Light Poster
edgar5 is offline Offline
30 posts
since Dec 2008
Jan 4th, 2009
0

Re: Problem with simple ComboBox

I think you need to check out the DropDownStyle property.
Set it to DropDownList.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Jan 4th, 2009
0

Re: Problem with simple ComboBox

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?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Data Adapter Update Command
Next Thread in VB.NET Forum Timeline: how to loop this code?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC