Combobox force Validation when no value is Selected

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 5714 Views - Last Post: 01 December 2012 - 11:24 AM Rate Topic: -----

#1 xrndMember  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 12

Combobox force Validation when no value is Selected

Posted 01 December 2012 - 07:34 AM

Hello,

I have a combo box in WPF project page. I have several items in it and one of the items is '(None)'.
Since my combo box is a compulsory field I would like to force validation on the combo box and wish to throw new Validation error if '(None)' is selected which gives red border around the combo box, just like on text box validation.

Your guidance will be of great help as I am very new to WPF.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Combobox force Validation when no value is Selected

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 07:52 AM

Well you've outlined your problem very well. That's the first step.
But what have you coded in a best effort to solve this? That's a requirement: We don't just give you the finished code.

Since the combobox raises an event of .SelectedItemChanged - that would be a logical place to check if the .SelectedItem is (None)
Was This Post Helpful? 0
  • +
  • -

#3 xrndMember  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 12

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 07:56 AM

Hello, thanks for your reply.
I have taken some steps.
i am trying to force an error when Selected item is (None).
In my function I have written following snippet

Validationerror validationerror = new Validationerror(new ComboBoxError(),
     mytext.GetBindingExpression(ComboBoxItem.ContentProperty), "Field Empty",null);

Validation.MarkInvalid(mytext.GetBindingExpression(ComboBoxItem.ContentProperty), validationerror);


which doesnt work as desired.

This post has been edited by tlhIn`toq: 01 December 2012 - 08:24 AM
Reason for edit:: code tags

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 08:26 AM

'as desired' - what *does* it do

This is such a small amount of code and totally out of context for the rest of the method it is in. When does this execute? What method? maybe just show us the entire method.
Was This Post Helpful? 0
  • +
  • -

#5 xrndMember  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 12

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 08:30 AM

It should ideally make combobox border red..
Just like textbox shows when validation error is thrown..
shown in Image attached.
but the code written throws runtime error called
"Value cannot be null.
Parameter name: bindingInError".
I have bound Combobox items to my class variable,which appears correct to me.
I suppose error lies in the code which throws error.

Attached image(s)

  • Attached Image

Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 08:36 AM

View PostxrndMember, on 01 December 2012 - 09:30 AM, said:

I suppose error lies in the code which throws error.


Yep
Was This Post Helpful? 0
  • +
  • -

#7 xrndMember  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 12

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 08:57 AM

hi
Any idea how to fix this?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 09:01 AM

I'm quite sure tlhIn`toq knows. The question, though, is what have you tried?

This post has been edited by Skydiver: 01 December 2012 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#9 xrndMember  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 12

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 09:44 AM

well then that answer will be disappointing to you.
I have just tried this code and the code for textbox worked abs fine.
the function is this.
private void errorCheckTextBox(System.Windows.Controls.Control controlName)
        {
            System.Windows.Controls.TextBox mytext = controlName as System.Windows.Controls.TextBox;
            if (String.IsNullOrEmpty((mytext.Text)) == true)
            {
                Validationerror validationerror = new Validationerror(new NumberValidator(),
                                                  controlName.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty), "Field Empty", null);
                Validation.MarkInvalid(controlName.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty), validationerror);
                ErrorExists = true;
            }
        }


but for combobox i hardly found things.and m very new to wpf. this is my 1st app in WPF. so not so well versed.

well the xaml for the target control is
<TextBox x:Name="txtDescription" Height="75"  HorizontalAlignment="Left" Margin="6,6,0,0"  VerticalAlignment="Top"
                 Width="223" Grid.Column="1" Grid.Row="3" Grid.RowSpan="2" HorizontalScrollBarVisibility="Auto"
                 VerticalScrollBarVisibility="Auto" TextChanged="txtDescription_TextChanged">
            <Binding Path="ModelName"  UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                <Binding.ValidationRules>
                    <ExceptionValidationRule />
                </Binding.ValidationRules>
            </Binding>
        </TextBox>

This post has been edited by tlhIn`toq: 01 December 2012 - 10:22 AM
Reason for edit:: Code tags added

Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 10:22 AM

Please use code tags if you want us to continue to deal with this thread. Everyone else does it, and we mention the need for it everywhere.

:code:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#11 xrndMember  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 12

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 10:24 AM

Surely tlhIn`toq.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 10:27 AM

Binding errors are real pain in the arse.
I've posted my own share of questions regarding them looking for help myself.
http://www.dreaminco...1&#entry1643927

Frankly its one of those things that I can't just look at a snippet of code and go "oh, there it is." If you wanted to shoot me a PM with the entire solution zipped up I'd take a look. But I'd be doing the same thing you should: Experiment like hell. Binding is very picky and sometimes it seems like you just have to try 10 different things to find exactly what sticks.
Was This Post Helpful? 0
  • +
  • -

#13 xrndMember  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 12

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 10:29 AM

Yeah,
I agree wid u on dat.
I surely will nd will post back the solution as soon as i get..
thanks dear.
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 10:36 AM

View PostxrndMember, on 01 December 2012 - 11:29 AM, said:

Yeah,
I agree wid u on dat.
I surely will nd will post back the solution as soon as i get..
thanks dear.


I no uncertain terms: I am not going to continue working on this thread of you choose to keep typing all SMS and ghetto wannabe.
Read tip #7 for further explanation as to why.

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, sum1, please and so on like this guy:

    Spoiler

Was This Post Helpful? 0
  • +
  • -

#15 xrndMember  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 12

Re: Combobox force Validation when no value is Selected

Posted 01 December 2012 - 11:13 AM

well,

Not dat intension..but sorry if it got so..
Was This Post Helpful? -1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2