6

I am designing a multipage windows form using panels.

I'm displaying a login form and validating the button click, and want to hide the login panel and show the main panel.

However, when I click the button, the login panel disappears alright, but the main panel does not appear. since there is nothing to display, the form window shrinks to just the minimize/maximize/close buttons.

Here's the code for the button:

private void btn_login_Click(object sender, EventArgs e)
{
    if (pwdBox.Text == optopwd)
    {
        MessageBox.Show("Good Morning!!");
        loginpanel.Visible = false;
        mainpanel.Visible = true;
    }
    else MessageBox.Show("Incorrect password!");

    pwdBox.Text = "";      
}

Please let me know what I have missed/misunderstood. Thanks!

Edit: Screenshots: Login Screen: http://img641.imageshack.us/img641/9310/loginscreenj.jpg

Empty window: http://img163.imageshack.us/img163/1376/emptyx.jpg

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
mankand007
  • 952
  • 2
  • 10
  • 22
  • how do I add print screens? is there any specific website to upload the images to? – mankand007 Feb 06 '12 at 20:05
  • Did you remember to add the panel to the form's controls? – ispiro Feb 06 '12 at 20:07
  • yeah.. the mainpanel is visible behind the login form if i don't set its visibility to false during design. but once the login page disappears, this panel disappears too... – mankand007 Feb 06 '12 at 20:17
  • If you want to inform someone that you responded to his comment, add @hisname before your comment. To respond to me you should have written: "@ispiro yeah.. the mainpanel"... – ispiro Feb 06 '12 at 20:29

3 Answers3

26

The standard mistake is that you accidentally put the mainpanel inside the loginpanel. So when you make loginpanel invisible, the mainpanel can never become visible. This accident is common in the designer, it won't let you put two panels on top of each other. You fix it with View + (Other Windows) + Document Outline. Drag mainpanel and drop it on the form. You'll have to fix the Location property by editing it in the Properties window instead of moving the panel with the mouse.

An entirely different approach is to use a TabControl. Easy in the designer, you just need to hide the tabs at runtime. Code is here.

Or use two UserControls.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Looks like your for is automatically resizing. There are 2 properties on the form responsible for auto size:

AutoSize = True;
AutoSizeMode = GrowAndShrink;

If you have the above settings then your form would shrink just to control panel (buttons) if there's nothing else to display.

Let me know if that helps.

UPDATED

also... does your control "pwdBox" belong to main panel?

Sebastian Siek
  • 2,045
  • 17
  • 16
  • I maximized the form to see if the contents of the mainpage are displayed that way, but all it shows is an empty form. – mankand007 Feb 06 '12 at 20:29
-4

Two suggestions: Try setting the height attribute to 100%

mainpanel.Height = 100%

If that doesn't work, ensure that the page isn't initializing with mainpanel.visible set to false on a postback.

Dredj
  • 137
  • 1
  • 2
  • 10