blah blah blah is here! blah blah » Close

up0down
link

I am sorry to ask such a seemingly abcd question but I have been trying in vain to hide/show forms. Also my main menu is made up of two forms, a main menu form is a central window, devoid of title bar, minimize, maximize and is mostly commands on a picture background. Then there is a black background form with some big text to cover the desktop, hence to assure that the desktop background don't interfere with the color picture/theme of my main menu. When the user click the first option and a new form appearing, I have difficulty hiding the main menu form and when I click somewhere in the background form, the active form just dissappear, actually it moves behind the background leaving the user with a black window.
Could anybody help on this???

last answered one year ago

2 answers

up1down
link

You need to keep a handle on your background window so that you can manage it. A good way to do this that I use is to make the form instances public in the static Program class. Here's an example of how this might work:

static class Program
{
public static frmBackground bkg;
public static frmMainMenu menu;
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bkg = new frmBackground();
bkg.Show();
// do what needs to be done to the background form here so it fills the screen etc.
menu = new frmMainMenu();
Application.Run(menu);
}
}


Then in your main menu form, you can just do something like:

this.WindowState = WindowState.Minimized; // minimize but don't hide the menu
Program.bkg.Hide(); // hide the background form
// Run some app here, or do something else.
Program.bkg.Show(); // bring the background form back to visible
this.WindowState = WindowState.Normal; // un-minimize
this.Focus(); //bring focus back to this window


To prevent the background form taking focus when it's clicked, you can add something to its Click event. It's crude, but it should do the trick...

public void frmMainMenu_Click(object sender, EventArgs e)
{
Program.menu.Focus();
}


Make sure also that your background form is not selectable in alt-tab nor does it show up in the taskbar. This prevents getting to it via the keyboard - at least easily.

A more elaborate way to do this would be by using an MDI application, and having an MDI parent form that houses the main menu as a child window. MDI forms naturally cannot have exclusive focus to the point of hiding all their child windows.

The background form will be automatically killed when you close the main form, since you executed Application.Run against the main menu form. You could of course also do it the "clean" way and manually issue "Program.bkg.Close()" on the background form when your program is exiting.

fm

Thank you very much. This and a new print out documents on form will assuredly help me. I am going to work out the shell of my program then I will be back to you. Thank you. From Nic.

hi, I have been unable to make the above program work. I believe that I must have wrongly placed the code. Could you please guide me to where should the code be pasted??? Thanks.

up0down
link

Well, here is a mock up of what a funtion to do this would be:

void FormShow(Form form)
{
this.Visuble = false;
form.ShowDialog();
//The code won't continue until this form is closed
this.Visible = true;
}


Hope this helps!

Feedback