11 Replies - 7465 Views - Last Post: 06 September 2010 - 12:02 PM Rate Topic: -----

#1 kareem_3d  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 40
  • Joined: 13-July 10

load form when pressing on button

Posted 05 September 2010 - 12:21 PM

i want when pressing on a button, the form load without using (close or hide) and start it again , is this possible ?
Is This A Good Question/Topic? 0
  • +

Replies To: load form when pressing on button

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: load form when pressing on button

Posted 05 September 2010 - 12:26 PM

Your question doesn't make much sense. what do you mean by "form load with using (close or hide) and start it again"? Are you asking how to make a form appear that you cannot close or hide?
Was This Post Helpful? 0
  • +
  • -

#3 kareem_3d  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 40
  • Joined: 13-July 10

Re: load form when pressing on button

Posted 05 September 2010 - 12:39 PM

No,Execuse my English ,, i'll give example :

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
Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: load form when pressing on button

Posted 05 September 2010 - 12:43 PM

Kind of like pushing the "Refresh" button in a web browser you mean?
Was This Post Helpful? 0
  • +
  • -

#5 kareem_3d  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 40
  • Joined: 13-July 10

Re: load form when pressing on button

Posted 05 September 2010 - 12:48 PM

yes i think that
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: load form when pressing on button

Posted 05 September 2010 - 02:09 PM

I think you need to define what you mean by "load".

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?
Was This Post Helpful? 1
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: load form when pressing on button

Posted 05 September 2010 - 05:29 PM

you can call the onload function from the onclick function
Was This Post Helpful? 1
  • +
  • -

#8 kareem_3d  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 40
  • Joined: 13-July 10

Re: load form when pressing on button

Posted 06 September 2010 - 10:29 AM

Yes , how can i call the onload function from the onclick function ??
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: load form when pressing on button

Posted 06 September 2010 - 10:41 AM

Technically you can do this

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

Was This Post Helpful? 0
  • +
  • -

#10 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: load form when pressing on button

Posted 06 September 2010 - 10:57 AM

go into the button event handlers and set the button's onclick event to call the onload function, then you're only defining one function but calling it from two different events
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: load form when pressing on button

Posted 06 September 2010 - 11:13 AM

View PostNakor, on 06 September 2010 - 09:57 AM, said:

go into the button event handlers and set the button's onclick event to call the onload function, then you're only defining one function but calling it from two different events


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.
Was This Post Helpful? 0
  • +
  • -

#12 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: load form when pressing on button

Posted 06 September 2010 - 12:02 PM

if you did it the way i described above you'd definitely want to comment what you'd done, but I'd have to agree it's not the best way, just an option
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1