Change dynamic buttons text with textbox - c#

Let me tell you what I want to do.
1.there are two button in my form.One of them let button when I click it.Another button finish to create button.
bool active = false;
private void button1_Click(object sender, EventArgs e)
{
active = true;
}
private void button2_Click(object sender, EventArgs e)
{
active = false;
}
2.I will create button with mousedown event I want to set location of this buttons with coordinate like this.
Button button_create;
private void frm_tr_MouseDown(object sender, MouseEventArgs e)
{
if (active)
{
button_create = new Button();
button_create.Location = new Point(e.X + 5, e.Y - 15);
button_create.Size = new Size(75, 30);
button_create.Text = "Button";
this.Controls.Add(button_create);
button_create.MouseClick += new MouseEventHandler(button_create_MouseClick);
}
}
3.I started mouseclickevent of my created button.When I click this created button I will create new form textbox and button.
TextBox button_text;
void button_create_MouseClick(object sender, MouseEventArgs e)
{
Form frm = new Form();
frm.Show();
button_text = new TextBox();
Button accept = new Button();
accept.Location = new Point(frm.Width / 2, frm.Height / 2);
frm.Controls.Add(button_text);
frm.Controls.Add(accept);
accept.MouseClick += new MouseEventHandler(accept_MouseClick);
}
4.I started mouseclickevent of my last created button.I want to change text of my first created button.
void accept_MouseClick(object sender, MouseEventArgs e)
{
button_create.Text = button_text.Text;
}
5.I will click button1 and I click anywhere of this form new button will create and I also click button2 to finish create new button.I can change my created button with textbox but
If I click button1 and I create a new one button I cant change text of previous created button How can I do that?

Related

Drag and Drop Buttons - How to get access the dropped button?

I want when I move button2 into button1 (img2) below code will done.
My code:
private void ButtonDragDrop(object sender, DragEventArgs e)
{
var btn = (Button)e.Data.GetData(typeof(Button)); //button2
(sender as Button).Text = "test1"; //button1
btn.Text = "Test2"; //button2
}
How Can I make when button1 touch button2 code will done?

How can I create a button programmatically in C# window app?

In the Form1_Load method what code should I write to create a simple button?
private void Form1_Load(object sender, System.EventArgs e)
{
}
So that on Load the button would show.
As you said it is Winforms, you can do the following...
First create a new Button object.
Button newButton = new Button();
Then add it to the form inside that function using:
this.Controls.Add(newButton);
Extra properties you can set...
newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);
Your issue you're running to is that you're trying to set it on Form_Load event, at that stage the form does not exist yet and your buttons are overwritten. You need a delegate for the Shown or Activated events in order to show the button.
For example inside your Form1 constructor,
public Form1()
{
InitializeComponent();
this.Shown += CreateButtonDelegate;
}
Your actual delegate is where you create your button and add it to the form, something like this will work.
private void CreateButtonDelegate(object sender, EventArgs e)
{
Button newButton= new Button();
this.Controls.Add(newButton);
newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);
newButton.Location = new Point(20, 50);
}
on your eventload form put this code
private void Form1_Load(object sender, EventArgs e)
{
Button testbutton = new Button();
testbutton.Text = "button1";
testbutton.Location = new Point(70, 70);
testbutton.Size = new Size(100, 100);
testbutton.Visible = true;
testbutton.BringToFront();
this.Controls.Add(testbutton);
}
It's simple :
private void Form1_Load(object sender, System.EventArgs e)
{
Button btn1 = new Button();
this.Controls.add(btn1);
btn1.Top=100;
btn1.Left=100;
btn1.Text="My Button";
}

How to check click method when button is created from a class?

I am using Tabcontrol and I created a class for all new Tabpages.
For examle when I open a new Tabpage the class creates controls and places them.
bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
tp.Controls.Add(bttn1);
So my question is how can I check if this button is clicked?
Also my other question is the same with a Timer tick event.
You can easily attach to the button's Click event from the code:
bttn1.Click += new EventHandler(butt1_Click);
And here's the handler:
void button1_Click(object sender, EventArgs e)
{
// ...
}
Visual Studio will help you when you type the Click +=. After typing +=, hit the Tab key twice to get the handler.
I hope that you have created a UserControl for this or have sub-classed the TabPage class to create your controls. You should expose the Click event of the button from this newly created class through some new event you create:
public class MyTabPage : TabPage
{
private Button bttn1;
public event EventHandler Button1Clicked;
public MyTabPage()
{
bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
bttn1.Click += bttn1_Click;
this.Controls.Add(bttn1);
}
void bttn1_Click(object sender, EventArgs e)
{
OnButton1Clicked();
}
protected virtual void OnButton1Clicked()
{
var h = Button1Clicked;
if (h != null)
h(this, EventArgs.Empty);
}
}
Now when you create an instance of MyTabPage, you can attach a handler to the Button1Clicked event:
MyTabPage page = new MyTabPage();
page.Button1Clicked += page_Button1Clicked;
tabControl.TabPages.Add(page);
...
void page_Button1Clicked(object sender, EventArgs e)
{
}

how to execute click event on dynamically created button in c#.net

I am trying to build an app, where user can select category and according to it displays its sub categories , these sub categories are buttons, which are dynamically created.
Now, as buttons are dynamically created so I am confuse how to write code under button_click event as I dont know how many subcategories are there.
So is there any way I can execute click event of a particular button , so that I can execute certain commands?
EDITED
This is the code that i tried
Button btnDynamicButton = new Button();
private void btnclick_Click(object sender, EventArgs e)
{
label2.Text = btnDynamicButton.Text;
}
private void btnappetizer_Click(object sender, EventArgs e)
{
groupBox2.Visible =false;
DataTable dt = new DataTable();
dt = itemmasterbl.SelectallrecordFromtblItem(btnappetizer.Text);
for (int i = 0; i < dt.Rows.Count; i++)
{
string name = "Appetizer" + DynamicButtonCount;
Button btnDynamicButton1 = new Button();
btnDynamicButton1.Name = name;
btnDynamicButton1.Text = name;
btnDynamicButton1.Size =
new System.Drawing.Size(150, 30);
btnDynamicButton1.Location =
new System.Drawing.Point(180, DynamicButtonCount * 30);
btnDynamicButton1.Click +=new EventHandler(btnclick_Click);<br>
Controls.Add(btnDynamicButton1);
DynamicButtonCount++;
btnDynamicButton = btnDynamicButton1;
}
}
Once I do this it creates three buttons according to number of values in itemmaster DB under appetizer, but once I click on any of the three buttons the label displays only last buttons text,because in last line I have :
btnDynamicButton = btnDynamicButton1;
Which will last buttons infos,but rather I want which ever button I press, label should display respective text. How can I achieve this.
you can put all your logic into one handler:
System.Windows.Forms.Button b = new System.Windows.Forms.Button();
b.Click += new EventHandler(b_Click);
//finally insert the button where it needs to be inserted.
...
void b_Click(object sender, EventArgs e)
{
MessageBox.Show(((System.Windows.Forms.Button)sender).Name + " clicked");
}
To your edit:
You are storing the reference for your button(s) inside the Field btnDynamicButton. Hence it always gets overwritten with the latest button you have created. You should not reference the button by using a field. The sender parameter of the click-handler contains the button element that has been clicked. See the code above: Simple cast sender to Button and you know which button has been clicked:
private void btnclick_Click(object sender, EventArgs e)
{
Button btn = (Button)sender
label2.Text = btn.Text;
}

Adding button events!

I add buttons dynamically.. e.g. Button newButton=new Button();
Now i want each button to be triggered. So i wrote them the following events:
public void response_Click(object sender, EventArgs e)
{
}
public void edit_Click(object sender, EventArgs e)
{
}
public void quote_Click(object sender, EventArgs e)
{
}
Button quote = new Button();
Button reply = new Button();
Button edit = new Button();
quote.ID = "quote";
reply.ID = "reply";
edit.ID = "edit";
How do i trigger them, as soon as the user clicks on the button..will my functions above be triggered? do i need to do the following:
this.Clicked+=quote;
this.Clicked+=reply;
this.Clicked+=edit;
if i do need to do that..where do i put those lines of code?
i use visual studio 1010. asp.net
You can do like..
quote.Click += new EventHandler(quote_Click);
reply.Click += new EventHandler(response_Click);
edit.Click += new EventHandler(edit_Click);
Yes, you would need to register the event of the button and associate it with particular method.
for example.
myButton.Click += new EventHandler(Button_Click);
void Button_Click(object sender, EventArgs e)
{
}

Categories

Resources