int i=1;
int b=1;
private void button1_Click(object sender, EventArgs e)
{
b++;
if (i < b) {
ComboBox nama = new ComboBox();
/* comboBox2.Location = new System.Drawing.Point(39, locacom*angka);
comboBox2.Name = "comboBox"+angka;
comboBox2.Size = new System.Drawing.Size(156, 21);
comboBox1.BackColor = System.Drawing.Color.Orange;
comboBox1.ForeColor = System.Drawing.Color.Black;
Controls.Add(comboBox2);
label3.Text = i.ToString();
i++;
*/
}
else
{
label2.Text = "Something went wrong";
}
label4.Text = angka.ToString();
label5.Text = i.ToString();
this.Refresh();
}
I already tried some
I can't came up with something new
i.e:
ComboBox name + i.toString() = new ComboBox();
var names = name + i.toString();
ComboBox names = new ComboBox();
Another question: What will happend if I can make combobox using the same name?
Related
I am a beginner and I meet a problem with the Buttons in a DataGridView.
When I fill a cell with text that is the row number of the grid, no problem, the text is correct and stay correct if I add a new line.
But if I put this same text on the Button, all the lines are repainted and all the Buttons have the text of the last Button created.
My code is in the click event of a Button out the DataGridView:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
textBox1.Text += "ligne clické " + e.RowIndex+ Environment.NewLine;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
textBox1.AppendText( "bouton clické "+btn.Tag+ Environment.NewLine) ;
}
}
private void button1_Click(object sender, EventArgs e)
{
string[] row;
row = new string[] { "", "Product "+dataGridView1.RowCount };
dataGridView1.Columns[1].ReadOnly = true;
btn.HeaderText = "Click Data";
Random rnd = new Random();
int RND = rnd.Next(1, 1003);
btn.Text = "Click Here"+dataGridView1.RowCount+" " + RND.ToString();
btn.Name = "btn"+RND.ToString();
btn.Tag = "tag ->" + dataGridView1.RowCount;
btn.UseColumnTextForButtonValue = true;
dataGridView1.Rows.Add(row);
}
I found the solution
private void button1_Click(object sender, EventArgs e)
{
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.HeaderText = "Click Data";
Random rnd = new Random();
int RND = rnd.Next(1, 1003);
btn.Text = "Click Here" + dataGridView1.RowCount + " " + RND.ToString();
btn.Name = "btn" + RND.ToString();
btn.Tag = "tag ->" + dataGridView1.RowCount;
int rowIndex = dataGridView1.Rows.Add(btn);
dataGridView1.Rows[rowIndex].Cells[0].Value = btn.Text;
dataGridView1.Rows[rowIndex].Cells[1].Value = "Product " + dataGridView1.RowCount;
}
I'm using this code to create buttons with two lines text
private void button1_Click(object sender, EventArgs e)
{
int top = 50;
int left = 100;
int n = 0;
int s = 99;
for (int i = 0; i < 20; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
panel1.Controls.Add(button); // here
button.Size = new Size(50, 50);
if (i == 4 || i == 9||i==14||i==19)
{
top = 30;
left = 23;
top +=button.Top+2;
left += button.Width+2;
}
else
left += button.Width + 2;
n = n + 1;
s = s + 1;
button.Text = Convert.ToString(n) + Environment.NewLine + Convert.ToString(s);
button.Click += Button_Click;
}
}
private void Button_Click(object sender, EventArgs e)
{
string s;
Button button = (Button)sender;
s = button.Text + Environment.NewLine;
MessageBox.Show(s);
}
So I need to select only second line brom my button text when I click dynamic button. How will I do this?
you can store some data in Tag property:
button.Text = Convert.ToString(n) + Environment.NewLine + Convert.ToString(s);
button.Tag = new int[] { n, s };
and later retrive it and use:
private void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
var data = button.Tag as int[];
string s = data[1].ToString();
MessageBox.Show(s);
}
private void Button_Click(object sender, EventArgs e)
{
string s;
Button button = (Button)sender;
s = button.Text + Environment.NewLine;
MessageBox.Show(button.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None)[1]);
}
I have dynamically created buttons as shown in code all all the button from 10:00 to 12:00 with 15 min of interval and deselect in lifo order like if i have selected 10:0 and 10:15 then i can not select 10:45 .. and i i have selected 10:0,10:15,10:30,10:45 and then i have to deselect 10:15 then first we have to deselect 10:30 and 10:45 ...please help me out.
private void GetControls()
{
count++;
for (int i = 10; i < 12; i++)
{
for (int j = 0; j < 60; j += 15)
{
Button btn = new Button();
btn.Text = i + "-" + j;
btn.ID = i + "-" + j;
btn.Command += new CommandEventHandler(this.btn_Click);
// btn.Click += btn_Click;
flag = true;
btn.CommandName = i + "-" + j;
if (count==1)
{
PlaceHolder1.Controls.Add(btn);
}
}
}
}
private void btn_Click(object sender, CommandEventArgs e)
{
count++;
string ID = (sender as Button).ID;
Label1.Text = " Congrates! Your meeting time has been sheduled upto " + ID;
Label1.Visible = false;
Button btn = sender as Button;
if (btn.BackColor == Color.Green)
{
btn.BackColor = System.Drawing.Color.Yellow;
getStatus(sender);
}
else
{
btn.BackColor = System.Drawing.Color.Green;
}
Correct me if I am wrong! You want to only select one button at a time so that if one button is clicked, the rest should reset? if that's the case, here's a code to do so:
[Edit]
Added a Session to remember dynamic button ids and used (.) instead of (-) for button Ids
private void GetControls()
{
count++;
for (int i = 10; i < 12; i++)
{
for (int j = 0; j < 60; j += 15)
{
Button btn = new Button();
btn.Text = i + "-" + j;
btn.ID = i + "." + j;
btn.Command += new CommandEventHandler(this.btn_Click);
// btn.Click += btn_Click;
flag = true;
btn.CommandName = i + "-" + j;
if (count == 1)
{
PlaceHolder1.Controls.Add(btn);
List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
if (!createdControls.Contains(btn.ID)) createdControls.Add(btn.ID);
Session["Controls"] = createdControls;
}
}
}
}
[Edited] Replace the btn-Click event with the following.
private void btn_Click(object sender, CommandEventArgs e)
{
count++;
string ID = (sender as Button).ID;
ResetButton(Convert.ToDouble(ID));
Label1.Text = " Congrates! Your meeting time has been sheduled upto " + ID;
Button btn = sender as Button;
if (btn.BackColor == Color.Green)
{
btn.BackColor = System.Drawing.Color.Yellow;
}
else
{
btn.BackColor = System.Drawing.Color.Green;
}
}
[Edited] And this is the method implementation
private void ResetButton(double selectedButtonID)
{
List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
TimeSpan timespan = TimeSpan.FromHours(selectedButtonID);
string currentSelectedTime = timespan.ToString("h\\:mm");
foreach (string buttonID in createdControls)
{
if (!string.IsNullOrEmpty(buttonID))
{
int comparisonResult = timespan.CompareTo(TimeSpan.FromHours(Convert.ToDouble(buttonID)));
Button button = Page.FindControl(buttonID) as Button;
if (button != null && comparisonResult==1)
{
button.BackColor = Color.Yellow;// selected
}
else
{
button.BackColor = Color.Red;// deselected
}
}
}
I'm trying to delete a label and a button based on their name but unfortunately the option to remove them (that I know of) is through Children.Remove where it only accepts the actual label/button itself and not their name. I needed the name since it's the one that determines which X button belongs to who.
Label labels;
Button buttons;
int counter;
private void button_Copy_Click(object sender, RoutedEventArgs e)
{
counter++;
Label lbl = new Label();
lbl.Content = counter.ToString();
lbl.HorizontalAlignment = HorizontalAlignment.Center;
lbl.VerticalAlignment = VerticalAlignment.Center;
lbl.FontSize = 50;
lbl.Name = "lbl" + counter;
labels = lbl;
Button bt = new Button();
bt.Content = "X";
bt.HorizontalAlignment = HorizontalAlignment.Right;
bt.VerticalAlignment = VerticalAlignment.Top;
bt.Name = "btn" + counter;
buttons = bt;
bt.Click += Button_Click;
grid.Children.Add(lbl);
grid.Children.Add(bt);
}
private void Button_Click(object sender, EventArgs e)
{
grid.Children.Remove(labels.Name = "btn" + counter);
grid.Children.Remove(buttons);
}
The grid.Children.Remove(labels.Name = "btn" + counter); is not correct but hopefully it tells how I kinda wanted it to happen.
You can first get the child element using a LINQ Where expression:
var child = grid.Children.OfType<Control>().Where(x => x.Name == "btn" + counter).First();
And then call Remove:
grid.Children.Remove(child);
I have looking for the solution but still found nothing
Here is my code:
private void Form1_Load(object sender, EventArgs e)
{
RichTextBox rtb = new RichTextBox();
rtb.Text = File.ReadAllText(#"C:\Users\Admin\Desktop\myfile\customers.txt");
int i = 0;
foreach (string line in rtb.Lines)
{
if (line == "--")
{
ListViewItem item = new ListViewItem();
item.Text = rtb.Lines[i + 1];
item.SubItems.Add(rtb.Lines[i + 2]);
item.SubItems.Add(rtb.Lines[i + 3]);
item.SubItems.Add(rtb.Lines[i + 4]);
listView1.Items.Add(item);
}
i += 1;
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 pop = new Form2();
pop.ShowDialog();
string name = pop.name;
int age = int.Parse(pop.Age);
string dob = pop.DateOfBirth;
string addr = pop.Address;
StreamWriter write = new StreamWriter(#"C:\Users\Admin\Desktop\myfile\customers.txt",true);
write.Write("--\n");
write.Write("{0}\n",name);
write.Write("{0}\n",dob);
write.Write("{0}\n",age);
write.Write("{0}\n",addr);
write.Close();
}
The question is how do I reload the list view after I write the data into the text file?
Extract the logic out of Form1_Load
private void Form1_Load(object sender, EventArgs e)
{
RefreshListView();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 pop = new Form2();
pop.ShowDialog();
string name = pop.name;
int age = int.Parse(pop.Age);
string dob = pop.DateOfBirth;
string addr = pop.Address;
StreamWriter write = new StreamWriter(#"C:\Users\Admin\Desktop\myfile\customers.txt",true);
write.Write("--\n");
write.Write("{0}\n",name);
write.Write("{0}\n",dob);
write.Write("{0}\n",age);
write.Write("{0}\n",addr);
write.Close();
RefreshListView();
}
private void RefreshListView()
{
listView1.Items.Clear();
RichTextBox rtb = new RichTextBox();
rtb.Text = File.ReadAllText(#"C:\Users\Admin\Desktop\myfile\customers.txt");
int i = 0;
foreach (string line in rtb.Lines)
{
if (line == "--")
{
ListViewItem item = new ListViewItem();
item.Text = rtb.Lines[i + 1];
item.SubItems.Add(rtb.Lines[i + 2]);
item.SubItems.Add(rtb.Lines[i + 3]);
item.SubItems.Add(rtb.Lines[i + 4]);
listView1.Items.Add(item);
}
i += 1;
}
}