locked
create many objects ?! RRS feed

  • Question

  • Hello,

      I have created a program (windows Forms) using C# Express 2010.

    This program takes me from form to another using buttons. If I reached the last form There is a button also that takes me to the home page. In my home page there is a button to start the program (Start button), when the user press it, it allows him to take the test. My question is:

    When I press the button in the last from that takes me to the home page, then I press the start button in the home page to redo the test several times, all the next forms should be cleared from the previous values! So, how can I create a function that do the following " Each time I press start button a clear new form without previous values appears to me". 

    What I did is wrong, because I created an instructor each time!!

    Any help?

     

    Thanks,

    Learner 2011

    Tuesday, September 13, 2011 6:35 PM

Answers

  • Lets start this project a bit differently. I would create a new class, which will have all the properties, that are values in all the forms.

    So when user enters some value on some form, it will SET (using set accessor) the property in that class (and so on for all the values). When you want to "reset" values, simply Set all the value to zero (for numbers), or string.Empty (for strings). 

    What you have to do, each time you come to some form, is to read the coresponding values (those properties that belong to form), so they will fill the controls on forms (for this you will use Get accessor).

     

    What do you thing? Can you do that, or you need some help?


    Mitja
    • Proposed as answer by Alex_Lee Thursday, September 15, 2011 6:55 PM
    • Marked as answer by Jackie-SunModerator Monday, September 26, 2011 6:27 AM
    Thursday, September 15, 2011 6:12 PM

All replies

  • Are you having an ASP.NET web site, a WinForms program or a WPF application or do you use SilverLight?

    Tuesday, September 13, 2011 6:45 PM
  • Just add some code to the Form.Activated event handler that clears the controls on the form:

        private void Form1_Activated(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }
    


    Just beware that this would clear the form if someone switches back and forth between another program. But that's the basic idea. Otherwise you could add your own method to the each form that clears the form. Then the other forms would call both the Clear and Show methods. For example:

    On each of the test page forms:

        public void Clear()
        {
            this.textBox1.Clear();
            this.textBox2.Clear();
        }
    


    On the home form, clear all the test pages when starting a new test

        public partial class HomeForm : Form
        {
            public TestPage1Form page1;
            public TestPage2Form page2;
    
            public HomeForm()
            {
                InitializeComponent();
            }
    
            // other code
    
            private void btnStart_Click(object sender, EventArgs e)
            {
                page1.Clear();
                page2.Clear();
                this.Hide();
                page1.Show();
            }
        }
    

     


    jmh
    Tuesday, September 13, 2011 7:10 PM
  • I don't understand what's wrong with just re-instantiating the pages every time the user hits start. I know it's lazy, but the garbage collector should take care of the discarded forms quick smart, and it has the advantage of not needing to maintain all the Clear() functions whenever controls are added or removed.


    private void btnStart_Click(object sender, EventArgs e)
    {
    page1 = new TestPage1Form();
    page2 = new TestPage2Form();
    this.Hide();
    page1.Show();
    }
    Wednesday, September 14, 2011 3:03 AM
  • Hello,

    try below code!

    public void startWizard()
    {
        this.Hide();
    
        //all forms inherited from System.Windows.Form! add wizard to List<Form>!
        List<Form> forms = new List<Form>();
        forms.Add(new Form());//form1
        forms.Add(new Form());//form2
        forms.Add(new Form());//form3
        forms.Add(new Form());//form4
        forms.Add(new Form());//form5
        forms.Add(new Form());//form6
        forms.Add(new Form());//form7
        forms.Add(new Form());//form8
        //...
    
        int index = 0;
        while (true)
        {
            if (index >= forms.Count || index < 0)
                break;
    
            Form form = forms[index];
    
            DialogResult result = form.ShowDialog();//set it as you want
    
            //DialogResult.No == next
            //DialogResult.Yes == back
            //DialogResult.Cancel back to homePage
    
            if (result == DialogResult.No)
            {//show next Form
                ++index;
            } 
            else if (result == DialogResult.Yes)
            {//show previous form
                --index;
            }
            else if (result == DialogResult.Cancel)
            {//back to main dialog
                break;
            }
        }
    
        //process forms inputs
    
        this.Show();
    }
    



    Any fool can know. The point is to understand.(Albert Einstein)
    Wednesday, September 14, 2011 8:32 PM
  • Stefan.. I am using C# Express 2010

     

    Jmh_gr , I applied your suggestion, but there is no clear function in the forms :S I mean I couldn't say (Form1.clear()) ..

    Ante..  I could not write  Form1= new TestForm1Form() ( I got error)

    Ehsan, I don't know where I sould place your suggested code?!

     

    Learner 2011

    Thursday, September 15, 2011 4:04 PM
  • Just try to reinitialize the Forms..

    Do not reuse the existing form objects..!


    - <BooM>
    Thursday, September 15, 2011 4:14 PM
  • Check if there are opened forms, and if they are (the sure are, you only made them Hide), you close them. So you can think of clearing them.

    On the mainform you do:

    foreach (Form f in Application.OpenForms)
    {
            if (f.Name != "HomePage") //change the name to your home page form (its actual name)!
                   f.Close();
    }
    


    This way you will close all previous opened forms, except the main one (home page called).

    And after this code, you can re-start the code ones again.


    Mitja
    Thursday, September 15, 2011 4:20 PM
  • BoovendanM .. I have to use the object, bcoz it keeps the values if I press the back button ...

    Mitja.. It does not work either :,( but you understand what I mean exactly... I wanna hide the values to put new ones :S

     

    Thanks,

    Learner 2011

    Thursday, September 15, 2011 5:05 PM
  • Now I actually dont understandy any longer. What values are you talking about? Please tell us more.
    Mitja
    Thursday, September 15, 2011 5:09 PM
  • Each form the user has to enter values in it, then he press next button and goes to the next form and also enter values and so on. the user can use next and back button to transfer between the forms, till he reaches the last form and got the total. in the form where the total appears there is a restart button if the user wants to redo the test. If he press the restart button all the forms appear with the last values that the user has been entered. I want to clear these values each time the user press the restart button.

     

    Thanks,

    Learner 2011

    Thursday, September 15, 2011 5:32 PM
  • Lets start this project a bit differently. I would create a new class, which will have all the properties, that are values in all the forms.

    So when user enters some value on some form, it will SET (using set accessor) the property in that class (and so on for all the values). When you want to "reset" values, simply Set all the value to zero (for numbers), or string.Empty (for strings). 

    What you have to do, each time you come to some form, is to read the coresponding values (those properties that belong to form), so they will fill the controls on forms (for this you will use Get accessor).

     

    What do you thing? Can you do that, or you need some help?


    Mitja
    • Proposed as answer by Alex_Lee Thursday, September 15, 2011 6:55 PM
    • Marked as answer by Jackie-SunModerator Monday, September 26, 2011 6:27 AM
    Thursday, September 15, 2011 6:12 PM
  • Thanks Ehsan - a neat way to implement a Wizard.  I wouldn't have thought of doing it this way, but quickly knocked up a little demo application to try it out which worked just fine.  I shall keep it as an aide memoire for the future.
    Friday, September 16, 2011 1:07 PM
  • Whenever you click the start button, create a bunch of new forms, and keep them in a variable or something until the test is done. When the test is done, close the old forms.
    Friday, September 16, 2011 1:20 PM