locked
c# code snippet RRS feed

  • Question

  • if (myRadioButton1.Focus().ToString())

    why is this code snippet wrong?

    Saturday, June 9, 2012 3:35 PM

Answers

  • Hi,

    The question is: What do you intend to do?

    Do you wanna check if radionbutton has a focus? If so do:

    if (myRadioButton1.Focused)
    {
       //has focus
    }
    

    Or you wanna get some other information out or control?


    Mitja

    Saturday, June 9, 2012 4:56 PM
  • Because you are not comparing the string value that is being returned to anything.  Perhaps you meant to use something like:

    if (myRadioButton1.Focus())
    {
    }


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by ms811 Sunday, June 10, 2012 2:31 AM
    Saturday, June 9, 2012 3:47 PM
  • Focus() returns a bool value.  If you use .ToString() you now have "False" or "True" (or some variation).  You need to compare the string to some value, such as:
           
        if (myRadioButton1.Focus().ToString() == "True")

    --
    Mike
    • Marked as answer by ms811 Sunday, June 10, 2012 5:36 PM
    Sunday, June 10, 2012 11:34 AM

All replies

  • which is this code snippet wrong?
    Saturday, June 9, 2012 3:34 PM
  • Because you are not comparing the string value that is being returned to anything.  Perhaps you meant to use something like:

    if (myRadioButton1.Focus())
    {
    }


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by ms811 Sunday, June 10, 2012 2:31 AM
    Saturday, June 9, 2012 3:47 PM
  • There are two errors:

    • You're calling ToString non a void method (Focus)
    • The if instruction needs a boolean condition, while you're trying to use a string.

    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    Saturday, June 9, 2012 4:31 PM
  • Hi,

    The question is: What do you intend to do?

    Do you wanna check if radionbutton has a focus? If so do:

    if (myRadioButton1.Focused)
    {
       //has focus
    }
    

    Or you wanna get some other information out or control?


    Mitja

    Saturday, June 9, 2012 4:56 PM
  • could you please elaborate

    ms

    Sunday, June 10, 2012 2:31 AM
  • Hello ms811,

    which is this code snippet wrong?

    not very clear what you ask, if you give some more information we can better assist you, if you check one or more RadioButton, if only one code Mitja does what you ask, but if you have more than one board or you a ForEach loop or Linq To Objects , one sample here.

       private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                foreach (var myRadioButton in Controls.OfType<RadioButton>().Where(myRadioButton => myRadioButton.Focused))
                {
                    MessageBox.Show(string.Format("Focused is on {0}", myRadioButton.Name));
                }
            }

    For to try this sample please set KeyPreview of your form to true , and manage keyDown event del Form .

    Regards.


    Sunday, June 10, 2012 9:05 AM
  • Kindly provide more info about what you want to do?
    Sunday, June 10, 2012 9:11 AM
  • Focus() returns a bool value.  If you use .ToString() you now have "False" or "True" (or some variation).  You need to compare the string to some value, such as:
           
        if (myRadioButton1.Focus().ToString() == "True")

    --
    Mike
    • Marked as answer by ms811 Sunday, June 10, 2012 5:36 PM
    Sunday, June 10, 2012 11:34 AM