Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Combo box input validation


Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2007, 01:31 AM
FragMagnet FragMagnet is offline
Newcomer
 
Join Date: Aug 2007
Posts: 9
Default Combo box input validation


As with most of the people who decide to join this forum, I'm rather new to VB. So far, most of my problems have been answered by a simple search, but this time around I wasn't so lucky, even after several attempts.

I'm using Visual Basic for Visual Studio 2005 at the moment, and basically what I'm trying to do is prevent my users from manually typing in a choice in a combo box that isn't in the selection I've given them. It's an unbound source, so it should be simple enough... I'm just having a hard time finding the right events/methods to use to do this in a neater manner than having to brute force it over the whole list. It feels too messy.

Edit: So far I've tried using the ComboBox.Validated() event, but somehow this seems counterproductive to me - I want the event to trigger when the contents are not valid, not when they're valid.

Apologies in advance if I put this in the wrong forum or if I didn't give the right info somehow... hopefully my first shot at asking for help wasn't a bad one.
__________________
http://quotedfortruth.net

Last edited by FragMagnet; 08-07-2007 at 01:36 AM. Reason: update in info
Reply With Quote
  #2  
Old 08-07-2007, 06:50 AM
LaVolpe's Avatar
LaVolpe LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default Welcome to the forum

Comboboxes have a few style properties. One of them only allows a user to select an item from the list: DropDown List. Now that is VB6, I am guessing .Net is similar. In the future you may wish to post .Net questions into that forum vs this one.

Using that style prevents any need for a Validate event because one can only select an item from the list, unless you are using Validate for some other reason. And when that style is set, the selection occurs on the Click event where the selected item can be obtained via the ListIndex property.
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
  #3  
Old 08-07-2007, 07:01 AM
FragMagnet FragMagnet is offline
Newcomer
 
Join Date: Aug 2007
Posts: 9
Default

That helps a lot, thank you.

Though if I may toss out one last question, I'm now having trouble with preventing blank data entry in a NumericUpDown element. Is there some option I need to enable to disallow a null value entry?
__________________
http://quotedfortruth.net
Reply With Quote
  #4  
Old 08-07-2007, 01:43 PM
LaVolpe's Avatar
LaVolpe LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default

I have no experience with that control (not in VB6). Again, you may want to post .Net related questions in the .Net forum. Here's Microsoft's info on that control should you want to review it.
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
  #5  
Old 08-08-2007, 01:27 AM
FragMagnet FragMagnet is offline
Newcomer
 
Join Date: Aug 2007
Posts: 9
Default

Bah. My apologies once again.

But in any case, thanks a lot for helping out even if I was in the wrong place.
__________________
http://quotedfortruth.net
Reply With Quote
  #6  
Old 08-08-2007, 08:43 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Preventing the NumericUpDown control from displaying a blank value actually seems to be kind of tricky, but a little investigation showed it isn't as bad as it seems.

The biggest hurdle to accomplishing this goal is the Text property doesn't appear in the property grid or Intellisense. This would seem to suggest that you can't directly get or set the text, which is a problem. When the control's text is blank, the internal value does not change, so it seemed there was no way to detect if the user input a blank value.

Indeed, the documentation seems to agree:
Quote:
The Text has no affect on the appearance of the NumericUpDown control; therefore, it is hidden in the designer and from IntelliSense.
Sometimes, though, it is good to follow hunches that the documentation doesn't know what it's talking about. Digging into the control's implementation proved interesting. Several methods exist to parse and validate the input text, and they were directly using the Text property. It was only when I looked at the declaration for the property that I understood what was going on:
Code:
<Browsable(False), Bindable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)> _
Public Overrides Property [Text] As String
The property is not inaccessible, simply hidden. Intellisense will not display the property, but using it provides the expected behavior. (So much for "having no effect"!)

This leaves you with some choices, but I think using the Leave event is the best. If you want to force focus back into the control you can, but in that case I suggest the Validating event. Here's an example of how you could get rid of blank entries:
Code:
    Private Sub numericUpDown1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numericUpDown1.Leave
        If String.IsNullOrEmpty(numericUpDown1.Text) Then
            numericUpDown1.Text = numericUpDown1.Value.ToString()
        End If
    End Sub
When the user leaves the control, this replaces a blank value with the control's current value.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 08-09-2007, 01:50 AM
FragMagnet FragMagnet is offline
Newcomer
 
Join Date: Aug 2007
Posts: 9
Default

That was immensely helpful. Thanks so much, you saved me from my self inflicted headaches of perfectionism.

Thanks so much. I needed to make a good impression on my boss after all.
__________________
http://quotedfortruth.net
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->