0

first of all I want to sorry for my English... I have an if:

if (imię.Text.Length > 2 && char.IsUpper(imię.Text,0) && char.IsLower(imię.Text,1)==true)
{
     PanelImie.BackgroundImage = Properties.Resources.Check_icon;
     imietest = true;
}
else
{
     PanelImie.BackgroundImage = Properties.Resources.Sign_Error_icon;
     imietest = false;
}
blokowanieOK();

And a form like on the picture:

In field "imię"(name) and "nazwisko"(surname) I set the first letter must be upper that's ok but I want the rest off letters be lower and checkicon will be true in this situation. At the moment I can write Uppercase ewerywhere and my program show me that's ok... How to change this if to First letter must be upper and rest of letters must be lower and then checkicon will be ok?

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • So are you saying when you write it (`imie`) all in uppercase the `if` condition is still satisfied? –  Sep 11 '14 at 16:51
  • Check this link: http://stackoverflow.com/questions/4135317/make-first-letter-of-a-string-upper-case – felixgondwe Sep 11 '14 at 16:52
  • I don't think he's "making" a string anything in this scenario. He's testing if it is/isn't. I believe, but he hasn't clarified yet –  Sep 11 '14 at 16:53
  • 1
    This is a terrible duplicate flag in my opinion. It's nothing like the other question. –  Sep 11 '14 at 16:57
  • You probably want change your test to `imię.Text.Length >= 2`, as it will always fail on short surnames like *Yi*, *Ho*, and *Wu*. – Michael Sep 11 '14 at 16:57
  • This is not a duplicate since this one explicitly wants the remaining chars as lowercase – Carlos Muñoz Oct 10 '14 at 16:30

2 Answers2

2

use a bit of LINQ in your if statement

if (imię.Text.Length > 2 && char.IsUpper(imię.Text[0]) && imie.Text.Skip(1).All(char.IsLower))
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • 1
    Why would he check if the first character `IsUpper` then test if all (including the first) `IsLower`? This condition will never be met. –  Sep 11 '14 at 16:52
  • whoops forgot to add the Skip – Jonesopolis Sep 11 '14 at 16:53
  • Jonsey thank you for your's so fast answer! Thank you very much it works! :) I don't know Linq before. One more time big thanks :) – user3507810 Sep 11 '14 at 17:01
1
 if ( imię.Text.ToString().Length > 2 && imię.Text == imię.Text.ToString().ToTitleCase())
   {

   }

Although, if the length check ( imię.Text.Length > 2) is only included in your question to prevent errors from (char.IsLower( imię.Text, 1 )) it's not necessary for the if statement.

Chuck Buford
  • 321
  • 1
  • 9
  • Best answer. +1. Although I'd change the variable name and body to make it more relevant. –  Sep 11 '14 at 16:54
  • Just noticed you're not doing the length check either. –  Sep 11 '14 at 16:55
  • 1
    I assumed the OP only used the length check to prevent errors on the second char check. I added it back in and added a caveat that it's not necessary for checking the case. – Chuck Buford Sep 11 '14 at 16:58
  • Yeah I think he's just using it to prevent the exception – Jonesopolis Sep 11 '14 at 16:59
  • 1
    Be careful, `"NAZWISKO".ToTitleCase() == "NAZWISKO"` is `true` in all (?) cultures (and `Text` doesn't have a `ToTitleCase()` method - you're looking for `TextInfo` :)). – Michael Sep 11 '14 at 17:03