Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Two Forms

Two Forms

Scheduled Pinned Locked Moved C#
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    picasso2
    wrote on last edited by
    #1

    Have a very simple form (form2) that checks for the credentials. The following code works but it leaves the dialog (form2) open. I just want to display Form1 if credentials are good. if(textBox1.Text=="username") { //MessageBox.Show("good user, check password","Info"); if(textBox2.Text=="password") { //credentials are good, display Form1 Form1 firstForm = new Form1(); firstForm.ShowDialog(this); } else { MessageBox.Show("Bad user or password","Info"); } } else { MessageBox.Show("Bad user or password","Info"); } }

    P 1 Reply Last reply
    0
    • P picasso2

      Have a very simple form (form2) that checks for the credentials. The following code works but it leaves the dialog (form2) open. I just want to display Form1 if credentials are good. if(textBox1.Text=="username") { //MessageBox.Show("good user, check password","Info"); if(textBox2.Text=="password") { //credentials are good, display Form1 Form1 firstForm = new Form1(); firstForm.ShowDialog(this); } else { MessageBox.Show("Bad user or password","Info"); } } else { MessageBox.Show("Bad user or password","Info"); } }

      P Offline
      P Offline
      peshkunta
      wrote on last edited by
      #2

      This is very easy to do. I just need to understand a few things of what you are trying to do: 1. Does this check happen on start up? If yes, there is a better way to do this. 2. After the MessageBox tells you "Wrong password...etc. what do you want to happen - everything to close? 3. You seem to have only OK button n the MessageBox that displays wrong password... do you want the same thing to happen if a person presses OK or cancels the dialog? This will close and release all the resources associated with the form (including memory) the main form after the dialog has opened that tells the password is incorrect and OK is pressed or that dialog has exited. if (MessageBox.Show("Bad user or password", "Info") == DialogResult.OK) { this.Dispose(); } Let me know if this helps.:->

      P 1 Reply Last reply
      0
      • P peshkunta

        This is very easy to do. I just need to understand a few things of what you are trying to do: 1. Does this check happen on start up? If yes, there is a better way to do this. 2. After the MessageBox tells you "Wrong password...etc. what do you want to happen - everything to close? 3. You seem to have only OK button n the MessageBox that displays wrong password... do you want the same thing to happen if a person presses OK or cancels the dialog? This will close and release all the resources associated with the form (including memory) the main form after the dialog has opened that tells the password is incorrect and OK is pressed or that dialog has exited. if (MessageBox.Show("Bad user or password", "Info") == DialogResult.OK) { this.Dispose(); } Let me know if this helps.:->

        P Offline
        P Offline
        picasso2
        wrote on last edited by
        #3

        Hi peshkunta I started the main project then later I decided to add another form (form2)to check credentials. So what I did is change the code to point to second form2 -Application.Run(new Form2());- This causes the second form2 to be displayed first. Then if credentials are OK, form2 dispaears and form1 is displayed I tried this code using (Form1 f = new Form1()) { this.Hide(); f.ShowDialog(); } this.Show(); but when I close the main form (Form1), the first form (Form2) is displayed again.

        P 1 Reply Last reply
        0
        • P picasso2

          Hi peshkunta I started the main project then later I decided to add another form (form2)to check credentials. So what I did is change the code to point to second form2 -Application.Run(new Form2());- This causes the second form2 to be displayed first. Then if credentials are OK, form2 dispaears and form1 is displayed I tried this code using (Form1 f = new Form1()) { this.Hide(); f.ShowDialog(); } this.Show(); but when I close the main form (Form1), the first form (Form2) is displayed again.

          P Offline
          P Offline
          peshkunta
          wrote on last edited by
          #4

          I think you are making this harder than it has to be - your approach. The most straight forward and easier for you in the long run would be: 1. Leave Form1 to start in the Application.Run function 2. Go to the Form1 events and add an on Load event handler(function). In this function add your code to start and show your Form2 - the authentication form. This will make Form2 open first and get the focus (be in front). Although both forms will be open (not sure if this is a problem in your case) if you make it as modal with ShowDialog() - that way the person will have to work with Form2 and not be able to mess with Form1 until it's been authenticated. OR If you want when you close Form1, Form2 to not be displayed, go to the event handler for Closing in Form1 and tell it to close Form2 when form1 is closing and that should solve the problem. I would use Dispose().

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups