How to add buttons with independent click event - c#

I want to add buttons and textboxes dynamically on runtime with each
button react differently.
ie newbutton1 linked with texbox1 ,newbutton2linked withtextbox2`
Right now any button just prints from the first to the last textbox
one after the other.
Also consider that I have a button1 & textbox1 already on the form for guides
Here is my code :
List<Button> buttons = new List<Button>();
List<TextBox> textboxes = new List<TextBox>();
int NumTextBox = 0;
void click(object sender, EventArgs e)
{
MessageBox.Show(textboxes[NumTextBox].Text);
NumTextBox++;
}
int x = 0;
int y = 0;
void AddClick(object sender, EventArgs e)
{
Button newButton = new Button();
buttons.Add(newButton);
newButton.Click += click;//
// newButton.Location.Y = button1.Location.Y + 20;
newButton.Location = new Point(button1.Location.X, button1.Location.Y+25+x);
x += 25;
this.Controls.Add(newButton);
TextBox newTextBox = new TextBox();
textboxes.Add(newTextBox);
// newTextBox.Click += click;
newTextBox.Location = new Point(textBox1.Location.X, textBox1.Location.Y+25+y);
y += 25;
this.Controls.Add(newTextBox);
}

you can have a class like mybutton that inherits from button class, in this new class you can have a property with textbox type . just like following code . and in your code when you want to Instantiated Button you can use list<mybutton> and set linkedTextbox property with a textbox.
public class myButton:Button
{
...
public TextBox linkedTextBox{set;get;}
}
and in your code you should write some thing like this :
list<myButton> buttons=new list<myButton>();
Textbox someTextBox=new TextBox();
buttons[0].linkedTextbox=someTextBox;
and in your event you can use:
((myButton)sender).linkedTextBox.text="Some thing";

Thank you everyone , I followed #Franck's answer .So WHAT CHANGED :
I've deleted the pre-made button1 & textbox1 and add them
programatically on the Form_load so that I can add them in the
Lists
A proof screenshot : http://prntscr.com/aprqxz
CODE:
List<Button> buttons = new List<Button>();
List<TextBox> textboxes = new List<TextBox>();
Button button1 = new Button();
TextBox textBox1 = new TextBox();
int x = 0;
int y = 0;
void click(object sender, EventArgs e)
{
var txt = textboxes[Convert.ToInt32(((Button)sender).Tag)].Text;
MessageBox.Show(txt.ToString());
}
void AddClick(object sender, EventArgs e)
{
Button newButton = new Button();
newButton.Click += click;
newButton.Location = new Point(button1.Location.X, button1.Location.Y+25+x);
x += 25;
newButton.Tag = buttons.Count;
this.Controls.Add(newButton);
buttons.Add(newButton);
//
TextBox newTextBox = new TextBox();
newTextBox.Location = new Point(textBox1.Location.X, textBox1.Location.Y+25+y);
y += 25;
this.Controls.Add(newTextBox);
textboxes.Add(newTextBox);
}
void MainFormLoad(object sender, EventArgs e)
{
button1.Click += click;
button1.Location = new Point(55, 48);
button1.Tag = buttons.Count;
this.Controls.Add(button1);
buttons.Add(button1);
//
textBox1.Location = new Point(137, 50);
this.Controls.Add(textBox1);
textboxes.Add(textBox1);
}
EDIT 1: As the counting starts from 0 I didn't added newButton.Tag = buttons.count+1; I added just newButton.Tag = buttons.count;

Related

Button_Clicked Event manipulate a specific Element (pure C# (Page.cs))

I want to create a lot of Labels and Buttons and to make this task easier and more dynamic I initialized these in a for loop in a pure C# Page (Page.cs).
How can I manipulate a specific Label with a Button_Clicked Event (in my case the Label with the index i, which has been created together with the Button in the for loop)?
Here is a summary of my problem:
// Constructor
public Page()
{
Label[] lb = new Label[10];
Button[] btn = new Button[lb.Length];
for (int i = 0; i < lb.Length; i++)
{
lb[i] = new Label();
btn[i] = new Button();
btn[i].Clicked += Button_Clicked;
// TODO Somehow reference the Label in the Button Clicked Method
}
// set Content ...
}
// Buton Clicked Method
private void Button_Clicked(object sender, EventArgs e)
{
lb[i].Text = "Hello World!";
// lb[i] should be the Label, which was initialized with the Button in the for loop
}
or is there a better way, to achieve my goal?
You may manipulate a specific Label with a Button_Clicked Event by wrapping a pair of button and label into one StackLayout and adding a ClassId property to each StackLayout.
The sample code is like this:
public partial class MainPage : ContentPage
{
Label[] lb;
Button[] btn;
StackLayout[] sl;
int i = 0;
public MainPage()
{
InitializeComponent();
sl = new StackLayout[10];
lb = new Label[10];
btn = new Button[lb.Length];
StackLayout content = new StackLayout();
for (i = 0; i < lb.Length; i++)
{
lb[i] = new Label()
{
Text = "Old Text"+i,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Center
};
btn[i] = new Button
{
Text = "Click to Change Label Text!"+i,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Center,
};
btn[i].Clicked += (sender, e) => Button_Clicked(sender, e);
// TODO Somehow reference the Label in the Button Clicked Method
sl[i] = new StackLayout
{
ClassId = ""+i,
Children =
{
lb[i],
btn[i]
}
};
content.Children.Add(sl[i]);
}
Content = content;
}
// Buton Clicked Method
private void Button_Clicked(object sender, EventArgs e)
{
Button bt = sender as Button;
StackLayout layout = bt.Parent as StackLayout;
int index = Int32.Parse(layout.ClassId);
lb[index].Text = "Hello World!" + index;
}
}
The result is like this:
If you only have 10 labels, I would just use lambdas.
Label[] lb = new Label[10];
Button[] btn = new Button[lb.Length];
for (int i = 0; i < lb.Length; i++)
{
lb[i] = new Label();
btn[i] = new Button();
btn[i].Clicked += (s,e) => { lb[i].Text = "hello world" };
}
One of the ways to achieve this is to modify event handler during the registration.
for (int i = 0; i < lb.Length; i++)
{
lb[i] = new Label();
btn[i] = new Button();
btn[i].Clicked += (sender, e) => Button_Clicked(sender, e, i);
//Here i is to pass the index to event handler Button_Clicked;
}
And now, you could access index in button clicked event handler and you could access the label with the index passed.

How to add a click event to dynamically created labels

Label[] l1 = new Label[30];
DataTable dt = ConsoleApp2.CitiesDB.getCities(this.region);
foreach(DataRow row in dt.Rows)
{
count++;
string city = row.Field<string>("city");
l1[count] = new Label();
l1[count].Location = new System.Drawing.Point(x,y);
l1[count].Size = new Size(140, 80);
l1[count].Font = new System.Drawing.Font("Century Gothic", 8.5F);
l1[count].Text = city;
x = x + 260;
this.Controls.Add(l1[count]);
this.Refresh();
if(count == 4 || count %4 ==0)
{
y = y + 150;
x = 40;
}
//l1[count].Click += new EventHandler(l1_click);
}
So i made a dynamic labels (each label is a city name). how can i make each label clickable? i have a register form - what i want is if user clicks on "new york" then in the "city" textbox it will appear. the way with the EventHandler doesnt works for me);. what can i do?
what i mean in code that doesnt work:
protected void l1_click(object sender, EventArgs e)
{
RegisterForm register = new RegisterForm(username,email,password,address,phone);
register.state.Text = region;
register.city.Text = l1[count].Text;
register.Show();
}
thanks (:
Assign one click event for each label (dynamically created):
l1[count].Click += l1_click;
In the one click event use the sender argument to see which label was clicked on:
protected void l1_click(object sender, EventArgs e)
{
Label lbl = (Label)sender;
MessageBox.Show(lbl.Text);
}

How to dynamically create buttons and add events to them?

I have array of buttons, and array of labels:
Label[] labels = new Label[10];
Button[] but = new Button[10];
While clicking the other button I want to dynamically create new button and new label from the array, i also want the but[i] to change the tex of labels[i]:
private void button1_Click(object sender, EventArgs e)
{
labels[i] = new Label();
labels[i].Location = new System.Drawing.Point(0, 15+a);
labels[i].Parent = panel1;
labels[i].Text = "Sample text";
labels[i].Size = new System.Drawing.Size(155, 51);
labels[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
a = labels[i].Height + labels[i].Top;
but[i] = new Button();
but[i].Text = "-";
but[i].Location = new System.Drawing.Point(0, labels[i].Height + labels[i].Top);
but[i].Parent = panel1;
but[i].Size = new System.Drawing.Size(155, 10);
but[i].Click += new System.EventHandler(but_Click);
i++;
}
private void but[i]_Click(object sender, EventArgs e)
{
labels[i].Text = "Changed Text";
}
But apparently I can't put an array in an event handler, how should I do it then?
One way to do this is to make your method return a handler instead of being a handler:
private EventHandler but_Click(int i)
{
return (s, e) => labels[i].Text = "Changed Text";
}
And use it like:
but[i].Click += but_Click(i);
Or do it inline:
but[i].Click += (s, ea) => labels[i].Text = "Changed Text";
What's happening in either of these is some compiler magic to capture the i variable. It's equivalent to this (which is also a valid, if verbose, way to do it):
class MyWrapper {
private int i;
public MyWrapper(int i) {
this.i = i;
}
public void TheHandler(object sender, EventArgs e) {
// TODO: capture the object that owns `labels` also, or this won't work.
labels[i].Text = "Changed Text";
}
}
//call with
but[i].Click += new EventHandler(new MyWrapper(i).TheHandler);
You could add the array index to the button as Tag property, and then pull it back out in but_Click.
So, add
but[i].Tag = i;
to the button creation. And then change the event handler:
private void but_Click(object sender, EventArgs e)
{
int buttonIndex = (int)((Button)sender).Tag;
labels[buttonIndex].Text = "Changed Text";
}
Or put the event handler inline:
but[i].Click += (s,e) => { label[i].Text = "Changed Text"; }
Or another option using the Tag property, add:
but[i].Tag = label[i];
...
private void but_Click(object sender, EventArgs e)
{
Label label = (Label)((Button)sender).Tag;
label.Text = "Changed Text";
}
Advantage of this approach is you're not relying on keeping arrays in synch after the initial creation of the controls.
I guess this is self-explaining:
public void SomeMehthod()
{
Button btn1 = new Button();
Button btn2 = new Button();
Button btn3 = new Button();
// Your button-array
Button[] btns = new Button[]
{
btn1,
btn2,
btn3
};
foreach(Button btn in btns)
{
// For each button setup the same method to fire on click
btn.Click += new EventHandler(ButtonClicked);
}
}
private void ButtonClicked(Object sender, EventArgs e)
{
// This will fire on any button from the array
// You can switch on the name, or location or anything else
switch((sender as Button).Name)
{
case "btn1":
// Do something
break;
case "btn2":
// Do something
break;
case "btn3":
// Do something
break;
}
}
Or if your array is accessible globaly:
Button[] btns = new Button[5];
Label[] lbls = new Label[5];
private void ButtonClicked(Object sender, EventArgs e)
{
Button clicked = sender as Button;
int indexOfButton = btns.ToList().IndexOf(clicked);
// ..IndexOf() returns -1 if nothign is found
if(indexOfButton > 0)
{
lbls[indexOfButton].DoWhatYouWant...
}
}

C# Button visible change when button cliked

i tried to do this(mine field) but i cant visible = false any button when im clicked that.
by the way this.visible = false is going to visible form -.-
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(541, 537);
for (int j = 24; j < ClientSize.Height; j += 25)
{
for (int i = 0; i < ClientSize.Width; i += 25)
{
Button btn = new Button();
btn.Width = 25;
btn.Height = 25;
btn.Location = new Point(i, j);
btn.Click += btn_Click;
Controls.Add(btn);
}
}
}
void btn_Click(object sender, EventArgs e)
{
//When i clicked anyone of them that comes be invisible,
}
thx for helping
The control that fired the event is stored in sender - cast it back to a Button.
void btn_Click(object sender, EventArgs e)
{
var button = (Button)sender;
button.Hide();
}
Alternatively, if that's all you're doing in the event, you could define it in a single line like this:
btn.Click += delegate { btn.Hide(); };

How to assign click event

I have an array of button which is dynamically generated at run time. I have the function for button click in my code, but I can't find a way to set the button's click name in code. So,
what is the code equivalent for XAML:
<Button x:Name="btn1" Click="btn1_Click">
Or, what should I place for "????" in the following Code:
Button btn = new Button();
btn.Name = "btn1";
btn.???? = "btn1_Click";
Button btn = new Button();
btn.Name = "btn1";
btn.Click += btn1_Click;
private void btn1_Click(object sender, RoutedEventArgs e)
{
// do something
}
The following should do the trick:
btn.Click += btn1_Click;
// sample C#
public void populateButtons()
{
int xPos;
int yPos;
Random ranNum = new Random();
for (int i = 0; i < 50; i++)
{
Button foo = new Button();
Style buttonStyle = Window.Resources["CurvedButton"] as Style;
int sizeValue = ranNum.Next(50);
foo.Width = sizeValue;
foo.Height = sizeValue;
foo.Name = "button" + i;
xPos = ranNum.Next(300);
yPos = ranNum.Next(200);
foo.HorizontalAlignment = HorizontalAlignment.Left;
foo.VerticalAlignment = VerticalAlignment.Top;
foo.Margin = new Thickness(xPos, yPos, 0, 0);
foo.Style = buttonStyle;
foo.Click += new RoutedEventHandler(buttonClick);
LayoutRoot.Children.Add(foo);
}
}
private void buttonClick(object sender, EventArgs e)
{
//do something or...
Button clicked = (Button) sender;
MessageBox.Show("Button's name is: " + clicked.Name);
}
I don't think WPF supports what you are trying to achieve i.e. assigning method to a button using method's name or btn1.Click = "btn1_Click". You will have to use approach suggested in above answers i.e. register button click event with appropriate method
btn1.Click += btn1_Click;
You should place below line
btn.Click = btn.Click + btn1_Click;

Categories

Resources