Self-removal button works only after the second click. Why? - c#

I have made a button that should self-remove, but it gives me an unexpected result: on the first click it does everything but not self-remove, on the second click it does everything including self-remove. I would like to know explanation of that and how to make it self-remove on the first click.
As the self-remove i mean MainForm.Controls.Remove(StartButton);
StartButton.Location = new Point(132, 35);
StartButton.Name = "StartButton";
StartButton.Size = new Size(85, 37);
StartButton.Text = "Start";
StartButton.Click += new EventHandler(StartButton_Click);
void StartButton_Click(object sender, EventArgs e)
{
MainForm.Controls.Remove(StartButton);
Console.WriteLine("Clicked");
}
Console output:
Clicked
Clicked

The problem were in my code. There were a void that creates a button, and it were called 2 times by a mistake. So there were two buttons.
CreateButton()
CreateButton()
Void CreateButton()
{
StartButton.Location = new Point(132, 35);
StartButton.Name = "StartButton";
StartButton.Size = new Size(85, 37);
StartButton.Text = "Start";
StartButton.Click += new EventHandler(StartButton_Click);
void StartButton_Click(object sender, EventArgs e)
{
MainForm.Controls.Remove(StartButton);
Console.WriteLine("Clicked");
}
}
Problem solved.

Related

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";
}

Change dynamic buttons text with textbox

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?

How to make a MessageBoxButton move?

I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes. Can you help me? Here is the code:
{
MsgBox = new CustomMsgBox();
MsgBox.label1.Text = Text;
MsgBox.button1.Text = btnOK;
MsgBox.button2.Text = btnCancel;
MsgBox.Text = Caption;
result = DialogResult.No;
MsgBox.ShowDialog();
return result;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{
button2.Location = new Point(+100, +100);
}
You will need to create your own form and make it act like a messagebox. Instead of creating a MessageBox, you will instantiate your own form and so that you can handle the buttons on it.

No overload for 'textBox1_TextChanged' matches delegate 'System.EventHandler'

Hello Everyone. This is my first Program and within in 5 minutes I have a error. I've only started to today using C#, so I know I should be really looking around, but I didn't think there was a problem with what I was doing.
My Program is a Generator
depending on what a user picks or types in all the textboxes depends on the outlook of the generated code.
I have two text boxes named: textBox1, and GeneratedCode
When I press checkBox1 it allows textbox1 to be used.
When I press my button it created a string "Testing" (which was to make sure I did it right).
When I pressed F5 to test my build it came back with this error:
No overload for 'textBox1_TextChanged' matches delegate 'System.EventHandler'
I do not know what this means.
Here's my code:
public void checkBox1_CheckedChanged(object sender, EventArgs e)
{
switch (checkBox1.Checked)
{
case true:
{
textBox1.Enabled = true;
break;
}
case false:
{
textBox1.Enabled = false;
break;
}
}
}
private void textBox1_TextChanged()
{
}
public void button1_Click(object sender, EventArgs e)
{
GenerateBox.Text += "Testing";
}
private void GenerateBox_Generated(object sender, EventArgs e)
{
}
This is form1.designer which is in C++:
//
// textBox1
//
this.textBox1.Enabled = false;
this.textBox1.Location = new System.Drawing.Point(127, 3);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(336, 20);
this.textBox1.TabIndex = 1;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); //Error
//
// GenerateBox
//
this.GenerateBox.Enabled = false;
this.GenerateBox.Location = new System.Drawing.Point(84, 6);
this.GenerateBox.MaxLength = 1000000;
this.GenerateBox.Multiline = true;
this.GenerateBox.Name = "GenerateBox";
this.GenerateBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.GenerateBox.Size = new System.Drawing.Size(382, 280);
this.GenerateBox.TabIndex = 1;
this.GenerateBox.TextChanged += new System.EventHandler(this.GenerateBox_Generated);
The function textbox1_textChanged should have two arguments as below to be accepted by EventHandler in this case
textBox1_TextChanged(object sender, EventArgs e)
Your textbox1_TextChanged method does not match what is expected of the System.EventHandler delegate. It should be
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
The compiler is telling you exactly what is wrong, you don't have an EventHandler called textBox1_TextChanged.
Change your textBox1_TextChanged method to read:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//Why are you handling this event if you aren't actually doing anything here???
}
For the rest of my concern with this question, please refer to the commented portion of my code example.
If you didn't mean to add a handler for this event, just remove the following from your designer code:
textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

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