I have 3 listboxes,3 texboxes,3buttons in my window ,when i enter data from listbox1 into the textbox1 and click button or enter then the other 2 listbox items of same index should appear on the other 2 textboxes.
this is the code written till now
private void Get_Click(object sender, RoutedEventArgs e)
{
int x = listbox1.SelectedIndex;
listbox2.SelectedIndex = x;
listbox3.SelectedIndex = x;
ListBoxItem lb1 = (listbox1.SelectedItem as ListBoxItem);
tb1.Text = lb1.Content.ToString();
ListBoxItem lb2 = (listbox2.SelectedItem as ListBoxItem);
tb2.Text = lb2.Content.ToString();
ListBoxItem lb3 = (listbox3.SelectedItem as ListBoxItem);
tb3.Text = lb3.Content.ToString();
}
private void Add_Click(object sender, RoutedEventArgs e)
{
int x = listbox1.SelectedIndex;
listbox2.SelectedIndex = x;
listbox3.SelectedIndex = x;
listbox1.Items.Add(tb1.Text);
listbox2.Items.Add(tb2.Text);
listbox3.Items.Add(tb3.Text);
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
int x = listbox1.SelectedIndex;
listbox2.SelectedIndex = x;
listbox3.SelectedIndex = x;
listbox1.Items.RemoveAt(listbox1.Items.IndexOf(listbox1.SelectedItem));
listbox2.Items.RemoveAt(listbox2.Items.IndexOf(listbox2.SelectedItem));
listbox3.Items.RemoveAt(listbox3.Items.IndexOf(listbox3.SelectedItem));
}
It's not really clear what you're after, but I think you're trying to get the text in tb1 to be the driver instead of selecting from listbox1. I've added some simple code to the Get_Click event:
private void Get_Click(object sender, RoutedEventArgs e)
{
foreach (ListBoxItem lbi in listbox1.Items) //new code
{ //new code
if (lbi.Content.ToString() == tb1.Text) //new code
{ //new code
lbi.IsSelected = true; //new code
break; //new code
} //new code
} //new code
int x = listbox1.SelectedIndex;
listbox2.SelectedIndex = x;
listbox3.SelectedIndex = x;
//ListBoxItem lb1 = (listbox1.SelectedItem as ListBoxItem); //updated
//tb1.Text = lb1.Content.ToString(); //updated
ListBoxItem lb2 = (listbox2.SelectedItem as ListBoxItem);
tb2.Text = lb2.Content.ToString();
ListBoxItem lb3 = (listbox3.SelectedItem as ListBoxItem);
tb3.Text = lb3.Content.ToString();
}
Is this the kind of thing you're after?
Related
I have an action for my button in c# Winform like this:
private void btnAction_Click(object sender, EventArgs e)
{
TextBox tbxdg = new TextBox();
tbxdg.Name = "tbx_DG" + cx.ToString();
tbxdg.Location = new Point(508, 12 + (40 * cx));
tbxdg.Size = new Size(200, 24);
tbxdg.Font = new Font("Tahoma", 10);
panel2.Controls.Add(tbxdg);
cx++;
}
Now I want to get text from the textbox that i've created by clicking my button. I've tried call the textbox by the name that i given to it in the button click action but it's not working.
u can try this:
var textBoxText = panel2.Controls.Find("name of textbox", false).First().Text;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int cx = 0;
private void button1_Click(object sender, EventArgs e)
{
TextBox tbxdg = new TextBox();
tbxdg.Name = "tbx_DG" + cx.ToString();
tbxdg.Location = new Point(0, 0 + (40 * cx));
tbxdg.Size = new Size(200, 24);
tbxdg.Font = new Font("Tahoma", 10);
panel1.Controls.Add(tbxdg);
cx++;
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = string.Empty;
foreach (TextBox tb in panel1.Controls)
{
label1.Text += $"{tb.Name} - {tb.Text}\n";
}
}
}
Demo
Instead of searching the control name in the panel, another approach is to add all the dynamic text boxes to global List<TextBox>
Please read comments inside the example:
private int cx;
private List<TextBox> DynamicTextBoxesList = new List<TextBox>();
private void btnAction_Click(object sender, EventArgs e)
{
TextBox tbxdg = new TextBox();
tbxdg.Name = "tbx_DG" + cx.ToString();
tbxdg.Location = new Point(508, 12 + (40 * cx));
tbxdg.Size = new Size(200, 24);
tbxdg.Font = new Font("Tahoma", 10);
panel2.Controls.Add(tbxdg);
// add to list
DynamicTextBoxesList.Add(tbxdg);
cx++;
}
// button event for example how to use DynamicTextBoxesList
private void btnExampleFoaccesingTextBoxes_Click(object sender, EventArgs e)
{
if (DynamicTextBoxesList.Count > 0)
{
foreach (TextBox t in DynamicTextBoxesList)
{
MessageBox.Show(t.Text);
}
// or you can find by name for example you need cx=1:
var txtbox = DynamicTextBoxesList.Where(x => x.Name == "tbx_DG1").FirstOrDefault();
if (txtbox != null)
{
MessageBox.Show(txtbox.Text);
}
}
}
Any idea how to add a mouse click event to generated Textbox fields. Here is the code for generating TextBox fields:
private void button1_Click(object sender, EventArgs e)
{
TextBox textbox = new TextBox();
textbox.Location = new Point(60, 25 * count);
textbox.Size = new Size(301, 20);
textbox.Name = "textbox_" + (count + 1);
textbox.TextChanged += new System.EventHandler(this.TextBox_Changed);
panel1.Controls.Add(textbox);
count++;
if (count == 4)
{
MessageBox.Show("");
button1.Enabled = false;
}
}
The code below must be in the method that handles the mouse click event for every generated Textbox field:
TextBox txtName = (TextBox)this.Controls.Find("textbox_1", true)[0];
TextBox txth = (TextBox)this.Controls.Find("textbox_2", true)[0];
if (txtName != null)
{
}
I think you are looking for something like this:
int count = 0;
private void button1_Click(object sender, EventArgs e)
{
TextBox textbox = new TextBox();
textbox.Location = new Point(60, 25 * count);
textbox.Size = new Size(301, 20);
textbox.Name = "textbox_" + (count + 1);
textbox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseClick);
textbox.TextChanged += new System.EventHandler(this.TextBox_Changed);
panel1.Controls.Add(textbox);
count++;
if (count == 4)
{
MessageBox.Show("");
button1.Enabled = false;
}
}
private void TextBox_MouseClick(object sender, MouseEventArgs e)
{
TextBox txtName = (TextBox)this.Controls.Find("textbox_1", true)[0];
TextBox txth = (TextBox)this.Controls.Find("textbox_2", true)[0];
if (txtName != null)
{
MessageBox.Show("Test");
}
}
When I preform a click on a TextBox this MessageBox window appears:
I would also change the TextBox_MouseClick method to :
private void TextBox_MouseClick(object sender, MouseEventArgs e)
{
Control[] txtName = this.Controls.Find("textbox_1", true);
Control[] txth = this.Controls.Find("textbox_2", true);
if ((TextBox)txtName[0] != null)
{
MessageBox.Show("Test");
}
}
Because if you create only one TextBox and perform a click on it you will get a System.IndexOutOfRangeException because the second TextBox isn't created so the array that holds you second TextBox is empty.
I have a lot of buttons in flow layout panel. I created these buttons programmatically.
But I didn't if click button its color will be light green after second click will be reset color. I wrote this handler for flow layout panel. How can I do second click will clear button color:
private void Form1_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append( "3 x coca cola 33 cl");
sb.Append(Environment.NewLine);
sb.Append("DIN");
sb.Append("\t\t");
sb.Append("200");
//button2.Text = sb.ToString();
string products = "30000|3;3110002;Cola;400;150;100;1;1000|3;3110003;Snickers;400;150;100;1;1000|3;3110004;NesttleCrunch;400;150;100;1;1000|3;3110005;Marlbora;400;150;100;1;1000|3;3110006;Orange;400;150;100;1;1000|3;3110007;Milk;400;150;100;1;1000|3;3110008;Water;400;150;100;1;1000|3;3110009;Banana;400;150;100;1;1000|3;3110010;Honey;400;150;100;1;1000|3;3110011;Beer;400;150;100;1;1000|3;3110012;Hazelnut;400;150;100;1;1000|3;3110013;RedBull;400;150;100;1;1000|3;3110014;ChewingGum;400;150;100;1;1000|3;3110015;Apple;400;150;100;1;1000";
string[] listproducts = products.Split('|');
lblAvaliablePoint.Text = listproducts[0];
for (int i = 1; i < listproducts.Count();i++ )
{
string[] perproduct = listproducts[i].Split(';');
Button newButton = new Button();
newButton.Size = new System.Drawing.Size(170, 87);
newButton.BackColor = Color.LightGray;
newButton.UseVisualStyleBackColor = false;
newButton.Tag =perproduct[1];
newButton.Text = perproduct[0] + "x" + perproduct[2];
newButton.Click += new EventHandler(ButtonClickHandler);
flowLayoutPanel1.Controls.Add(newButton);
}
flowLayoutPanel1.VerticalScroll.Maximum = flowLayoutPanel1.Height+40;
flowLayoutPanel1.VerticalScroll.LargeChange = 30;
}
public void ButtonClickHandler(Object sender,EventArgs e)
{
((Button)sender).BackColor = Color.LightGreen;
}
What about add simple if condition into button handler like this?
public void ButtonClickHandler(object sender, EventArgs e)
{
if (((Button)sender).BackColor == Color.LightGreen)
{
((Button)sender).BackColor = Color.White; // Your default color
}
else
{
((Button)sender).BackColor = Color.LightGreen;
}
}
Change your ButtonClickHandler
public void ButtonClickHandler(Object sender, EventArgs e)
{
var currentButton = ((Button)sender);
if(currentButton != null)
{
currentButton.BackColor = currentButton.BackColor == Color.LightGreen ? Color.LightGray : Color.LightGreen;
}
}
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 want to attach an event to a button clicked generated at runtime. Till this point I've wrote the code, but can't pass the button's ID to the method. Here is my code
This code does not through any error, another problem is after the click event the controls get washed away. How to prevent this ?
protected void Button1_Click(object sender, EventArgs e)
{
int i = int.Parse(TextBox1.Text);
for (int x = 1; x <= i; x++)
{
Button b = new Button();
b.ID = "btn_" + x.ToString();
b.Text = "btn_" + x.ToString();
b.Click += new System.EventHandler(myEventHandler);
pnlHolder.Controls.Add(b);
}
}
private void myEventHandler(object sender, EventArgs e)
{
txtMain.Text = sender.ToString(); // I want to know which button was pressed
}
try,
txtMain.Text = (sender as Button).Name;
or
txtMain.Text = (sender as Button).Text;
Try this
private void myEventHandler(object sender, EventArgs e)
{
Button b = (Button) sender;
txtMain.Text = b.ID;
//
txtMain.Text = b.Text;
if(b.ID == "button1")
doThis();
else if(b.ID == "button2")
doThat();
}