-
November 14th, 2012, 09:21 AM
#1
first program in c#
I am trying to pick up c# and was wondering if someone could show me what I am doing wrong. In the code below I have three radio buttons(go, slow down, and stop). I also have a checkbox is show the results of what the car is doing when each radio button is checked. When I run the program there is nothing in the textbox and just wanting to see if someone can take a quick look and what is wrong...
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Making_Decisions_Winforms1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void DoTrafficLight(string color)
{
switch (color)
{
case "green":
textBox1.Text = "The car is moving!";
//tmrYellow.Enabled = true;
break;
case "yellow":
textBox1.Text = "The car is slowing down!";
tmrYellow.Enabled = true;
break;
case "red":
textBox1.Text = "The car has stopped!";
tmrYellow.Enabled = false;
break;
}
}
private void greenRadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (greenRadioButton1.Checked == true)
{
DoTrafficLight("green");
}
}
private void yellowRadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (yellowRadioButton1.Checked == true)
{
DoTrafficLight("yellow");
}
}
private void redRadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (redRadioButton1.Checked == true)
{
DoTrafficLight("red");
}
}
}
}
-
November 14th, 2012, 01:43 PM
#2
Re: first program in c#
This will probably not completely solve your problem, but notice that clicking a RadioButton will fire not one but two of those CheckedChanged events. You need to have one _CheckedChanged assigned to all three, with all three of the if (x.Checked) snippets in the one event handler.
-
November 14th, 2012, 02:06 PM
#3
Re: first program in c#
So what I am seeing here is that whichever radio button is check the textBox1 will display the strings you have defined. You should be seeing that text. Now, I do not see anything about a checkbox you mentioned to show results? I threw something together real quick that should work very similar to yours:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestTraffic
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void RadioBtn_CheckedChanged(object sender, EventArgs e)
{
if (greenRadioBtn.Checked)
DoTrafficLight("green");
if (yellowRadioBtn.Checked)
DoTrafficLight("yellow");
if (redRadioBtn.Checked)
DoTrafficLight("red");
}
void DoTrafficLight(string color)
{
switch (color)
{
case "green":
if (chkBox1.Checked) //Show "car status" check box
textBox1.Text = "The car is moving!";
//tmrYellow.Enabled = true;
break;
case "yellow":
if (chkBox1.Checked)
textBox1.Text = "The car is slowing down!";
//tmrYellow.Enabled = true;
break;
case "red":
if (chkBox1.Checked)
textBox1.Text = "The car has stopped!";
//tmrYellow.Enabled = false;
break;
}
}
private void chkBox1_CheckedChanged(object sender, EventArgs e)
{
if (!chkBox1.Checked)
textBox1.Clear();
else
{
//Show status of car with current radio button that is already checked when open program
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Also what Zips mentioned is correct, you only need one _CheckChanged assigned to all three. But the form automatically generates a new one for every radio button you double click. That will work but creates code that is not necessarily needed.
Last edited by Deranged; November 14th, 2012 at 03:36 PM.
-
November 14th, 2012, 04:12 PM
#4
Re: first program in c#
I apologize I actually made a typo in my first post. There is not a checkbox in my program. Under the form layout, I just have a groupbox with 3 radio buttons in it (green, yellow, and red). I also have a textbox that should show the results so no checkbox is used.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|