Share via

Why the code does not my expected result?

BenTam 1,806 Reputation points
2023-12-31T04:18:03.6+00:00

Dear All,

Could anyone tell me why the code at the bottom does not work?Students

I want to achieve the effect below.

  1. When the "RadioButton" "Company" is clicked, the textbox for entering "Company Name" becomes visible, and the textboxes for entering "Surname, Given Name" and the label "Given Name" become invisible.
  2. When the "RadioButton" "surname" is clicked, the textbox for entering "Company Name" becomes invisible, and the textboxes for entering "Surname, Given Name" and the label "Given Name" become visible.

Company

Surname

        private void Company_radioButton_CheckedChanged(object sender, EventArgs e)
        {
        if (CheckAlreadyRun)
            CheckAlreadyRun = false;
        else
            CheckAlreadyRun = true;
            StuID_label.Text = "Co. ID";
            Company_radioButton.Checked = true;
            Surname_radioButton.Checked = false;
            CompanyName_textBox.Visible = true;
            Surname_textBox.Visible = false;
            GivenName_textBox.Visible = false;
            GivenName_label.Visible = false;
        }

        private void Surname_radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckAlreadyRun)
                CheckAlreadyRun = false;
            else
            {
                CheckAlreadyRun = true;
                StuID_label.Text = "Stu ID";
                Company_radioButton.Checked = false;
                Surname_radioButton.Checked = true;
                CompanyName_textBox.Visible = false;
                Surname_textBox.Visible = true;
                GivenName_textBox.Visible = true;
                GivenName_label.Visible = true;
            }
        }

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author

Viorel 127K Reputation points
2023-12-31T09:24:06.5066667+00:00

Usually these radio buttons belong to the same panel or user control, and the AutoCheck property is set to True. (You can use a separate borderless child panel for M and F radio buttons). Then consider a different approach too:

private void Form_Load( object sender, EventArgs e )
{
    Company_radioButton.Checked = true;
}

private void Company_radioButton_CheckedChanged( object sender, EventArgs e )
{
    Adjust( );
}

private void Surname_radioButton_CheckedChanged( object sender, EventArgs e )
{
    Adjust( );
}

private void Adjust( )
{
    CompanyName_textBox.Visible = Company_radioButton.Checked;
    Surname_textBox.Visible = Surname_radioButton.Checked;
    GivenName_textBox.Visible = Surname_radioButton.Checked;
    GivenName_label.Visible = Surname_radioButton.Checked;
}

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Vahid Ghafarpour 23,605 Reputation points
    2023-12-31T06:53:40.43+00:00

    Thanks for posting your question in the Microsoft Q&A forum.

    It seems it can cause recursion issues.

    Would you please check this updated version?

    private bool CheckAlreadyRun = false;
    
    private void Company_radioButton_CheckedChanged(object sender, EventArgs e)
    {
        if (!CheckAlreadyRun)
        {
            CheckAlreadyRun = true;
            
            StuID_label.Text = "Co. ID";
            Company_radioButton.Checked = true;
            Surname_radioButton.Checked = false;
    
            CompanyName_textBox.Visible = true;
            Surname_textBox.Visible = false;
            GivenName_textBox.Visible = false;
            GivenName_label.Visible = false;
    
            CheckAlreadyRun = false;
        }
    }
    
    private void Surname_radioButton_CheckedChanged(object sender, EventArgs e)
    {
        if (!CheckAlreadyRun)
        {
            CheckAlreadyRun = true;
    
            StuID_label.Text = "Stu ID";
            Company_radioButton.Checked = false;
            Surname_radioButton.Checked = true;
    
            CompanyName_textBox.Visible = false;
            Surname_textBox.Visible = true;
            GivenName_textBox.Visible = true;
            GivenName_label.Visible = true;
    
            CheckAlreadyRun = false;
        }
    }
    
    

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful **

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.