How to add parameters in EventHandler - c#

This is what i want to do:
....
for(i=0;i<=99;i++)
{
btn[i].Click += new EventHandler(btn_Clicked,i);
}
}
private void btn_Clicked(object sender, EventArgs e,int i)
{
compute((Button)sender, picRetrieved[i]);
}
Please help. It is showing error. But, I want to send the value of i for further processing. Is there any way to do it?
Thank you.

Thanks to closures and anonymous functions you can do something similar:
for(int i=0;i<=99;i++)
{
int j = i;
btn[i].Click += (sender, e) =>
{
// Very important! Here use only j, not i!
compute((Button)sender, picRetrieved[j]);
};
}
There is a small "problem" (that isn't a problem, it's how they are done) so that you mustn't use the i variable inside the anonymous function, but if you copy it somewhere else (j for example) then you can use j.

You can't. The signature of the event handler is fixed, you can't change it.
In this case, you could use an anonymous delegate to get what you want.
for(i = 0; i <= 99; i++)
{
int dummy = i;
btn[i].Click += delegate(object sender, EventArgs e)
{
compute(btn[dummy], picRetrieved[dummy]);
}
}
Another option is to 'tag' the button:
for(i = 0; i <= 99; i++)
{
btn[i].Click += btn_Clicked;
btn[i].Tag = i;
}
private void btn_Clicked(object sender, EventArgs e,int i)
{
Button b = (Button)sender;
int i = (int)b.Tag;
compute(b, picRetrieved[i]);
}

Related

Min and max button and label

I'm trying to build a exam grader using C#. I'm new to this and don't know very much. What code would I use to add min and max buttons and to add a label stating whether it's a min or max?
private void btnAdd_Click(object sender, EventArgs e)
{
int points;
try
{
points = int.Parse(txtPoints.Text);
lstPoints.Items.Add(points);
txtPoints.Clear();
txtPoints.Focus();
if (lstPoints.Items.Count == 12)
{
txtPoints.Enabled = false;
btnAdd.Enabled = false;
}
if (lblResult.Text != "")
{
lblResult.Text = "";
}
}
catch
{
MessageBox.Show("Please enter only whole numbers");
txtPoints.Clear();
txtPoints.Focus();
}
}
private void btnAvg_Click(object sender, EventArgs e)
{
double total = 0;
for (int i = 0; i < lstPoints.Items.Count; i++)
{
total += (int)lstPoints.Items[i];
}
total /= lstPoints.Items.Count;
lblResult.Text = total.ToString();
}
private void btnClear_Click(object sender, EventArgs e)
{
lstPoints.Items.Clear();
txtPoints.Enabled = true;
btnAdd.Enabled = true;
}
}
}
hope this works
private void getMax()
{
int max=0;
for (int i = 0; i < lstPoints.Items.Count; i++)
{
if(max<(int)lstPoints.Items[i])
{
max=(int)lstPoints.Items[i];
}
}
lblResult.Text = max.ToString();
}
}
private void getMin()
{
int min=(int)lstPoints.Items[0];
for (int i = 1; i < lstPoints.Items.Count; i++)
{
if(min>(int)lstPoints.Items[i])
{
min=(int)lstPoints.Items[i];
}
}
lblResult.Text = min.ToString();
}
}
There are two possiblities as I see:
1) When you are writing this:
lstPoints.Items.Add(points);
Instead of adding to List(Of Integer) use SortedList. So the
list will always have the sorted result sets.
2) Use Array.Sort() to sort the records.
Once you have sorted records the first one is the minimum and the last one is the maximum (Assuming sorted in ascending order).
Take out two buttons and placed on the form, set Text Property from property window to Min and Max respectively and in event handler handle the Click event and pick the relevant resultset from lstPoints array.
Hope it helps!

.net - How to add controls dynamically?

I want to add dynamic controls
Like the below image I want to do
I am struggling to do that
If I click Add More Experience button I want to display another rows
I tried with user control but it is not working properly.
Below code is working fine but if I add controls then close the browser page and then open the browser again added controls are coming.
I think the problem is static int i=0;
static int i = 0;
protected void addnewtext_Click(object sender, EventArgs e)
{
i++;
for (int j = 0; j <= i; j++)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
placeHolder.Controls.Add(ac);
placeHolder.Controls.Add(new LiteralControl("<BR>"));
}
}
Please provide your ideas? Thanks in advance
As per my comment, when using a static variable within an ASP.Net page, it will be shared amongst all users until the application pool or server is restarted.
Instead you should really be using a ViewState or similar to read/write the value.
private int controlCount
{
get
{
int val = 0;
try
{
val = (int)Page.ViewState["ControlCount"];
}
catch(Exception e)
{
// handle exception, if required.
}
return val;
}
set { Page.ViewState["ControlCount"] = value; }
}
protected void addnewtext_Click(object sender, EventArgs e)
{
int i = controlCount++;
for (int j = 0; j <= i; j++)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
placeHolder.Controls.Add(ac);
placeHolder.Controls.Add(new LiteralControl("<BR>"));
}
}

How can I learn which method runs during a for loop?

I have a for loop, and each time through I invoke the same method. I need to find a way to learn what the previous equation was. I have to find this without using the value of incrementation. for example:
for (int i = 0; i < 101; i++) {
checkBox[i].Click += new System.EventHandler(checkBoxMethod); }
Somehow checkBoxMethod should get the previous function in this case for instance:
checkBox[50].Click
Inside of the for loop, also set the tag of each checkbox. I am assuming you are using Windows Forms here. So, here is what the modified for loop would look like:
for (int i = 0; i < 101; i++) {
checkBox[i].Click += new System.EventHandler(checkBoxMethod);
checkBox[i].Tag = i;
}
Then in your event handler you can cast the sender variable to a checkbox like so:
void checkBoxMethod (object sender, EventArgs args)
{
CheckBox box = (CheckBox)sender;
int number = (int)box.Tag;
}
Whatever 'i' was at the time of creating that checkbox's event handler will be retrieved in the variable 'number' and you can use it however you want.
Instead of using a for loop, use recursion and pass the current count into the function:
void checkBoxMethod (object sender, EventArgs args)
{
CheckBox box = (CheckBox)sender;
int number = (int)box.Tag;
myRecursiveMethod(number);
}
private void myRecursiveMethod(int count)
{
//do whatever stuff I need to do
if (!timeToExitTheMethod)
myRecursiveMethod(count++);
}
You've given us no explanation of exactly what you are doing in your for loop and your question doesn't make a lot of sense (i.e. what are the CheckBoxes you allude to?), so I can't be very specific with my code example. Note that you'll have to code an exit point for the recursive method (otherwise it will be called until you get a stack overflow).
If you are simply looking to count the number of times a function has been called then do this:
public class MyClass
{
private int _myCounter = 0;
void checkBoxMethod (object sender, EventArgs args)
{
CheckBox box = (CheckBox)sender;
//do whatever you need to do
_myCounter++;
}
}
If you be more specific with your requirements then we can be more specific with our suggestions.
You can pass the required information to handler using lambda experssions.
for (int i = 0; i < n; i++) {
int number = i;
buttons[i].Click += (sender, args) => OnButtonClick(sender, args, number);
}
...
private void OnButtonClick(object sender, RoutedEventArgs e, int number) {
MessageBox.Show(number.ToString(CultureInfo.InvariantCulture));
}

C# checkbox

I have 3 check boxes in each row of 8 total rows. I want to have the third checkbox in each row to get checked only when the first two checkboxes are unchecked. I do not want to write a checkRow() method for each row.
What is the best way to go about it?
private void checkRow()
{
for (int i = 0; i < 8; i++)
{
var arraylist = new[] { checkbox1, checkbox2, checkbox3 };
if (checkbox1.Checked || checkbox2.Checked)
{
arraylist[2].Checked = false;
}
else
arraylist[2].Checked = true;
}
}
private void checbox1_CheckedChanged(object sender, EventArgs e)
{
checkRow();
}
private void checbox2_CheckedChanged(object sender, EventArgs e)
{
checkRow();
}
private void checbox3_CheckedChanged(object sender, EventArgs e)
{
checkRow();
}
In response.
private void checkRow()
{
var arraylist = new[] { checkEdit1, checkEdit2, checkEdit3 };
var arraylist1 = new[] { checkEdit4, checkEdit5, checkEdit6 };
var arraylist2 = new[] { checkEdit7, checkEdit8, checkEdit9 };
var array = new[] { arraylist, arraylist1, arraylist2 };
for (int i = 0; i < 8; i++)
{
//if checkedit1 or checkedit2 is checked the checkedit3 should not be checked
if (array[i]....Checked || array[i]....Checked)
{
arraylist[i]...Checked = false;
}
else
arraylist[i]...Checked = true;
}
}
I was trying to do something like this so that I dont have to write the checkRow() for each row
You should use the same method as the handler for all three delegates.
chkbox.CheckedChanged += new EventHandler(chkbox_CheckedChanged);
chkbox2.CheckedChanged += new EventHandler(chkbox_CheckedChanged);
chkbox3.CheckedChanged += new EventHandler(chkbox_CheckedChanged);
private void chkbox_CheckedChanged(object sender, EventArgs e)
{
// do your stuff here
}
Assuming you're not using a DataGridView or other way of organizing them into logical rows, why don't you do the following:
Store the checkboxes in an array so you have easy access to them.
CheckBox[,] checkArray = new CheckBox[8,3]...
Store the row index in the Tag property of the first and second checkboxes.
checkBox01.Tag = 0;
checkBox02.Tag = 0;
checkBox11.Tag = 1;
checkBox12.Tag = 1;
Have all the first and second checkboxes point to the same event handler:
checkBox01.CheckedChanged += new EventHandler(aCheckBox_CheckedChanged);
checkBox02.CheckedChanged += new EventHandler(aCheckBox_CheckedChanged);
checkBox11.CheckedChanged += new EventHandler(aCheckBox_CheckedChanged);
checkBox12.CheckedChanged += new EventHandler(aCheckBox_CheckedChanged);
In the event handler, you now know exactly which check box to update and no longer have to loop:
private void aCheckBox_CheckedChanged(object sender, EventArgs e)
{
int rowIndex = (int)((CheckBox)sender).Tag;
checkArray[rowIndex,2].Checked = !(checkArray[rowIndex,0].Checked ||
checkArray[rowIndex,1].Checked);
}
You can also do this using string lookups with the checkbox name, but it is surely slower and is a pain to refactor later if you choose to rename the checkboxes.

How can I get the array index of a given object in C# with control arrays?

I am dynamically adding a bunch of controls to a form. Each control calls the same method, and in that method I need to know the array index of the the control that performed the action.
CheckBox[] myCB = new CheckBox[100];
int i;
for (i = 0; i < 100; i++)
{
myCB[i] = new CheckBox();
myCB[i].Text = "Clicky!";
myCB[i].Click += new System.EventHandler(dynamicbutton_Click);
tableLayoutPanel1.Controls.Add(myCB[i]);
}
private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
label1.Text = sender.???array index property???.ToString();
}
So if I click myCB[42] label1 will read "42" Of course, if there is an easier way to handle dynamic controls I'd appreciate pointers.
private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
label1.Text = Array.IndexOf(myCB, (CheckBox)sender).ToString();
}
Control's should have a Tag property. Maybe you can attach the index to the Tag. You will incur boxing though...
int j = i;
myCB[i].Click += delegate(object sender, EventArgs e) {
// here you can use "j"
};
One obvious solution would be to set the tag:
CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
myCB[i] = new CheckBox();
myCB[i].Text = "Clicky!";
myCB[i].Click += new System.EventHandler(dynamicbutton_Click);
myCB[i].Tag = i;
tableLayoutPanel1.Controls.Add(myCB[i]);
}
Then:
private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
Control control = (Control) sender;
label1.Text = sender.Tag.ToString();
}
Another alternative is to capture the information in the event handler, most simply using a lambda expression or anonymous method:
CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
int index = i; // This is very important, as otherwise i will
// be captured for all of them
myCB[i] = new CheckBox();
myCB[i].Text = "Clicky!";
myCB[i].Click += (s, e) => label1.Text = index.ToString();
tableLayoutPanel1.Controls.Add(myCB[i]);
}
or for more complicated behaviour:
CheckBox[] myCB = new CheckBox[100];
for (int i = 0; i < myCB.Length; i++)
{
int index= i; // This is very important, as otherwise i will
// be captured for all of them
myCB[i] = new CheckBox();
myCB[i].Text = "Clicky!";
myCB[i].Click += (s, e) => DoSomethingComplicated(index, s, e);
tableLayoutPanel1.Controls.Add(myCB[i]);
}
(where you declare DoSomethingComplicated appropriately).

Categories

Resources