2

I have a textbox that gets the gender value from database. Based on the textchanged event, the radiobuttons respond accordingly.

private void txtInvisibleGender_TextChanged(object sender, EventArgs e)
{
    if (txtInvisibleGender.Text == "Female")
        rbFemale.Checked = true;
    else
        rbMale.Checked = true;
}

The problem is that when the data in textbox is Female, then why is the Male radiobutton being checked? It doesn't check based on the data from the textbox. How do I make this work?

deepz
  • 225
  • 3
  • 13

2 Answers2

3

You should user string.Compare instead of ==.

it means that your textbox doesn't have this value, atleast not in the same casing. Try this

private void txtInvisibleGender_TextChanged(object sender, EventArgs e)
{
 if(string.Compare(txtInvisibleGender.Text.Trim(), "Female", StringComparison.OrdinalIgnoreCase) == 0)     
    rbFemale.Checked = true;
else
    rbMale.Checked = true;
}
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • 1
    @SamLeach You should read http://stackoverflow.com/questions/3678792/are-string-equals-and-operator-really-same – Ehsan Jul 24 '13 at 13:31
  • Thanks. Interesting, favourited. +1 – Sam Leach Jul 24 '13 at 13:37
  • @SamLeach i have updated my answer and read this as well http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp – Ehsan Jul 24 '13 at 13:38
2

Your false branch is always excecuting as your condition is never true.

if (txtInvisibleGender.Text == "Female")
    rbFemale.Checked = true;
else
    rbMale.Checked = true; // we are reaching here.

I suggest changing it to

if (txtInvisibleGender.Text.Trim().ToLower().Contains("female"))
    rbFemale.Checked = true;
else
    rbMale.Checked = true; 

It's worth noting that the if-else will check male under all circumstances where txtInvisibleGender does not contain "female". So typing "foobar" will check male.

I would change it to:

// "female" contains "male" so Contains() cannot be used!
if (txtInvisibleGender.Text.Trim().ToLower().Equals("female"))
    rbFemale.Checked = true;

if (txtInvisibleGender.Text.Trim().ToLower().Equals("male"))
    rbMale.Checked = true;

then if it's anything other than "male" or "female" it doesn't check anything.

Sam Leach
  • 12,746
  • 9
  • 45
  • 73
  • even if I try another If statement for Male and also the Else-If, It doesn't work as it is supposed to.. – deepz Jul 24 '13 at 13:13
  • 1
    See my updated answer. Change to txtInvisibleGender.Text.ToLower().Contains("female"). – Sam Leach Jul 24 '13 at 13:13
  • Yes I just tried... That worked! Can you please explain why my code didn't work? It was basically the same thing with the txtInvisibleGender.Text == "Female" – deepz Jul 24 '13 at 13:15
  • 1
    Your code didn't work because the text box did not exactly contain the string "Female". It might have had other characters like "/n" or the case was wrong. "Female" != "female" – Sam Leach Jul 24 '13 at 13:19
  • 1
    Trim() removes whitespace, ToLower() removed uppercase and Contains() says that the string has the string "female" somewhere within it. – Sam Leach Jul 24 '13 at 13:21
  • 2
    I'm not so sure the bottom code is correct. If you call `.Contains("male")` on the string "female", then it will return true, and check the male radio button. I think `.Equals` would be better. That, or it needs to be `else-if` – Shaz Jul 24 '13 at 13:24
  • oh yeah! female contains male! Thanks RyanWH:) – Sam Leach Jul 24 '13 at 13:30
  • @SamLeach seen. Thanks for explanations. – deepz Jul 24 '13 at 13:35