-
January 5th, 2013, 01:50 PM
#1
Radiobuttons
Hi all,
I am using two radiobuttons in visual studio window form. Now I want some buttons to be factional when radiobutton1 is pressed and the same thing with radiobutton2.
To do so, I copied and paste the coding of the buttons in the radiobutton1 loop, as show below:
Code:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
private void button1_Click_MouseDown(object sender, MouseEventArgs e)
{
label1.Text = "X";
}
private void button1_Click_MouseUp(object sender, MouseEventArgs e)
{
label1.Text = "Y";
}
}
When running, the program is giving errors. Can someone please tell me the way to do this ??
Thanks in advance.
-
January 5th, 2013, 09:40 PM
#2
Re: Radiobuttons
Originally Posted by Dritech
....Now I want some buttons to be factional when radiobutton1 is pressed and the same thing with radiobutton2. .
what is "factional"? Also, what type of errors are you seeing? We can't help if you only say you're getting an error - you need to be specific on what isn't working and the actual error you're getting.
-
January 6th, 2013, 06:13 AM
#3
Re: Radiobuttons
Hi, thanks for the reply.
functional.
The errors are:
1) } expected ... but I cannot see a missing }
2) Expected class, delegate, enum, interface, or struc
3) Type or namespace definition, or end-of-file expected
Below is the coding:
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 aa
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Left";
label2.Text = "Label2";
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
private void button2_Click(object sender, EventArgs e)
{
label2.Text = "Right";
label1.Text = "Label1";
}
}
}
}
The aim of this program is to ignore buttom2 when radiobutton1 is ticked and ignore buttom1 when radiobutton2 is true.
Thanks in advance.
-
January 6th, 2013, 01:23 PM
#4
Re: Radiobuttons
You cannot have a procedure inside another procedure. Meaning, you cannot contain void button1_Click inside radioButton1_CheckedChanged . You would have to use some sore of boolean logic to determine which radio button was selected or not.
-
January 6th, 2013, 04:16 PM
#5
Re: Radiobuttons
Hi, thanks for the reply.
I used boolean but it will not look neat when having lets say 10 buttons for each radiobutton. This is because you need to add "(if bool == true)" for every button as shown below:
Code:
private void button1_Click(object sender, EventArgs e)
{
(if bool == true)
blabla
}
Is there other ways of for to solve this please?
-
January 6th, 2013, 08:51 PM
#6
Re: Radiobuttons
Originally Posted by Dritech
I used boolean but it will not look neat when having lets say 10 buttons for each radiobutton. This is because you need to add "(if bool == true)" for every button as shown below:
Code:
private void button1_Click(object sender, EventArgs e)
{
(if bool == true)
blabla
}
Is there other ways of for to solve this please?
isn't valid code syntax. Perhaps this is a typo in your post, but if it isn't you might want review C# syntax basics. Also, try to be clearer on what you are actually trying to do. I'm guessing that you want to enable/disable some buttons based which radio button is selected, but I'm not sure from your description.
At any rate, check out the following post for a couple of different approaches toward this end.
http://forums.codeguru.com/showthrea...tton-questions
-
January 7th, 2013, 02:59 PM
#7
Re: Radiobuttons
Hi,
You could use arrays of controls. Try something like this.
Add 2 labels to a form then do this....
Code:
public partial class Form1 : Form
{
RadioButton[] RadioButtons = new RadioButton[10];
Button[] Buttons = new Button[10] ;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int x = 100;
int y = 20;
try
{
for (int i = 0; i < RadioButtons.Length; i++)
{
RadioButtons[i] = new System.Windows.Forms.RadioButton();
RadioButtons[i].AutoSize = true;
RadioButtons[i].Location = new System.Drawing.Point(x, y);
RadioButtons[i].Name = "radioButton" + i;
RadioButtons[i].Size = new System.Drawing.Size(85, 17);
RadioButtons[i].TabIndex = 8;
RadioButtons[i].TabStop = true;
RadioButtons[i].Text = "radioButton" + i;
RadioButtons[i].Tag = i.ToString();
RadioButtons[i].UseVisualStyleBackColor = true;
Controls.Add(RadioButtons[i]);
RadioButtons[i].CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
y = y + 24;
}
x = 300;
y = 20;
for (int i = 0; i < Buttons.Length ; i++)
{
Buttons[i] = new System.Windows.Forms.Button();
Buttons[i].Location = new System.Drawing.Point(x, y);
Buttons[i].Name = "button" + i;
Buttons[i].Size = new System.Drawing.Size(86, 22);
Buttons[i].TabIndex = 3;
Buttons[i].Text = "button" + i;
Buttons[i].UseVisualStyleBackColor = true;
Buttons[i].Tag = i.ToString();
Controls.Add(Buttons[i]);
Buttons[i].Click += new System.EventHandler(this.button_Click);
y = y + 24;
}
}
catch
{
}
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
try
{
RadioButton myNewType = default(RadioButton);
myNewType = (RadioButton)sender;
label1.Text = myNewType.Tag.ToString();
// add your code here to change text, labels
}
catch (Exception ex)
{
}
}
private void button_Click(object sender, EventArgs e)
{
try
{
Button myNewType = default(Button);
myNewType = (Button)sender;
label2.Text = myNewType.Tag.ToString();
// add your code here to change text, labels
}
catch (Exception ex)
{
}
}
}
Curt
-
January 7th, 2013, 03:17 PM
#8
Re: Radiobuttons
Hi, thanks for the replies.
Code:
isn't valid code syntax. Perhaps this is a typo in your post, but if it isn't you might want review C# syntax basics.
Yes that's a typo.
Code:
Also, try to be clearer on what you are actually trying to do. I'm guessing that you want to enable/disable some buttons based which radio button is selected, but I'm not sure from your description.
That's what I was trying to do. I solved the problem by using 'button.Enabled = false;' as shown below:
Code:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
}
-
January 7th, 2013, 03:46 PM
#9
Re: Radiobuttons
Originally Posted by Dritech
That's what I was trying to do. I solved the problem by using 'button.Enabled = false;' as shown below:
Code:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
}
That's one approach but it doesn't scale well. Consider what would happen if you had two groups of 5 radio buttons managing the enable/disable state of 10 buttons each?
Check out the two sample code projects in the link I posted and try to understand those approaches. Both eliminate the need to create check changed handlers for every radio button which should result in smaller, more maintainabile code.
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
|