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);
}
Related
in relation to the post UWP C# Add Button Dynamically and Organizing On StackPanel I have additional questions
how do I control these dynamically created button(s)' event? eg. button 1 to turns on LED 1, button 2 to turns on LED 2 etc.
how to selectively remove the button(s) and reorganize the remaining buttons with no empty spaces in between.
Thank you.
Update:
I have a routine to add the client with details such as client IP etc. from the client and to add and display them in a scrollviewer.
How do i link either the clientname or client ip to the dictionary?
private async void AddClientList()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
ClientListUserControl clientListControl = new ClientListUserControl(this, new ClientList(clientName, receiveIP, DateTime.Now, receivePort, receiveService, receiveDEV, receiveSTS, receiveACT));
ClientList_Panel.Children.Add(clientListControl);
clientListControl.updateDisplay();
});
}
You can also use Tag property of Button to pass the parameter. This property is inherited from FrameworkElement, and generally it is used to get or set an arbitrary object value that can be used to store custom information about this object.
Please refer to following code.
private void ButtonCreateNewButton_Click(object sender, RoutedEventArgs e)
{
Button b = new Button();
b.Height = 30;
b.Width = 100;
b.VerticalAlignment = VerticalAlignment.Top;
b.HorizontalAlignment = HorizontalAlignment.Left;
b.Margin = new Thickness(6, 6, 6, 6);
b.Content = "Button " + buttonCounter;
b.Tag = "LED-" + buttonCounter;
b.Click += Button_Click;
....
buttonCounter++;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
var led = btn.Tag;
//use led_name as a parameter here, according with this variable to turn on the LED
TurnOnOffLed(led);
}
To your first question:
To handle this, you should introduce a dictionary, where the button is the key and your value is the client. So you can get the linked client in the ClickHandler.
public Dictionary<Button, object> clientDict = new Dictionary<Button, object>();
Note: Here the type of the client is object, because I don`t know what type you have!
You add the button inside of your AddButton routine. Again: I dont know where you get your client from, so I added the value null. Change this to fulfil your requirements. Then you add another ClickHandler and get the linked client:
b.Click += HandleButtonClick;
clientDict.Add(b, null);
private void HandleButtonClick(object sender, RoutedEventArgs e)
{
//Execute whatever you want from your client:
var client = clientDict[sender as Button];
}
To your second question:
You need to add a RemoveMethod, where you get the column and row of the button, which should be deleted. Afterwards you can manipulate all other buttons column and row property. To avoid, that a new added button is not aligned to the others, you need to change the add-process, to make the position of the new button depending on the number of elements in your dictionary. Here an example how the full code could look like:
public int buttonCounter = 1;
public Dictionary<Button, object> clientDict = new Dictionary<Button, object>();
private void RemoveBtn(Button button)
{
var row = Grid.GetRow(button);
var column = Grid.GetColumn(button);
//Rearange
foreach (var btn in clientDict.Keys)
{
var r = Grid.GetRow(btn);
var c = Grid.GetColumn(btn);
if (c > column || (c == column && r > row))
{
if (r != 0)
{
//Set the row new
Grid.SetRow(btn, r - 1);
}
else
{
//Need to set it to a new column
Grid.SetRow(btn, 3);
Grid.SetColumn(btn, c - 1);
}
}
}
myGrid.Children.Remove(button);
clientDict.Remove(button);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//Create the button
Button b = new Button();
b.Height = 30;
b.Width = 100;
b.VerticalAlignment = VerticalAlignment.Top;
b.HorizontalAlignment = HorizontalAlignment.Left;
b.Margin = new Thickness(20, 20, 0, 0);
b.Content = "Button " + buttonCounter;
b.Click += HandleButtonClick;
clientDict.Add(b, null);
//Calculate the place of the button
int column = (int)(clientDict.Count / 4);
int row = clientDict.Count % 4;
//Check if you need to add a columns
if (row == 0 && myGrid.ColumnDefinitions.Count <= column)
{
ColumnDefinition col = new ColumnDefinition();
col.Width = new GridLength(column, GridUnitType.Auto);
myGrid.ColumnDefinitions.Add(col);
}
//Add the button
myGrid.Children.Add(b);
Grid.SetColumn(b, column);
Grid.SetRow(b, row);
buttonCounter++;
}
private void HandleButtonClick(object sender, RoutedEventArgs e)
{
//Execute whatever you want from you handler:
var client = clientDict[sender as Button];
}
Note: The rearrange process is not performance optimized.
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;
I have tried creating textboxes dynamically using lists. All i need now is, how can i reset all text boxes that i have created by hitting a reset button.
The following is my code:
public void button2_Click_1(object sender, EventArgs e)
{
int number = Convert.ToInt32(textBox2.Text);
List<TextBox> inputTextBoxes;
inputTextBoxes = new List<TextBox>();
for (int i = 1; i <= number; i++)
{
Label labelInput = new Label();
TextBox textBoxNewInput = new TextBox();
labelInput.Text = "Activity No: " + i;
labelInput.Location = new System.Drawing.Point(30, textBox2.Bottom + (i * 40));
labelInput.AutoSize = true;
textBoxNewInput.Location = new System.Drawing.Point(labelInput.Width+60, labelInput.Top - 3);
inputTextBoxes.Add(textBoxNewInput);
this.Controls.Add(labelInput);
this.Controls.Add(textBoxNewInput);
}
}
The answer is:
private void resetButton_Click(object sender,EventArgs e)
{
for (int i = 0; i <= inputTextBoxes.Length; i++)
{
inputTextBoxes[i].Text = "";
}
}
And you should declare inputTextBoxes is a class member which is same class' of buttons.
Move the following line outside the event handler function (outside the function but inside the class)
List<TextBox> inputTextBoxes;
Then on the reset button click
private void btnReset_Click(object sender, EventArgs e)
{
foreach(TextBox txt in inputTextBoxes)
{
this.Controls.Remove(txt);
}
inputTextBoxes.Clear();
}
Edit: Corrected the class type in foreach loop (from Button to TextBox)
I have added check boxes dynamically to a panel. now how can i get a alert message with "you have checked 1 or 2 or 3....". when check boxes get selected??
CheckBox[] premiumticket = new CheckBox[50];
private void Form1_Load(object sender, EventArgs e)
{
var panel1 = new Panel()
{
Size = new Size(600, 70),
Location = new Point(20, 130),
BorderStyle = BorderStyle.FixedSingle
};
for (int i = 0; i < 20; i++)
{
premiumticket[i]=new CheckBox();
premiumticket[i].Text=(i+1).ToString();
premiumticket[i].Name=(i+1).ToString();
premiumticket[i].Location=new Point(x,y);
panel1.Controls.Add(premiumticket[i]);
x = x - 55;
if (x < 55)
{
y = y + 20;
x = 550;
}
}
x = 550; y = 10;
Controls.Add(panel1);
}
Add an event-handler to each CheckBox:
public void Checkbox_CheckedChanged(Object sender, EventArgs e) {
CheckBox cb = (CheckBox)sender;
MessageBox.Show( cb.Name + " was clicked!");
}
for (int i = 0; i < 20; i++) {
premiumticket[i] = new CheckBox();
premiumticket[i].OnCheckChanged += new EventHandler( Checkbox_CheckedChange );
...
}
Dynamically Create events for Each Checkbox and put ur alert code on it
var checkBox = new CheckBox { ID = "WCCheckBox" + item.ItemID.ToString(), ItemID = item.ItemID, Checked = item.UserValue == "1", CssClass = "wounditem" };
Your implementation may be another.
Add this to your for loop:
premiumticket[i].OnCheckChanged += new EventHandler( premiumTicketChanged );
Checkbox toggled handler:
public void premiumTicketChanged (Object sender, EventArgs e)
{
int ticketCount = premiumticket.Count(c => c.Checked);
MessageBox.Show( string.Format("You have checked {0} checkboxes....", ticketCount));
}
After you add dynamically a control you can use AddHandler to manage the events of this control.
Remember set the checkbox autopostback property to true.
i'm designing a windows form application. In this app, user selects a number from a combobox, then depending on the number, some dynamic controls will be created(labels and comboboxes).
My problem is, i need to write some code on these dynamically created comboboxs' "selectedindexchanged" event. But i don't know how to create an event to dynamic combobox.
Here is my function:
FORM1.CS
public void getchildCntrl(Panel pnl,ComboBox cmbb)
{
for (int ix = pnl.Controls.Count - 1; ix >= 0; ix--)
if (pnl.Controls[ix].Name.Substring(0, 5) == "Child") pnl.Controls[ix].Dispose();
if (cmbb.SelectedIndex != 0)
{
Label[] childLabels = new Label[cmbb.SelectedIndex];
ComboBox[] txtTeamNames = new ComboBox[cmbb.SelectedIndex];
for (int i = 0; i < txtTeamNames.Length; i++)
{
//label create
var lbl = new Label();
childLabels[i] = lbl;
lbl.Name = "ChildLb" + i.ToString();
lbl.Text = (i + 1).ToString() + ". Çocuk-Yaş :";
lbl.Width = 80;
lbl.Location = new Point(cmbb.Location.X - 85, cmbb.Location.Y + 7 + ((i + 1) * 28));
lbl.Visible = true;
pnl.Controls.Add(lbl);
//combobox create
var cmb = new ComboBox();
txtTeamNames[i] = cmb;
cmb.Name = "Child" + i.ToString();
cmb.Location = new Point(cmbb.Location.X, cmbb.Location.Y + 5 + ((i + 1) * 28));
cmb.Width = 40;
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
cmb.DataSource = ages.ToArray();
cmb.Visible = true;
pnl.Controls.Add(cmb);
}
}
}
You just register to the event like below...
cmb.SelectedIndexChanged += new System.EventHandler((object o, EventArgs e) =>
{
//Do something here
});
Or
cmb.SelectedIndexChanged += new System.EventHandler(cmb_SelectedValueChanged);
private void cmb_SelectedValueChanged(object sender, EventArgs e)
{
//Do something here.
}
You can hook up an event handler to the ComboBox like this:
cmd.SelectionChanged += new SelectionChangedEventHandler(GuiController_SelectionChanged);
void GuiController_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
throw new NotImplementedException();
}
Register event handler this way:
cmb.SelectedIndexChanged+=new EventHandler(cmb_SelectedIndexChanged);
Unregister this way:
cmb.SelectedIndexChanged-=new EventHandler(cmb_SelectedIndexChanged);
private void cmb_SelectedIndexChanged(object sender, System.EventArgs e)
{
//write your event code here
}
How to: Create Event Handlers at Run Time for Windows Forms
public void getchildCntrl(Panel pnl,ComboBox cmbb)
{
//// your code.....
//combobox create
var cmb = new ComboBox();
cmb.SelectedIndexChanged+=new EventHandler(cmb_SelectedIndexChanged);
// remaining code
cmb.Visible = true;
pnl.Controls.Add(cmb);
}
}
}
For specifying parameters - go through:
ComboBox.SelectedIndexChanged Event