Focus Stuck on TextBox

J

jehugaleahsa

Hello:

When we have a text box that is bound to an int?, the text box will
not allow the user to leave the control until it has a valid integer
in it. This is actually very bad. If the user wants to remove the
value, they can't anymore. They can't even leave the form! They are
being forced to enter 0 or some other bogus value.

The weird thing is that it doesn't take affect until they set a value
and lose focus on the control.

Is there some way of shutting this off? I'm assuming it is some sort
of funky validation.

Thanks,
Travis
 
C

Ciaran O''Donnell

Can you handle the Validating event of the text box, validate it yourself and
it its ok, set e.Cancel = true;

That should stop it being annoying.
 
L

Leo Seccia

Hello Travis,

you can't assign null to a "value" type such as int. They are not nullable
You need to use the nullable version of it, eg. int? xxx = null;

L
 
J

jehugaleahsa

Hello Travis,

you can't assign null to a "value" type such as int. They are not nullable
You need to use the nullable version of it, eg. int? xxx = null;

L


Please read more carefully.
 
J

jehugaleahsa

Can you handle the Validating event of the text box, validate it yourselfand
it its ok, set e.Cancel = true;

That should stop it being annoying.

Setting e.Cancel = true; actually causes the validation to fail - in
other words, keeps the control in focus yet again.

I would much rather see an error provider or error message than
prevent the user from tabbing away.

I mean, I could always set e.Cancel = false;, which would force
validation to succeed. I could pop up a MessageBox to indicate the
requirement.

I will try that.
 
J

jehugaleahsa

I actually found the answer to this question on MSDN.

For one, you can allow focus to change on failed validation if you set
the Form's AutoValidate property to EnableAllowFocusChange.

Secondly, I can optionally set the Binding instances to have an
DataSourceUpdateMode of OnPropertyChanged. I can then turn validation
on or off.

I can also handle the Validating event and report issues here. If I
set AutoValidate to EnableAllowFocusChange, I can correctly set
e.Cancel = true; when validation fails. I can also put an
ErrorProvider on the form and call SetError(myControl, "Error
Message") to display an exclamation with the message, or I could use a
MessageBox.

Finally, if you want to suppress validation until the user is ready to
save, you can set CausesValidation = false for each control. Then in
the save method you can call ValidateChildren on the form. It will
return true if all the controls are validated.

It is a very flexible system. I just wish there was someone who could
answer questions when someone like myself comes around.
 
P

Pavel Minaev

Hello:

When we have a text box that is bound to an int?, the text box will
not allow the user to leave the control until it has a valid integer
in it. This is actually very bad. If the user wants to remove the
value, they can't anymore. They can't even leave the form! They are
being forced to enter 0 or some other bogus value.

The weird thing is that it doesn't take affect until they set a value
and lose focus on the control.

Is there some way of shutting this off? I'm assuming it is some sort
of funky validation.

If you want to map empty string input by the user to null value in the
int?, just set the DataSourceNullValue property of the Binding to
"null", and NullValue property of the binding to empty string. You
can't do it from the form designer, so you'll have to write some code:

textBox1.DataBindings["Text"].DataSourceNullValue = null;
textBox1.DataBindings["Text"].NullValue = "";
 
J

jehugaleahsa

If you want to map empty string input by the user to null value in the
int?, just set the DataSourceNullValue property of the Binding to
"null", and NullValue property of the binding to empty string. You
can't do it from the form designer, so you'll have to write some code:

            textBox1.DataBindings["Text"].DataSourceNullValue= null;
            textBox1.DataBindings["Text"].NullValue = "";

Very cool.
 
J

jehugaleahsa

[...]
It is a very flexible system. I just wish there was someone who could
answer questions when someone like myself comes around.

Me too.  Sorry we weren't more helpful.  Please keep in mind though that  
a) this is a C# newsgroup, not a .NET newsgroup, and b) .NET is _huge_ and  
no one person knows everything about .NET.  For better or worse, I doubt  
that the sort of binding you're using is something most people do  
(ironically, the more complex database-related binding seems more  
typical), and so it's not too surprising to me that there's a dearth of  
expertise on all the ins-and-outs.

That said, you found the answer on MSDN, which is the place you should be 
looking to start with for answers to questions like these.  If nothing  
else, hopefully it's a good lesson on self-reliance.  :)  And now you're  
the expert on binding, so when someone _else_ comes asking related  
questions, _you_ can step up and answer them.

Pete

Sorry for the attitude Pete. It wasn't my intention. You help and
everyone else's is always appreciated.

Believe me, I do always look at MSDN first; I just have a really hard
time finding answers to some of my more stupid questions. This time I
found the answer after continuing to search long after my post. Some
times rewording the question makes Google's engine a little smarter. I
just wish Google wouldn't list 3000 commercial sites before listing a
single forum.

Thank you everyone!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top