473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dynamic Checkbox Control??

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!!

Nov 27 '06 #1
5 7765
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
Nov 27 '06 #2
Could You Send an example?
I´m searching in Google, but without sucess...

Marc Gravell escreveu:
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
Nov 27 '06 #3
What runtime? 1.1 or 2.0? IIRC it isn't present in 1.1, but I can post
equivalent code if needed...

Marc
Nov 27 '06 #4
I don't know...
I´m using the Visual Studio 2003 with framework 1.1...
Is it??

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

Marc
Nov 27 '06 #5
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;

}
}
Nov 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bobby1041 | last post by:
Hi All! I am trying to make several fields in a HTML form validated, but only when a dynamic checkbox is selected. I am not sure how to do this. Here is a snippet of the dynamic checkbox...
10
by: Jennyfer J Barco | last post by:
Hello, I have a datagrid that brings some information from a query. I need to have a checkbox in each row so the user can select the rows he wants to reprint. Is it possible to have a checkbox...
1
by: savvy | last post by:
I'm comparing two strings and trying to check a dynamic checkbox when its true on pageload , but i dont know why i'm not able to do that on page load, when i click some other links and come back to...
0
by: gwilliam | last post by:
Problem with checkboxes in webpages. I am trying to re-set the values of dinamically created checkboxes in a web page, but no matter what I try the "Checked" property is not set correctly. A...
5
by: lucius | last post by:
Can someone provide a sample of how to add a new CheckBox control to an ASP.NET page, dynamically completely in code-behind? I am having a problem understanding which event should handle the...
3
by: Raymond | last post by:
I am having a problem about the dynamic checkbox Private Sub Page_Load Me.NumberOfChkControls = 0 Dim cbCheckBox As New CheckBox cbCheckBox.Text = .Id.ToString cbCheckBox.ID = "ControlID_" +...
0
by: J | last post by:
I'm dynamically adding checkboxes in the Page_Load (regardless of PostBack). When IsPostBack, the checkboxes display as I expected and their checked/unchecked status is also as expected. At this...
1
by: iderocks | last post by:
Hi All, I created a dynamic checkbox in ASP .Net inside a Button1_Click event method (outside the page_load event) and performed the event handling method for the CheckedChanged event and when I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.