I'm trying to have panel displaying the result differently each time a user select items from pre-loaded combobox and dynamically created combobox.
Initially it will load a comboBox with item of ("HelloWorld"), each time when I do a SelectedIndexChanged with "HelloWorld", the panel will show 1.
However, problem lies on whenever I hit on add button and do SelectedIndexChanged with "HelloWorld" on the newly created button. It simply doesn't show 2 but instead when I hit on pre-loaded comboBox, it show 3.
Is it something to do with life-cycle events?
class form{
int index = 0;
private void formMain_Load(object sender, EventArgs e)
{
Button add = new Button();
panel.Controls.Add(search());
add.Click += new EventHandler((object o, EventArgs e) => { panel.Controls.Add(search()); });
panel.Controls.Add(add);
}
public ComboBox search()
{
ComboBox searchField = new ComboBox();
searchField.Items.Add("HelloWorld");
searchField.SelectedIndexChanged += new EventHandler((object io, EventArgs ie) =>
{
index++;
Label display = new Label();
display.Text = index.ToString();
panel.Controls.Add(display);
});
return searchField;
}
}
I have tried many days and couldn't understand it ... Any helps would be appreciated. Thanks
int index { get; set; }
public Form2()
{
InitializeComponent();
index = 0;
Button add = new Button();
add.Text = "Add";
add.Location = new Point(search().Location.X + search().Width + 10, search().Location.Y);
panel.Controls.Add(search());
add.Click += new EventHandler((object o, EventArgs e) => {
panel.Controls.Add(search());
});
panel.Controls.Add(add);
}
public ComboBox search()
{
ComboBox searchField = new ComboBox();
searchField.Items.Add("HelloWorld");
searchField.Name = "cmb " + index.ToString();
searchField.Location = new Point(10, (index + 1) * 20);
searchField.SelectedIndexChanged += new EventHandler((object io, EventArgs ie) =>
{
index++;
Label display = new Label();
display.Location = new Point(250, search().Location.Y-12);
panel.Controls.Add(display);
display.Text = index.ToString();
});
return searchField;
}
Related
This question already has answers here:
Passing variable values between methods...?
(3 answers)
Closed 3 years ago.
I have a Checkbox, when marked it will create a Listbox, Button and a Textbox.
The generated Button, should have the Click event to fill the generated Listbox with the value of the generated Textbox.
But I get compile time error inside public System.Windows.Forms.Button AddNewButton() :
The name Txb does nost exist in the current context
The name lb does nost exist in the current context
Here is the code:
private void cbDd_CheckedChanged(object sender, EventArgs e)
{
AddNewListBox();
AddNewTextBox();
AddNewButton();
}
public System.Windows.Forms.ListBox AddNewListBox()
{
System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
this.Controls.Add(lb);
lb.Top = 74;
lb.Left = 500;
cLeft = cLeft + 1;
return lb;
}
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txb = new System.Windows.Forms.TextBox();
this.Controls.Add(txb);
txb.Top = 180;
txb.Left = 500;
txb.Text = "item name";
cLeft = cLeft + 1;
return txb;
}
public System.Windows.Forms.Button AddNewButton()
{
System.Windows.Forms.Button btn = new System.Windows.Forms.Button();
this.Controls.Add(btn);
btn.Top = 210;
btn.Left = 500;
btn.Text = "Add item";
btn.Click += (s, e) => { if (string.IsNullOrEmpty(txb.Text)) return;
};
lb.Items.Add(cbTxb.Text);
return btn;
}
In addition to AddNew(ListBox|TextBox|Button)
public System.Windows.Forms.ListBox AddNewListBox()
{
return new System.Windows.Forms.ListBox() {
Location = new Point(500, 74),
parent = this, // instead of this.Controls.Add(...)
};
}
public System.Windows.Forms.TextBox AddNewTextBox()
{
return new System.Windows.Forms.TextBox() {
Location = new Point(500, 180),
Text = "item name",
Parent = this,
};
}
public System.Windows.Forms.Button AddNewButton()
{
return new System.Windows.Forms.Button() {
Location = new Point(500, 210),
Text = "Add item",
Parent = this,
};
}
I suggest implementing AddNewControls() where created controls can interact with one another:
private void AddNewControls() {
var lb = AddNewListBox();
var txb = AddNewTextBox();
var btn = AddNewButton();
btn.Click += (s, e) => {
// now btn (button) can use txb (TextBox)
if (string.IsNullOrEmpty(txb.Text))
return;
//TODO: put relevant code here
}
cLeft += 3;
//TODO: check lb and cbTxb.Text
lb.Items.Add(cbTxb.Text);
}
Then you can put
private void cbDd_CheckedChanged(object sender, EventArgs e)
{
AddNewControls();
}
i need to use a button to make a number of textboxes with related buttons each button will add 1 to the textbox (that i want to be related to it HOW?)
i have a windows form with button1 and three panels
==========================================
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace AdvancedCounter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (panel3.Controls != null)
{
var btn = panel3.Controls.Find("_B", true).First();
btn.Click += new EventHandler(Btn_Click);
}
}
int a = 0;
int counter=0;
private void button1_Click(object sender, EventArgs e)
{
counter++;
Button btn = new Button();
btn.Location = new Point(0, 100);
btn.Text = "ADD";
btn.Name ="_B";
btn.Dock = DockStyle.Left;
btn.Click += new EventHandler(Btn_Click);
TextBox txt = new TextBox();
txt.Name = "_T";
txt.Location = new Point(500, 100);
txt.Dock = DockStyle.Left;
txt.Text = a.ToString();
panel3.Controls.Add(txt);
panel3.Controls.Add(btn);
foreach (var item in panel3.Controls.Find("_B", true))
{
item.Text = "ass";
}
}
private void Btn_Click(object sender, EventArgs e)
{
// MessageBox.Show(sender.ToString());
// throw new NotImplementedException();
var txtbx= panel3.Controls.Find("_T", true).First();
var btnbx = panel3.Controls.Find("_B", true).First();
a++;
// find[1].Dispose();
txtbx.Text = a.ToString();
}
}
}
First you are giving same Name property for every TextBox and Button, you should not do that. Instead do like this for example:
btn.Name = "_B_"+ counter;
So you will have different Name for Buttons and TextBoxes. And in event handler:
private void Btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string index = btn.Name.Split('_')[2];
string tbName = "_T_" + index;
var txtbx= panel3.Controls.Find(tbName, true).First();
a++;
txtbx.Text = a.ToString();
}
I'm creating dynamically buttons into a repeater like somebody recommend in previous questions I make into a Page_Init and I make some conditions but the problem persist, the button don't fire the assigned event
this is the first condition
public bool AssignClicked
{
get
{
return Convert.ToBoolean(ViewState["AssignClicked"]);
}
set
{
ViewState["AssignClicked"] = value;
}
}
then this is the code when I make click in the button that create the buttons changing into true the condition:
protected void DButton(object sender, EventArgs e)
{
AssignClicked = true;
Page_Init(sender, e);
Page_Load(sender, e);
}
And start the creations of the buttons
protected void Page_Init(object sender, EventArgs e)
{
if (AssignClicked)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);
Button Btn_clic = (Button)sender;
var name = Btn_clic.Text;
List.ListUsers listArea = new List.ListUsers();
List<Data.Area> Area = listArea.AreaList();
List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == name && x.STANDBY == 0).ToList();
var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();
foreach (var team in TeamFCH)
{
Button newButton = new Button();
newButton.CommandName = "Btn" + Convert.ToString(team);
newButton.ID = "Btn_" + Convert.ToString(team);
newButton.Text = team;
newButton.CommandArgument = name;
newButton.Click += new EventHandler(newButton_Click);
Repeater1.Controls.Add(newButton);
newButton.CssClass = "btn-primary outline separate";
}
}
}
but when I click over the button created this doesn't do anything
I've got panel on which by default are two comboboxes and one "+" button which creates two new combo boxes bellow the first one, I can create multiple (n) rows with two combo boxes and everything is working, I just can't figure out how to get values of those boxes?
Here's code for creating (adding) controls
private void btnCreateFilter_Click(object sender, EventArgs e)
{
y += comboBoxHeight;
ComboBox cb = new ComboBox();
cb.Location = new Point(x, y);
cb.Size = new Size(121, 21);
panelFiltri.Controls.Add(cb);
yDrugi += comboBoxHeight;
ComboBox cbSql = new ComboBox();
cbSql.Location = new Point(xDrugi, yDrugi);
cbSql.Size = new Size(121, 21);
panelFiltri.Controls.Add(cbSql);
btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + 25);
btnSaveFilter.Location = new Point(btnSaveFilter.Location.X, btnSaveFilter.Location.Y + 25);
}
And here's code where I'm lost:
private void btnSaveFilter_Click(object sender, EventArgs e)
{
int i;
foreach (Control s in panelFiltri.Controls)
{
//GOT LOST
}
}
You can get the text in the ComboBox as
private void btnSaveFilter_Click(object sender, EventArgs e)
{
foreach (Control control in panelFiltri.Controls)
{
if (control is ComboBox)
{
string valueInComboBox = control.Text;
// Do something with this value
}
}
}
I don't really know what you're trying to achieve... Maybe this will help you along...
private void btnSaveFilter_Click(object sender, EventArgs e)
{
foreach (ComboBox comboBox in panelFiltri.Controls)
{
var itemCollection = comboBox.Items;
int itemCount = itemCollection.Count; // which is 0 in your case
}
}
I need to make a ListBox that displays how often a Button is clicked.
The user chooses how many buttons are available to click. Here is what I've tried:
int clicked;
clicked = int.Parse(((Button)(sender)).Text);
freq_array[clicked]++;
for (int i = 0; i < freq_array[clicked]; i++)
lstFrequencies.Items[clicked] = clicked + "\t\t" + freq_array[clicked];
freq_array uses the 'clicked' variable to add to the frequency that button has been clicked. Or, it's supposed to.
When I debug it, 'clicked' always comes out to 0. I want 'clicked' to equal the text value of the button that's clicked. When I try to run the program, I get an error saying "Input string was not in correct format."
Edit:
I was able to fix my program with help from you guys. I realized I didn't show enough of my code to be clear enough, and I apologize for that. I had to add some things and move things around and got it soon enough. Thank you all.
Here is the code just for those who may need help in the future:
public partial class Form1 : Form
{
int[] freq_array = new int[11];
int[] numList = new int[11];
int oBase = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
invisiblity();
}
private void invisiblity()
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is Button)
if (Char.IsDigit(ctrl.Text[0]))
ctrl.Visible = false;
}
}
private void btnSetBase_Click(object sender, EventArgs e)
{
Form2 frmDialog = new Form2();
frmDialog.ShowDialog(this);
if (frmDialog.DialogResult == DialogResult.OK)
{
oBase = frmDialog.Base;
//lblOutDigits.Text = oBase.ToString();
for (int i = 0; i < oBase; i++)
{
numList[i] = i;
}
}
ShowBaseButtons(oBase);
}
private void ShowBaseButtons(int last_digit)
{
invisiblity();
foreach (Control ctrl in this.Controls)
{
if (ctrl is Button)
if (Char.IsDigit(ctrl.Text[0]))
if (int.Parse(ctrl.Text) <= last_digit - 1)
ctrl.Visible = true;
}
}
private void btnN_Click(object sender, EventArgs e)
{
lblOutDigits.Text += ((Button)(sender)).Text;
int clicked = int.Parse(((Button)(sender)).Text);
freq_array[clicked]++;
}
private void btnShowFreq_Click(object sender, EventArgs e)
{
lstFrequencies.Items.Clear();
for (int i = 0; i < oBase; i++)
lstFrequencies.Items.Add(numList[i] + " \t\t\t" + freq_array[i]);
}
Your code should work as long as your Button Text is actually just a number. Since what you are trying to do is create an index, what I usually do is use the Tag Property of the control, set it to the Index I want in the designer and then cast that to an Int.
i.e.
if (int.TryParse(((Button)sender).Tag.ToString(), out clicked))
freq_array[clicked]++;
I believe what is happening is that you are not initializing your ListBox, This example Code does work using your initial method. Just create a new Form and paste it in and test.
public partial class Form1 : Form
{
ListBox lstFrequencies = new ListBox();
int[] freq_array = new int[10];
public Form1()
{
InitializeComponent();
Size = new Size(400, 400);
lstFrequencies.Location = new Point(150, 0);
lstFrequencies.Size = new Size(150, 200);
Controls.Add(lstFrequencies);
int top = 0;
for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.Size = new Size(70, 30);
btn.Location = new Point(5, top);
Controls.Add(btn);
top += 35;
btn.Tag = i;
btn.Text = i.ToString();
btn.Click += new EventHandler(btn_Click);
lstFrequencies.Items.Add(i.ToString());
}
}
void btn_Click(object sender, EventArgs e)
{
int clicked;
clicked = int.Parse(((Button)(sender)).Text);
freq_array[clicked]++;
lstFrequencies.Items[clicked] = clicked + "\t\t" + freq_array[clicked]; //Cleaned up you do not need to iterate your list
// Using my example code
//if (int.TryParse(((Button)sender).Tag.ToString(), out clicked))
//{
// freq_array[clicked]++;
// lstFrequencies.Items[clicked] = clicked + "\t\t" + freq_array[clicked];
//}
}
}
Your code always comes out to 0 because you never assign last clicked value to button text. Try this code:
int clicked = 0;
private void button1_Click(object sender, EventArgs e)
{
clicked = Convert.ToInt32(((Button)sender).Text);
lstFrequencies.Items.Add(((Button)sender).Name + " " + ++clicked);
button1.Text = clicked.ToString(); // you lose this line
}
EDIT: Counter from variable member
int clicked = 0;
private void button1_Click(object sender, EventArgs e)
{
// if you want to display button name, change .Text to .Name
lstFrequencies.Items.Add(((Button)sender).Text + " " + ++clicked);
}