load form when pressing on button
Page 1 of 111 Replies - 4890 Views - Last Post: 06 September 2010 - 12:02 PM
#1
load form when pressing on button
Posted 05 September 2010 - 12:21 PM
Replies To: load form when pressing on button
#2
Re: load form when pressing on button
Posted 05 September 2010 - 12:26 PM
#3
Re: load form when pressing on button
Posted 05 September 2010 - 12:39 PM
I have a form ,label and button , i wrote this code in Form_Load Event :
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Hello World";
}
and i want when i click on the button the Code in the form loads again
but i don't want to hide the form then show it again like that :
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
this.Show();
}
i just want the code in the form to load again
#4
Re: load form when pressing on button
Posted 05 September 2010 - 12:43 PM
#5
Re: load form when pressing on button
Posted 05 September 2010 - 12:48 PM
#6
Re: load form when pressing on button
Posted 05 September 2010 - 02:09 PM
You can either keep the same instance of your form: And show/hide it.
Or you can make a new instance of that form object.
So do you want to make a NEW form... or keep the same form throughout the lifetime of your application?
#7
Re: load form when pressing on button
Posted 05 September 2010 - 05:29 PM
#8
Re: load form when pressing on button
Posted 06 September 2010 - 10:29 AM
#9
Re: load form when pressing on button
Posted 06 September 2010 - 10:41 AM
private void Button1_Click(object sender, ButtonclickEventArgs e)
{
// Call the load method matching the arguments
Form1_Load(sender /*pass the same object*/, EventArgs.Empty);
}
Personally, I wouldn't do it exactly that way.
Instead have both the onload and the Buttonclick call the same DoSetup method
private void Form1_Load(object sender, EventArgs e)
{
DoSetup();
}
private void Button1_Click(object sender, EventArgs e)
{
DoSetup();
}
private void DoSetup()
{
label1.Text = "Hello World";
This post has been edited by tlhIn'toq: 06 September 2010 - 10:42 AM
#10
Re: load form when pressing on button
Posted 06 September 2010 - 10:57 AM
#11
Re: load form when pressing on button
Posted 06 September 2010 - 11:13 AM
Nakor, on 06 September 2010 - 09:57 AM, said:
I would not recommend doing it that way. It puts the assignment into the Form1.Designer.cs file. When you look at your Form1.cs code you won't see the assignment. It makes the code harder to follow and maintain. A human does not naturally assume that a button_click has been assigned to the Form_Load handler. A year from now you're going to spend half a day trying to figure out just what you've done.
You can see it here on lines 40 and 50. Imagine when your form has 100 controls on it and you have to scrub though this to figure out what control events are cross-wired to which methods.
partial class FormTest
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 55);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.FormTest_Load);
//
// FormTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button1);
this.Name = "FormTest";
this.Text = "FormTest";
this.Load += new System.EventHandler(this.FormTest_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
Get in the habit of compartimentalizing your code. If you have a Setup routine then make a Setup() method and call it. Call it from Load, call it from Buttonclick, call it from another form, whatever. But a year from now when you are wanting to change the Setup proceedure you will be able to find it.
#12
Re: load form when pressing on button
Posted 06 September 2010 - 12:02 PM
|
|

New Topic/Question
Reply




MultiQuote









|