Dynamic Checkbox Control??

S

Senger

Hi There!

I need change the state of a checkboxbutton of a list of buttons.
I use the folowing code to create buttons:


for (z=1; z<=10; z++)
{
string booth = "boo" + (z+t).ToString();
CheckBox MiControl = new CheckBox();
MiControl.Name = boo;
MiControl.Appearance = System.Windows.Forms.Appearance.Button;
MiControl.BackColor = System.Drawing.Color.White;
MiControl.Cursor = System.Windows.Forms.Cursors.Hand;
MiControl.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
MiControl.ForeColor = System.Drawing.Color.White;
MiControl.ImageIndex = 3;
MiControl.ImageList = this.IconesCabines;
MiControl.Location = new System.Drawing.Point(x, y);
MiControl.Size = new System.Drawing.Size(50, 43);
MiControl.CheckStateChanged += new
System.EventHandler(this.boo_change);
x = x + 56;
this.Painel.Controls.Add(MiControl);
}


private void boo_change(object sender, System.EventArgs e){


CheckBox rdb = (CheckBox) sender;
if (rdb.Checked)
{
rdb.ImageIndex = 1;
}
else{
rdb.ImageIndex = 3;
}



}


It's perfect...

But how can I change the properties of the checkbox like


private void button_click(object sender, System.EventArgs e){
boo5.ImageIndex = 2;



}


Tks!!
 
M

Marc Gravell

Either you need to find the control dynamically by name (Controls.Find()),
else you need to keep the instances in a field somewhere - either an array
or a dictionary would do.

Marc
 
S

Senger

Could You Send an example?
I´m searching in Google, but without sucess...

Marc Gravell escreveu:
 
M

Marc Gravell

What runtime? 1.1 or 2.0? IIRC it isn't present in 1.1, but I can post
equivalent code if needed...

Marc
 
S

Senger

I don't know...
I´m using the Visual Studio 2003 with framework 1.1...
Is it??

Marc Gravell escreveu:
 
M

Marc Gravell

Have assumed 1.1, but included 2.0 approach too; just press [Return] in the
textbox to focus the named button. Not a very good example, but an example
none-the-less.

using System;
using System.Windows.Forms;

class FormDemo : Form
{
static void Main()
{
Application.Run(new FormDemo());
}
TextBox tb;
public FormDemo()
{
tb = new TextBox();
GroupBox gb = new GroupBox();

tb.Text = "Button3";
tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
tb.Dock = DockStyle.Top;
gb.Text = "Just a container";
gb.Dock = DockStyle.Fill;
int top = gb.Padding.Top;
for (int i = 0; i < 10; i++)
{
Button b = new Button();
string name = "Button" + i.ToString();
b.Text = b.Name = name;
top += b.Margin.Top;
b.Top = top;
top += b.Height + b.Margin.Bottom;
gb.Controls.Add(b);
}
this.Controls.Add(gb);
this.Controls.Add(tb);

}

void tb_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
// 2.0 Controls.Find
//Control[] found = this.Controls.Find(tb.Text, true);
//if (found.Length == 1) found[0].Focus();

// 1.1
Control found = FindControl(this, tb.Text);
if (found != null) found.Focus();
}
}

// could do as a queue / stack, but this should be OK
static Control FindControl(Control control, string name)
{
if (control.Name == name) return control;
foreach (Control child in control.Controls)
{
Control found = FindControl(child, name);
if (found != null) return found;
}
return null;

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top