locked
Some basic C# questions RRS feed

  • Question

  • In the process of learning C#, I have a small project that I am using as a learning excersise.  So far, the project uses 2 forms:

    A login form

    A project form

    The Login form gets launched via the Main entry point with the Application function that VS created.  On my login form, after the user has clicked submit, the form validates the user against a SQL database and once it has verified this, it instantiates a user credentials object that contains user name, authority (from the DB) and some user preference data (also taken from the DB).

    Following this it loads up the Project form like this:

    ProjectManager formProjects = new ProjectManager();

    formProjects.Show();

    formProjects.BringToFront();

    this.Hide();

    this.SendToBack();

    This all works fine.  My first of two questions is this:

    Question #1:  When the user quits the project form, I want to have the Login form re-displayed.  How do I reference the login form from the project form?

    Question #2: If I instantiate an object in my login form, how can I make it available to the project form?  As it is now, the object is not in the scope of the project form.

    Thank you all very much in advance for any assistance you can provide on these two issues!

     

    Sunday, August 20, 2006 10:50 PM

Answers

  • well as for Question 1, you can do this:

     

    one the credentials have been verified by the SQL database, when you are going to show the other form, you can either:

     

  • hide the login form, show the new form and pass a reference of your login form to the other form, so you can then restore it back on the form_closing event of the other form.

  • use ShowDialog() on the main form to show the other form which means that the user will not be able to access the login form at all in any way until the current form showing is closed, its like a thread blocking type system.

     

    I would stick with ShowDialog() personally.

    to use showdialog...here is an example:

     

    //main form:



  • if (this.DoLogin())
    {
       FrmOtherForm theOtherForm = new FrmOtherForm();
       theOtherForm.ShowDialog();
    }
    else
    {
       //handle your errors
    }

     

     

    as for the other approach, hiding/showing the form, here is an example:

     

    //main form:



    if (this.DoLogin())
    {
       FrmOtherForm theOtherForm = new FrmOtherForm(this);
       this.Hide();
       theOtherForm.Show();
       theOtherForm.BringToFront();
    }
    else
    {
       //handle your errors
    }

     

     

    //Other Form



    //global properties:
     
    private MainForm theMainForm = null;
     
    //Form constructor:
    public FrmOtherForm(MainForm mainForm)
    {
       this.theMainForm = mainForm;
    }
    ..
    //Form closing:
     
    this.theMainForm.Show();
    this.theMainForm.BringToFront();

     

     

    as for Question 2: use the above approach to do the same thing for passing objects from one class (form in this case) to another, by giving it via the constructor or setting it directly from a public property

    does this help?

Sunday, August 20, 2006 11:06 PM

All replies

  • well as for Question 1, you can do this:

     

    one the credentials have been verified by the SQL database, when you are going to show the other form, you can either:

     

  • hide the login form, show the new form and pass a reference of your login form to the other form, so you can then restore it back on the form_closing event of the other form.

  • use ShowDialog() on the main form to show the other form which means that the user will not be able to access the login form at all in any way until the current form showing is closed, its like a thread blocking type system.

     

    I would stick with ShowDialog() personally.

    to use showdialog...here is an example:

     

    //main form:



  • if (this.DoLogin())
    {
       FrmOtherForm theOtherForm = new FrmOtherForm();
       theOtherForm.ShowDialog();
    }
    else
    {
       //handle your errors
    }

     

     

    as for the other approach, hiding/showing the form, here is an example:

     

    //main form:



    if (this.DoLogin())
    {
       FrmOtherForm theOtherForm = new FrmOtherForm(this);
       this.Hide();
       theOtherForm.Show();
       theOtherForm.BringToFront();
    }
    else
    {
       //handle your errors
    }

     

     

    //Other Form



    //global properties:
     
    private MainForm theMainForm = null;
     
    //Form constructor:
    public FrmOtherForm(MainForm mainForm)
    {
       this.theMainForm = mainForm;
    }
    ..
    //Form closing:
     
    this.theMainForm.Show();
    this.theMainForm.BringToFront();

     

     

    as for Question 2: use the above approach to do the same thing for passing objects from one class (form in this case) to another, by giving it via the constructor or setting it directly from a public property

    does this help?

Sunday, August 20, 2006 11:06 PM
  • hi,

    you can use properties in the project form one for the caller, and other one for the object that you will pass to it

    after you creat instance of project form you can pass those variables to it

    ProjectManager formProjects = new ProjectManager();

    formProjects.caller = this;

    formProjects.someobject = this.myobject;

    formProjects.Show();

    also you can use the constractor of the projectform to pass variable add a new method somthing like this

    public formProjects(Form loginForm, object myObject)

    {

    intializeComponents();

    //set the properties

    Caller = loginForm;

    someObjects = myObject;

    }

    and you can instantiate you form project  like this

    ProjectManager formProjects = new ProjectManager(this, this.myobject);

    formProjects.Show();

    to show the hidden loginform again from project form you can use something like this because the login form is now a member in project form

    caller.show();

    hope this helps

    Sunday, August 20, 2006 11:08 PM
  • Excellent, ahmedilyas!  That does indeed answer my questions!  One more since I have your gracious attention :)

    Lets say I have 5 different objects that I needed to have the child form have access to - What is the best approach for that?  Make an array and pass the array?

    Sunday, August 20, 2006 11:32 PM
  • indeed, that would be the way, or pass each object to the other form as above!
    Sunday, August 20, 2006 11:59 PM
  • Thank you all SO much!  This resolved my issues nicely and now I am on to the next set of challenges!

    In the words of our govener here in CA, "I'll be back!"

    Monday, August 21, 2006 12:25 AM