opening a window form from another form programmatically

后端 未结 6 1316
一整个雨季
一整个雨季 2021-01-01 12:03

I am making a Windows Forms application. I have a form. I want to open a new form at run time from the original form on a button click. And then close this

相关标签:
6条回答
  • 2021-01-01 12:14

    This might also help:

    void ButtQuitClick(object sender, EventArgs e)
    {
        QuitWin form = new QuitWin();
        form.Show();
    }
    

    Change ButtQuit to your button name and also change QuitWin to the name of the form that you made.

    When the button is clicked it will open another window, you will need to make another form and a button on your main form for it to work.

    0 讨论(0)
  • 2021-01-01 12:16
    private void btnchangerate_Click(object sender, EventArgs e)
        {
            this.Hide();  //current form will hide
            Form1 fm = new Form1(); //another form will open
            fm.Show();
    
    
        }
    

    on click btn current form will hide and new form will open

    0 讨论(0)
  • 2021-01-01 12:17

    To open from with button click please add the following code in the button event handler

    var m = new Form1();
    m.Show();
    

    Here Form1 is the name of the form which you want to open.

    Also to close the current form, you may use

    this.close();
    
    0 讨论(0)
  • 2021-01-01 12:19

    I would do it like this:

    var form2 = new Form2();
    form2.Show();
    

    and to close current form I would use

    this.Hide(); instead of

    this.close();
    

    check out this Youtube channel link for easy start-up tutorials you might find it helpful if u are a beginner

    0 讨论(0)
  • 2021-01-01 12:19

    You just need to use Dispatcher to perform graphical operation from a thread other then UI thread. I don't think that this will affect behavior of the main form. This may help you : Accessing UI Control from BackgroundWorker Thread

    0 讨论(0)
  • 2021-01-01 12:22

    This is an old question, but answering for gathering knowledge. We have an original form with a button to show the new form.

    The code for the button click is below

    private void button1_Click(object sender, EventArgs e)
    {
        New_Form new_Form = new New_Form();
        new_Form.Show();
    }
    

    Now when click is made, New Form is shown. Since, you want to hide after 2 seconds we are adding a onload event to the new form designer

    this.Load += new System.EventHandler(this.OnPageLoad);
    

    This OnPageLoad function runs when that form is loaded

    In NewForm.cs ,

    public partial class New_Form : Form
    {
        private Timer formClosingTimer;
    
        private void OnPageLoad(object sender, EventArgs e)
        {
            formClosingTimer = new Timer();  // Creating a new timer 
            formClosingTimer.Tick += new EventHandler(CloseForm); // Defining tick event to invoke after a time period
            formClosingTimer.Interval = 2000; // Time Interval in miliseconds
            formClosingTimer.Start(); // Starting a timer
        }
        private void CloseForm(object sender, EventArgs e)
        {
            formClosingTimer.Stop(); // Stoping timer. If we dont stop, function will be triggered in regular intervals
            this.Close(); // Closing the current form
        }
    }
    

    In this new form , a timer is used to invoke a method which closes that form.

    Here is the new form which automatically closes after 2 seconds, we will be able operate on both the forms where no interference between those two forms.

    For your knowledge,

    form.close() will free the memory and we can never interact with that form again form.hide() will just hide the form, where the code part can still run

    For more details about timer refer this link, https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netframework-4.7.2

    0 讨论(0)
提交回复
热议问题