New to C# - Trying to select radio button/hide textbox during Form Load....

I'm new to C# and the syntax is definitely different from that of VB.NET. I'm trying to have a default radio button selected and a textbox hidden when the windows form first loads and is presented to the user.
The code is really straightforward but the code below does nothing to accomplish what I want.
I would appreciate if any of you experts in C# could recommend some good resources for the Load Event and anything to do with C# Programming. Thanks!!
WD
private void MainForm_Load(object sender, EventArgs e)
        {
            radioSingleObj.Checked = true;
            textBoxMult.Visible = false;
        }

Open in new window

wally_davisAsked:
Who is Participating?
 
SankoziConnect With a Mentor Commented:
I checked your code and it worked. Your radio buttons probably aren't in the same group (= in the same panel, frame, groupbox etc.). Because of that they aren't "linked" with each other.

Moreover:

1. This handler should be added only to radioSingleObj if there are only two radio buttons in this group. The better way to handle such events is written below.
2. You don't have to do this, this should be done automatically if radio buttons are in the same group
3. You change this once, at the beginning (radioSingleObj is checked at the form load), it is better to set it in the designer
private void radioSingleObj_CheckedChanged(object sender, EventArgs e)// 1.
{
     if (radioSingleObj.Checked)
     {
           textBoxSingle.Enabled = true;//3
           textBoxSingle.Visible = true;
           textBoxMult.Visible = false;
           radioMultObj.Checked = false;//2.
     }
     else if (radioMultObj.Checked)
     {
           textBoxSingle.Visible = false;
           radioSingleObj.Checked = false;//2.
           textBoxMult.Visible = true;
     }
}
 
//easier to read, less error-prone way
 
private void radioSingleObj_CheckedChanged(object sender, EventArgs e)
{
    if (radioSingleObj.Checked)
    {
        textBoxSingle.Visible = true;
    } 
    else 
    {
        textBoxSingle.Visible = false;
    }
}
 
private void radioMultObj_CheckedChanged(object sender, EventArgs e)
{
    if (radioMultObj.Checked)
    {
        textBoxMult.Visible = true;
    } 
    else 
    {
        textBoxMult.Visible = false;
    }
}
   

Open in new window

0
 
SankoziCommented:
You should check event tutorial on msdn page : http://msdn.microsoft.com/en-us/library/aa288436(VS.71).aspx

Here is list of other tutorials : http://www.freeprogrammingresources.com/csharptutorials.html

I checked your method - it should work. You probably haven't added it to the event in the constructor:
MainForm(){
/* ... */
this.Load += new System.EventHandler(this.MainForm_Load);
}

Open in new window

0
 
wally_davisAuthor Commented:
See my code below. It does check the radio button "radioSingleObj" but when I click on the "radioMultObj" radio button, it doesn't hide the txtBoxSingle textbox object nor does it make the txtBoxMult textbox visible. Again, this is my first real attempt at C# so please bear with me.
Thanks.
public MainForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.MainForm_Load);
        }
 
        private void MainForm_Load(object sender, EventArgs e)
        {
            radioSingleObj.Checked = true;
            textBoxMult.Visible = false;
        }
 
        private void radioSingleObj_CheckedChanged(object sender, EventArgs e)
        {
            if (radioSingleObj.Checked)
            {
                textBoxSingle.Enabled = true;
                textBoxSingle.Visible = true;
                textBoxMult.Visible = false;
                radioMultObj.Checked = false;
            }
            else if (radioMultObj.Checked)
            {
                textBoxSingle.Visible = false;
                radioSingleObj.Checked = false;
                textBoxMult.Visible = true;
            }
        }
 
        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
margajet24IT Business AnalystCommented:
public MainForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.MainForm_Load);
            radioSingleObj.Checked = true;
            textBoxMult.Visible = false;
        }

when
radioSingleObj.Checked = true;
 is executed, radioSingleObj_CheckedChanged will be fired

0
 
wally_davisAuthor Commented:
Sankozi, Even though your code looks much better and I thought it would work, it didn't. The code now looks like what you see below, but, I had to go to do the following extra steps to get it to work. I would like to add this to the code if possible and then remove the changes I made to the radio button controls event properties.

I had to click on the "radioSingleObj" (for single workstations) radio button object, click on the Events button, and then add "radioSingleObj_CheckChanged" as the value to the radioSingleObj radio button Event Property.
I also had to click on the "radioMultObj" (for single workstations) radio button object, click on the Events button, and then add "radioMultObj_CheckChanged" as the value to the radioMultObj radio button Event Property.
FINALLY, my apologies. If I thought this would become a major challenge, I would have offered more points and I could if there were a way.

namespace Delete_AD_Objects
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.MainForm_Load);
        }
 
        private void MainForm_Load(object sender, EventArgs e)
        {
            radioSingleObj.Checked = true;
            textBoxMult.Visible = false;
            //textBoxSingle.Enabled = true;
        }
 
        private void radioSingleObj_CheckedChanged(object sender, EventArgs e)
        {
            if (radioSingleObj.Checked)
            {
                textBoxSingle.Visible = true;
            }
            else
            {
                textBoxSingle.Visible = false;
            }
        }
 
        private void radioMultObj_CheckChanged(object sender, EventArgs e)
        {
            if (radioMultObj.Checked)
            {
                textBoxMult.Visible = true;
                textBoxSingle.Enabled = true;
            }
            else
            {
                textBoxMult.Visible = false;
            }
        }

Open in new window

0
 
SankoziCommented:
This code also works. It must be something wrong in the other part of class (created by the designer - in this case probably file has name MainForm.Designer.cs). Could you show it?
0
 
wally_davisAuthor Commented:
Sankozi, the actual name of the file is Form1.Designer.cs. Do you think that's the problem? I renamed the Form from Form1 to MainForm as that is the name of the object used in the code or that at least, should be.
See the Form1.Designer.cs code below.
Thanks!
namespace Delete_AD_Objects
{
    partial class MainForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.buttonRun = new System.Windows.Forms.Button();
            this.radioSingleObj = new System.Windows.Forms.RadioButton();
            this.radioMultObj = new System.Windows.Forms.RadioButton();
            this.textBoxSingle = new System.Windows.Forms.TextBox();
            this.gbSelection = new System.Windows.Forms.GroupBox();
            this.textBoxMult = new System.Windows.Forms.TextBox();
            this.buttonExit = new System.Windows.Forms.Button();
            this.gbAdConnStatus = new System.Windows.Forms.GroupBox();
            this.labelDisplayStatus = new System.Windows.Forms.Label();
            this.buttonAdTest = new System.Windows.Forms.Button();
            this.gbSelection.SuspendLayout();
            this.gbAdConnStatus.SuspendLayout();
            this.SuspendLayout();
            // 
            // buttonRun
            // 
            this.buttonRun.Location = new System.Drawing.Point(202, 314);
            this.buttonRun.Name = "buttonRun";
            this.buttonRun.Size = new System.Drawing.Size(87, 25);
            this.buttonRun.TabIndex = 0;
            this.buttonRun.Text = "&Run";
            this.buttonRun.UseVisualStyleBackColor = true;
            this.buttonRun.Click += new System.EventHandler(this.buttonRun_Click);
            // 
            // radioSingleObj
            // 
            this.radioSingleObj.AutoSize = true;
            this.radioSingleObj.Location = new System.Drawing.Point(24, 21);
            this.radioSingleObj.Name = "radioSingleObj";
            this.radioSingleObj.Size = new System.Drawing.Size(127, 18);
            this.radioSingleObj.TabIndex = 1;
            this.radioSingleObj.TabStop = true;
            this.radioSingleObj.Text = "Single Workstation";
            this.radioSingleObj.UseVisualStyleBackColor = true;
            this.radioSingleObj.CheckedChanged += new System.EventHandler(this.radioSingleObj_CheckedChanged);
            // 
            // radioMultObj
            // 
            this.radioMultObj.AutoSize = true;
            this.radioMultObj.Location = new System.Drawing.Point(24, 49);
            this.radioMultObj.Name = "radioMultObj";
            this.radioMultObj.Size = new System.Drawing.Size(141, 18);
            this.radioMultObj.TabIndex = 2;
            this.radioMultObj.TabStop = true;
            this.radioMultObj.Text = "Multiple Workstations";
            this.radioMultObj.UseVisualStyleBackColor = true;
            this.radioMultObj.CheckedChanged += new System.EventHandler(this.radioMultObj_CheckChanged);
            // 
            // textBoxSingle
            // 
            this.textBoxSingle.Location = new System.Drawing.Point(171, 17);
            this.textBoxSingle.Name = "textBoxSingle";
            this.textBoxSingle.Size = new System.Drawing.Size(147, 22);
            this.textBoxSingle.TabIndex = 3;
            // 
            // gbSelection
            // 
            this.gbSelection.Controls.Add(this.textBoxSingle);
            this.gbSelection.Controls.Add(this.textBoxMult);
            this.gbSelection.Controls.Add(this.radioSingleObj);
            this.gbSelection.Controls.Add(this.radioMultObj);
            this.gbSelection.Location = new System.Drawing.Point(14, 38);
            this.gbSelection.Name = "gbSelection";
            this.gbSelection.Size = new System.Drawing.Size(356, 138);
            this.gbSelection.TabIndex = 4;
            this.gbSelection.TabStop = false;
            this.gbSelection.Text = "Delete Options";
            // 
            // textBoxMult
            // 
            this.textBoxMult.Location = new System.Drawing.Point(171, 17);
            this.textBoxMult.Multiline = true;
            this.textBoxMult.Name = "textBoxMult";
            this.textBoxMult.Size = new System.Drawing.Size(147, 105);
            this.textBoxMult.TabIndex = 4;
            // 
            // buttonExit
            // 
            this.buttonExit.Location = new System.Drawing.Point(295, 314);
            this.buttonExit.Name = "buttonExit";
            this.buttonExit.Size = new System.Drawing.Size(75, 25);
            this.buttonExit.TabIndex = 5;
            this.buttonExit.Text = "E&xit";
            this.buttonExit.UseVisualStyleBackColor = true;
            this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
            // 
            // gbAdConnStatus
            // 
            this.gbAdConnStatus.Controls.Add(this.buttonAdTest);
            this.gbAdConnStatus.Controls.Add(this.labelDisplayStatus);
            this.gbAdConnStatus.Location = new System.Drawing.Point(14, 182);
            this.gbAdConnStatus.Name = "gbAdConnStatus";
            this.gbAdConnStatus.Size = new System.Drawing.Size(356, 72);
            this.gbAdConnStatus.TabIndex = 6;
            this.gbAdConnStatus.TabStop = false;
            this.gbAdConnStatus.Text = "AD Connection Status";
            // 
            // labelDisplayStatus
            // 
            this.labelDisplayStatus.AutoSize = true;
            this.labelDisplayStatus.Location = new System.Drawing.Point(151, 28);
            this.labelDisplayStatus.Name = "labelDisplayStatus";
            this.labelDisplayStatus.Size = new System.Drawing.Size(0, 14);
            this.labelDisplayStatus.TabIndex = 1;
            // 
            // buttonAdTest
            // 
            this.buttonAdTest.Location = new System.Drawing.Point(24, 30);
            this.buttonAdTest.Name = "buttonAdTest";
            this.buttonAdTest.Size = new System.Drawing.Size(111, 23);
            this.buttonAdTest.TabIndex = 2;
            this.buttonAdTest.Text = "Test Connection:";
            this.buttonAdTest.UseVisualStyleBackColor = true;
            this.buttonAdTest.Click += new System.EventHandler(this.buttonAdTest_Click);
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(385, 351);
            this.Controls.Add(this.gbAdConnStatus);
            this.Controls.Add(this.buttonExit);
            this.Controls.Add(this.gbSelection);
            this.Controls.Add(this.buttonRun);
            this.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "MainForm";
            this.Text = "Delete AD Wkstn Object(s)";
            this.gbSelection.ResumeLayout(false);
            this.gbSelection.PerformLayout();
            this.gbAdConnStatus.ResumeLayout(false);
            this.gbAdConnStatus.PerformLayout();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private System.Windows.Forms.Button buttonRun;
        private System.Windows.Forms.RadioButton radioSingleObj;
        private System.Windows.Forms.RadioButton radioMultObj;
        private System.Windows.Forms.TextBox textBoxSingle;
        private System.Windows.Forms.GroupBox gbSelection;
        private System.Windows.Forms.TextBox textBoxMult;
        private System.Windows.Forms.Button buttonExit;
        private System.Windows.Forms.GroupBox gbAdConnStatus;
        private System.Windows.Forms.Label labelDisplayStatus;
        private System.Windows.Forms.Button buttonAdTest;
    }
}

Open in new window

0
 
SankoziCommented:
Name of file shouldn't be a problem.

I merged class files from your last 2 posts. I run it and it worked too.

I think there may be some problems with your solution. Try creating new one: create new project (Windows Forms Application), add new empty code file, paste contents from this code snippet and change line "Application.Run(new Form1());" to  "Application.Run(new WindowsFormsApplicationTest.MainForm());" and run. I hope this will work.
using System.Windows.Forms;
using System;
namespace WindowsFormsApplicationTest
{
    public class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.MainForm_Load);
        }
 
        private void MainForm_Load(object sender, EventArgs e)
        {
            radioSingleObj.Checked = true;
            textBoxMult.Visible = false;
            //textBoxSingle.Enabled = true;
        }
 
        private void radioSingleObj_CheckedChanged(object sender, EventArgs e)
        {
            if (radioSingleObj.Checked)
            {
                textBoxSingle.Visible = true;
            }
            else
            {
                textBoxSingle.Visible = false;
            }
        }
 
        private void radioMultObj_CheckChanged(object sender, EventArgs e)
        {
            if (radioMultObj.Checked)
            {
                textBoxMult.Visible = true;
                textBoxSingle.Enabled = true;
            }
            else
            {
                textBoxMult.Visible = false;
            }
        }
 
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.buttonRun = new System.Windows.Forms.Button();
            this.radioSingleObj = new System.Windows.Forms.RadioButton();
            this.radioMultObj = new System.Windows.Forms.RadioButton();
            this.textBoxSingle = new System.Windows.Forms.TextBox();
            this.gbSelection = new System.Windows.Forms.GroupBox();
            this.textBoxMult = new System.Windows.Forms.TextBox();
            this.buttonExit = new System.Windows.Forms.Button();
            this.gbAdConnStatus = new System.Windows.Forms.GroupBox();
            this.labelDisplayStatus = new System.Windows.Forms.Label();
            this.buttonAdTest = new System.Windows.Forms.Button();
            this.gbSelection.SuspendLayout();
            this.gbAdConnStatus.SuspendLayout();
            this.SuspendLayout();
            // 
            // buttonRun
            // 
            this.buttonRun.Location = new System.Drawing.Point(202, 314);
            this.buttonRun.Name = "buttonRun";
            this.buttonRun.Size = new System.Drawing.Size(87, 25);
            this.buttonRun.TabIndex = 0;
            this.buttonRun.Text = "&Run";
            this.buttonRun.UseVisualStyleBackColor = true;
            //this.buttonRun.Click += new System.EventHandler(this.buttonRun_Click);
            // 
            // radioSingleObj
            // 
            this.radioSingleObj.AutoSize = true;
            this.radioSingleObj.Location = new System.Drawing.Point(24, 21);
            this.radioSingleObj.Name = "radioSingleObj";
            this.radioSingleObj.Size = new System.Drawing.Size(127, 18);
            this.radioSingleObj.TabIndex = 1;
            this.radioSingleObj.TabStop = true;
            this.radioSingleObj.Text = "Single Workstation";
            this.radioSingleObj.UseVisualStyleBackColor = true;
            this.radioSingleObj.CheckedChanged += new System.EventHandler(this.radioSingleObj_CheckedChanged);
            // 
            // radioMultObj
            // 
            this.radioMultObj.AutoSize = true;
            this.radioMultObj.Location = new System.Drawing.Point(24, 49);
            this.radioMultObj.Name = "radioMultObj";
            this.radioMultObj.Size = new System.Drawing.Size(141, 18);
            this.radioMultObj.TabIndex = 2;
            this.radioMultObj.TabStop = true;
            this.radioMultObj.Text = "Multiple Workstations";
            this.radioMultObj.UseVisualStyleBackColor = true;
            this.radioMultObj.CheckedChanged += new System.EventHandler(this.radioMultObj_CheckChanged);
            // 
            // textBoxSingle
            // 
            this.textBoxSingle.Location = new System.Drawing.Point(171, 17);
            this.textBoxSingle.Name = "textBoxSingle";
            this.textBoxSingle.Size = new System.Drawing.Size(147, 22);
            this.textBoxSingle.TabIndex = 3;
            // 
            // gbSelection
            // 
            this.gbSelection.Controls.Add(this.textBoxSingle);
            this.gbSelection.Controls.Add(this.textBoxMult);
            this.gbSelection.Controls.Add(this.radioSingleObj);
            this.gbSelection.Controls.Add(this.radioMultObj);
            this.gbSelection.Location = new System.Drawing.Point(14, 38);
            this.gbSelection.Name = "gbSelection";
            this.gbSelection.Size = new System.Drawing.Size(356, 138);
            this.gbSelection.TabIndex = 4;
            this.gbSelection.TabStop = false;
            this.gbSelection.Text = "Delete Options";
            // 
            // textBoxMult
            // 
            this.textBoxMult.Location = new System.Drawing.Point(171, 17);
            this.textBoxMult.Multiline = true;
            this.textBoxMult.Name = "textBoxMult";
            this.textBoxMult.Size = new System.Drawing.Size(147, 105);
            this.textBoxMult.TabIndex = 4;
            // 
            // buttonExit
            // 
            this.buttonExit.Location = new System.Drawing.Point(295, 314);
            this.buttonExit.Name = "buttonExit";
            this.buttonExit.Size = new System.Drawing.Size(75, 25);
            this.buttonExit.TabIndex = 5;
            this.buttonExit.Text = "E&xit";
            this.buttonExit.UseVisualStyleBackColor = true;
            //this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
            // 
            // gbAdConnStatus
            // 
            this.gbAdConnStatus.Controls.Add(this.buttonAdTest);
            this.gbAdConnStatus.Controls.Add(this.labelDisplayStatus);
            this.gbAdConnStatus.Location = new System.Drawing.Point(14, 182);
            this.gbAdConnStatus.Name = "gbAdConnStatus";
            this.gbAdConnStatus.Size = new System.Drawing.Size(356, 72);
            this.gbAdConnStatus.TabIndex = 6;
            this.gbAdConnStatus.TabStop = false;
            this.gbAdConnStatus.Text = "AD Connection Status";
            // 
            // labelDisplayStatus
            // 
            this.labelDisplayStatus.AutoSize = true;
            this.labelDisplayStatus.Location = new System.Drawing.Point(151, 28);
            this.labelDisplayStatus.Name = "labelDisplayStatus";
            this.labelDisplayStatus.Size = new System.Drawing.Size(0, 14);
            this.labelDisplayStatus.TabIndex = 1;
            // 
            // buttonAdTest
            // 
            this.buttonAdTest.Location = new System.Drawing.Point(24, 30);
            this.buttonAdTest.Name = "buttonAdTest";
            this.buttonAdTest.Size = new System.Drawing.Size(111, 23);
            this.buttonAdTest.TabIndex = 2;
            this.buttonAdTest.Text = "Test Connection:";
            this.buttonAdTest.UseVisualStyleBackColor = true;
            //this.buttonAdTest.Click += new System.EventHandler(this.buttonAdTest_Click);
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(385, 351);
            this.Controls.Add(this.gbAdConnStatus);
            this.Controls.Add(this.buttonExit);
            this.Controls.Add(this.gbSelection);
            this.Controls.Add(this.buttonRun);
            this.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "MainForm";
            this.Text = "Delete AD Wkstn Object(s)";
            this.gbSelection.ResumeLayout(false);
            this.gbSelection.PerformLayout();
            this.gbAdConnStatus.ResumeLayout(false);
            this.gbAdConnStatus.PerformLayout();
            this.ResumeLayout(false);
 
        }
 
        private System.Windows.Forms.Button buttonRun;
        private System.Windows.Forms.RadioButton radioSingleObj;
        private System.Windows.Forms.RadioButton radioMultObj;
        private System.Windows.Forms.TextBox textBoxSingle;
        private System.Windows.Forms.GroupBox gbSelection;
        private System.Windows.Forms.TextBox textBoxMult;
        private System.Windows.Forms.Button buttonExit;
        private System.Windows.Forms.GroupBox gbAdConnStatus;
        private System.Windows.Forms.Label labelDisplayStatus;
        private System.Windows.Forms.Button buttonAdTest;
 
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
 
    }
}

Open in new window

0
 
wally_davisAuthor Commented:
Sankozi, you're solution and final solution you recommended worked. Not sure what went wrong in the code but after creating new Form all went well. I'm awarding you 500 points instead of 150 for your efforts. Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.