|
Thanks Gerry,
I'd actually stumbled upon a similar but shorter solution and was working on that before I read your comment.
void textBoxSample_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = Char.IsPunctuation(e.KeyChar) ||
Char.IsSeparator(e.KeyChar) ||
Char.IsSymbol(e.KeyChar);
}
This works really well when I want to stop any non whole numerical value, and I still use this in conjunction with the Text_Changed event to check for correct range etc.
Where this goes wrong is when I need to allow a "." i.e. 7.2 as the Char.IsPunctuation blocks the "."
But if I remove Char.IsPunctuation it allows characters like \ / ? which crashes my app !
How can I get round this either by modifying what I have or using your suggested method or other ?
Here's what I have:
private void nicstrength_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = Char.IsSeparator(e.KeyChar) || Char.IsSymbol(e.KeyChar);
}
private void nicstrength_TextChanged(object sender, EventArgs e)
{
double ns;
if (string.IsNullOrEmpty(nicstrength.Text))
;
else
{
if ((nicstrength.Text.Any(chr => char.IsLetter(chr))) || (nicstrength.Text.StartsWith(".")))
{
MessageBox.Show("Please enter a number between 1 and 10 inc decimal points i.e 7.2");
nicstrength.Clear();
nicstrength.Focus();
}
else
{
ns = Convert.ToDouble(nicstrength.Text);
if (!(ns >= 1 && ns <= 10))
{
MessageBox.Show("Please enter a number between 1 and 10 inc decimal points i.e 7.2");
nicstrength.Clear();
nicstrength.Focus();
}
}
}
}
Many thanks
Damian
|
|
|
|
|
OK fixed it ! probably viewed as clunky, but it works !
Created this:
private readonly char[] notallowed = { '!', '"', '#', '%', '&', '\'', '(', ')', '*', ',', '-', '/', ':', ';', '?', '@', '[', '\\', ']', '_', '{', '}', '+' };
Then in the functions where I need to allow the use of a "." but no other non numerical characters i've added this:
if ((nicstrength.Text.Any(chr => char.IsLetter(chr))) || (nicstrength.Text.StartsWith(".")) || (nicstrength.Text.IndexOfAny(notallowed) != -1))
I have now tested every single textbox, and none will allow any incorrect entries .. yay.
I think my first C# app is done and ready to meet the world !
Many thanks for the help thus far, I'm sure i'll need plenty more
Cheers
Damian
|
|
|
|
|
Myself, I found keyboard handling easier when I only "allowed" certain characters; versus trying to find what to "exclude".
E.g. for most "numeric" textboxes, I simply just "allow" digits and possibly a decimal; with a few navigation keys thrown in.
It's easier to "fix" a key that is "not working" (i.e. include it), than to stop all the "others", individually, from not working (IMO).
(So, default to "handled" for all except the "good" keys).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi everybody,
I would like to address at run time datagrid cells individually and change their content depending on some conditions.
Could someone give me some directions on this topic?
Best regards,
RV
|
|
|
|
|
|
Thank you for your reply,
Is not this approach somehow more suitable for WinForm projects?
I'm actually developing my application in the WPF c# environment.
Best regards,
RV
|
|
|
|
|
If you're doing this using WPF, you really should use MVVM. Then you would manipulate the data via the ViewModel.
This space for rent
|
|
|
|
|
Thank Pete you for your advice. I will try to find out how to proceed using MVVM.
|
|
|
|
|
Member 13511312 wrote: I'm actually developing my application in the WPF c# environment. And we are supposed to guess that?
|
|
|
|
|
Sorry for having forgotten to mention this detail.
|
|
|
|
|
Is there any difference between:
DataHelpers dh = new DataHelpers {};
and
DataHelpers dh = new DataHelpers ();
|
|
|
|
|
Not as written; both redundant in this case.
Otherwise, {...} is used to initialize fields / properties; whereas (...) might be used to pass parameters to a constructor.
Other options: ... (...){...}
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Thanks for the reply. If both are redundant, why am I getting a red line in Visual Studio?
|
|
|
|
|
You post "fragments" and expect comprehensive answers ...
I suspect you spent 0 time looking up either topic.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
That kind of comment makes people who are just starting out, not want to ask questions. Maybe if you're feedback is neither helpful nor constructive, you might consider not being so "helpful."
|
|
|
|
|
Hi Rick, your profile here on CP suggests you are not "just starting out."
Nothing "wrong" with that, but I do wonder if you are taking the time to do some basic research before posting. CodeProject is "bursting at the seams" with relevant content on almost any topic ... ditto StackOverflow.
cheers, Bill
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
Thanks Bill. I'm sure Gerry appreciates your coming to his rescue, but the answer you gave below is exactly the opposite of the answer that that Gary gave. I only mention this because you both have taken so much time out of your day to criticize my question. Should I check with you two before posting next time?
modified 27-Dec-17 0:31am.
|
|
|
|
|
Rick_Bishop wrote: I'm sure Gerry appreciates your coming to his rescue My message was only for you ... I gave up "rescues" when I stopped being a social worker at age 42 (32 years ago) cheers, Bill
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
I'm a Paramedic/Firefighter for a total of 14 years for emergency services all over the U.S. Hats off to you for being a social worker! What a thankless and difficult job at times. I'll bet you've enjoyed a better quality of life since changing occupations. I know I have. Thanks for help!
|
|
|
|
|
I tell eveyone the same thing when I make an effort, and they don't.
Don't feel that I singled you out; but I will make an effort to avoid you in the future.
Cheers.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
It was admittedly a poorly worded question. Next time I'll try to convey the effort put forth to obtain an answer. But please don't answer any question that seems stupid. It's hard enough for me to decipher what's outdated information on the Internet, let alone trying to guess what you'll think is a stupid question.
|
|
|
|
|
There are no "stupid" questions.
I provide opportunities for a "dialog".
It's up to you what happens next in how you choose to "participate".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
If you have used the version with {}, Visual Studio will be complaining that you have supplied an object initialiser, but haven't actually tried to initialise any properties inside it. In other words, the {} is redundant.
This space for rent
|
|
|
|
|
Yes, there is a significant difference.
Look at what Object () and Collection {} initializers are, and what they do: [^].
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
I was not aware that one is called an Object Initializer and the other is called a Collection initializer. Thank you for pointing that out. I will research that.
What prompted me to ask this question is that my code worked either way.
|
|
|
|