I have a form that pops up at a specific event. It draws up buttons from an array and sets the Tag value to a specific value. So if you are to press or click this button the function should return the Tag value.
How can I do this? And how do I know which button was clicked?
At this moment the code returns DialogResult, but I want to return the Tag value from the function. How shall I modify my code so that it can do this?
public static DialogResult SelectBox(string title, string[] btnArray, string[] btnValueArray)
{
Form form = new Form();
Button[] buttonArray;
buttonArray = new Button[5];
form.Text = title;
for (int i = 0; i < btnArray.Length; i++)
{
buttonArray[i] = new Button();
buttonArray[i].Text = btnArray[i];
buttonArray[i].Tag = new int();
buttonArray[i].Tag = btnValueArray[i];
buttonArray[i].TabStop = false;
buttonArray[i].Location = new System.Drawing.Point(0, i * 40);
buttonArray[i].Size = new System.Drawing.Size(240, 40);
}
form.ClientSize = new Size(240, 268);
form.Controls.AddRange(new Control[] { buttonArray[0], buttonArray[1], buttonArray[2] });
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
DialogResult dialogResult = form.ShowDialog();
return dialogResult;
}
Add a private variable in the form:
private object SelectedTag;
Add a button click handler:
private void Button_Click(object sender, EventArgs e) {
SelectedTag = ((Button)sender).Tag;
}
Assign the handler to each button you create:
..
buttonArray[i].OnClick += form.Button_Click;
..
Then in your static function, simply return form.SelectedTag instead of the dialogresult.
You could call the same click event for all buttons. then in your handler:
private void ButtonClick(object sender, EventArgs args)
{
Button oButton = (Button) sender;
object data = oButton.Tag;
}
The DialogResult property already tells you which button was clicked. You can set each individual button to return a different DialogResult, and then check for that at the bottom of the function.
And if you want to return the clicked button's Tag property instead, you need to change the function's return value to Object (because the Tag property is of type Object).
You can add a ButtonClick event handler in a TestForm, set the button's tag to the Form's tag.
Here is the sample.
Main Form:
private void Form1_Load(object sender, EventArgs e)
{
Object tag;
SelectBox("test", new String[] { "One", "Two", "Three" }, new String[] {"one value", "Two value", "three value" }, out tag);
MessageBox.Show(tag.ToString());
}
public static DialogResult SelectBox(string title, string[] btnArray, string[] btnValueArray, out Object tagValue)
{
TestForm form = new TestForm();
Button[] buttonArray;
buttonArray = new Button[5];
form.Text = title;
for (int i = 0; i < btnArray.Length; i++)
{
buttonArray[i] = new Button();
buttonArray[i].Text = btnArray[i];
buttonArray[i].Tag = new int();
buttonArray[i].Tag = btnValueArray[i];
buttonArray[i].TabStop = false;
buttonArray[i].Location = new System.Drawing.Point(0, i * 40);
buttonArray[i].Size = new System.Drawing.Size(240, 40);
// subscribe to button click event..
// the handler is in TestForm
buttonArray[i].Click += form.HandleOnButtonClick;
}
form.ClientSize = new Size(240, 268);
form.Controls.AddRange(new Control[] { buttonArray[0], buttonArray[1], buttonArray[2] });
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
DialogResult dialogResult = form.ShowDialog();
// set the out args value
tagValue = form.Tag;
return dialogResult;
}
TestForm that whose instance we create in the SelectBox dialog:
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
}
public void HandleOnButtonClick(Object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
this.Tag = button.Tag;
}
}
Edit:
If you want to capture every button's value then expose a Dictionary<String, Object> Tags property.
Related
I have an event registered.
layout.SizeChanged += parentToggleBox.Resize;
Problem is, I would like this Resize event to wait until my mouse has moved away from the box.
How to make this event wait until a boolean is changed from false to true?
or wait until OnMouseLeave(object sender, MouseEventArgs e) is triggered?
Here's an example that I put together:
public class Form1 : Form
{
private Panel Panel1;
private Label Label1;
public Form1()
{
this.Panel1 = new Panel()
{
BackColor = System.Drawing.Color.Red,
};
this.Label1 = new Label()
{
Top = 200,
Text = "Click",
};
bool clicked = false;
bool entered = false;
this.Panel1.Click += (s, e) =>
{
this.Panel1.BackColor = System.Drawing.Color.Blue;
clicked = true;
};
this.Panel1.MouseEnter += (s, e) =>
{
this.Panel1.BackColor = System.Drawing.Color.Yellow;
clicked = false;
entered = true;
};
this.Panel1.MouseLeave += (s, e) =>
{
if (entered && clicked)
{
this.Panel1.BackColor = System.Drawing.Color.Green;
this.Label1.Text = "Success";
}
};
this.Controls.Add(this.Panel1);
this.Controls.Add(this.Label1);
}
}
I've used a Click event rather than a Resize to demonstrate the technique.
Effectively this is just setting two booleans and when the right combination of entered and clicked occurs then the code in the block that contains this.Label1.Text = "Success"; will run.
The colours are set for debugging purposes.
To run, do this:
var form1 = new Form1();
form1.ShowDialog();
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 want to create a form programmatically, then add controls to that for and handle click events on those controls,
such as click on button should show impact on text box
namespace formwizard
{
public partial class Form1 : Form
{
Form form = new Form();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
form.Text = formtitle.Text;
int count = Convert.ToInt32(FormName.Text);
int i=1;
while (i<= count)
{
TextBox tb = new TextBox();
tb.Text = "Text box"+ i.ToString();
Button bt = new Button();
bt.Text = "Button" + i.ToString();
tb.Location = new Point(15, i*20);
bt.Location = new Point(120, i*20);
bt.Name = "Button" + i.ToString();
form.Controls.Add(tb);
form.Controls.Add(bt);
bt.Click +=new EventHandler(bt_Click);
i++;
}
// form.Controls.Add(...);
form.ShowDialog();
}
void bt_Click(object sender, EventArgs e)
{
Button btn = (Button) sender;
string a=btn.Text.Substring(6,btn.Text.Length-6);
MessageBox.Show("You clicked Button "+a);
}
}
}
Your code is correct, the only problem I see is you haven't initialized a new instance of Form:
Form form2 = new Form();
//now add your controls to this form
//show form using "form2.ShowDialog()"
So I'm trying to make a button to so that when it is clicked, the button will turn transparrent, and a game in the background will begin. I'm new and don't know what i'm doing, but here is my code:
public Form1()
{
//Just ignore all this
InitializeComponent();
Label[] labelArray = { label1, label2, label3, label4, label5,
label6, label7, label8, label9 };
for (int i = 0; i < labelArray.Length; i++)
{
labelArray[i].BackColor = System.Drawing.Color.Transparent;
}
//Button details...
Button buttonStart = new Button();
buttonStart.Location = new Point(90, 150);
buttonStart.Text = ("Click start to begin");
buttonStart.Size = new Size(150, 50);
//Adding the evnet handler
buttonStart.Click += new EventHandler(buttonStart_Click);
//Adding the button to the form
this.Controls.Add(buttonStart);
buttonStart.BringToFront();
//Clicking it, in the hopes that what in (buttonStart_Click event
//handler would do something)
buttonStart.PerformClick();
}
//Eventhandler that says it cannot recoqnize
//my "buttonStart", and I cannot seem to find any other way to do this
private void buttonStart_Click(object sender, EventArgs e)
{
buttonStart.BackColor = System.Drawing.Color.Transparent;
}
try this:
private void buttonStart_Click(object sender, EventArgs e)
{
this.buttonStart.visible = false;
}