I have made a sidebar that I dock on the left side of my application.
Now I'm wondering what's the best way to show a form based on the menu option they've selected from my sidebar.
This is basically what I want to do:
http://www.dreamincode.net/forums/topic/176782-building-an-application-poscash-register-part-one/
On the left is my menu bar, on the right side I want to have a form based on the option clicked on the left.
I have looked into MDI but when I do that I always get a ControlBox even though I've disabled it in the child form.
Update: Seems that this works as well:
Looks like you can also create your own user controls to do it:
User controls
If you want second wont allow you to access your main form unless its closed.then use this...
second_form_name sfn = new second_form_name();
sfn.ShowDialog();
If you want second allow you to access your main form unless its closed.then use
second_form_name sfn = new second_form_name();
sfn.Show();
First create a panel in your form that will hold your current form's contents (add it to the form but leave it empty for now).
Panel active = new Panel();
this.Controls.Add(active);
Then create a panel containing controls for each form you want to display.
Panel firstFormPanel = new Panel();
firstFormPanel.Add(new Button());
Panel secondFormPanel = new Panel();
secondFormPanel.Add(new Button());
Now assign which panel you want by default:
active = firstFormPanel;
Then when you want to change to a new form (click event in sidebar), assign one of the panels to the active panel like so:
active.Visible = false;
active = secondFormPanel;
active.Visible = true;
Here's a little example i have of a sidebar from my game im making being able to slide a new submenu into visual range. I use a timer to control movement and a list of submenus also a index to determine which to show. as far as i've gotten along theres only one so far so not a prime example.
public List<UserControl> Submenus = new List<UserControl>();
Multiplayer_Menu MPM;
enum At { Left, Right }
At Current = At.Right;
At Go_to = At.Right;
int Submenu_Index = 0;
bool done = false;
public void Load_Submenus()
{
Multiplayer_Menu MM = new Multiplayer_Menu(this);
MainMenu.Controls.Add(MM);
MM.Location = new Point(MainMenu.Size.Width, 0);
MM.Visible = false;
Submenus.Add(MM);
PictureBox PB = new PictureBox();
MainMenu.Controls.Add(PB);
PB.Location = new Point(MainMenu.Size.Width, 0);
PB.Size = new System.Drawing.Size(924, 736);
PB.SizeMode = PictureBoxSizeMode.StretchImage;
PB.ImageLocation = "http://www.taloreal.com/Earth%20Rotation/Rotating.gif";
PB.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
Load_Submenus();
}
public void MML_Multiplayer_Click(object sender, EventArgs e)
{
Submenus[Submenu_Index].Visible = false;
if (Current == At.Left)
Go_to = At.Right;
if (Current == At.Right)
Go_to = At.Left;
ShowHideMenus.Enabled = true;
Submenu_Index = 0;
}
private void ShowHideMenus_Tick(object sender, EventArgs e)
{
Point P = new Point(MainMenu.Location.X, MainMenu.Location.Y);
Size S = new Size(MainMenu.Size.Width, MainMenu.Size.Height);
if (Go_to == At.Left)
{
P = new Point(P.X - 30, P.Y);
S = new Size(S.Width + 30, S.Height);
if (P.X == 0)
{
Submenus[Submenu_Index].Visible = true;
ShowHideMenus.Enabled = false;
Current = Go_to;
}
}
if (Go_to == At.Right)
{
P = new Point(P.X + 30, P.Y);
S = new Size(S.Width - 30, S.Height);
if (P.X == 930)
{
ShowHideMenus.Enabled = false;
Current = Go_to;
}
}
Reposition_Menu(P, S);
}
void Reposition_Menu(Point P, Size S)
{
MainMenu.Location = P;
MainMenu.Size = S;
}
i tried to create a form instance with in another form and then add that form into main form. but the form which i added that is not showing. i want to show that form at center at top of all controls.
here is my code
BBA.Controls.ExecludeSpecialist ucExecludeSpecialist = null;
Form frmContainer = null;
private void btnExclude_Click(object sender, EventArgs e)
{
if (ucExecludeSpecialist != null)
{
if (frmContainer != null)
{
frmContainer.Controls.Remove(ucExecludeSpecialist);
ucExecludeSpecialist = null;
}
}
if (frmContainer != null)
{
this.Controls.Remove(frmContainer);
frmContainer = null;
}
frmContainer = new Form();
frmContainer.ControlBox = false;
frmContainer.StartPosition = FormStartPosition.Manual;
frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
ucExecludeSpecialist = new BBA.Controls.ExecludeSpecialist();
ucExecludeSpecialist.SaveClicked +=
new BBA.Controls.ExecludeSpecialist.SaveComplete(OnSaveClicked);
ucExecludeSpecialist.CloseClicked +=
new BBA.Controls.ExecludeSpecialist.CloseComplete(OnCloseClicked);
ucExecludeSpecialist.BringToFront();
frmContainer.Height = ucExecludeSpecialist.Height;
frmContainer.Width = ucExecludeSpecialist.Width;
//frmContainer.Top = this.Height - frmContainer.Height / 2;
//frmContainer.Left = this.Height - frmContainer.Height / 2;
frmContainer.BringToFront();
frmContainer.TopLevel = false;
frmContainer.Controls.Add(ucExecludeSpecialist);
this.Controls.Add(frmContainer);
}
please guide me how to show that form on top of all control of another form at center. thanks
If I understand your comment correct, your problem is that a DataGrid overlays your recently added form? Try :
After you have add
frmContainer.Show();
your Form shoul be visible. After that you should solve your problem, if you call ucExecludeSpecialist.BringToFront(); after calling frmContainer.Show();
Example :
private void button1_Click(object sender, EventArgs e)
{
frmContainer = new Form();
frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmContainer.Height = this.Height / 2;
frmContainer.Width = this.Width / 2;
frmContainer.BackColor = Color.Red;
frmContainer.TopLevel = false;
this.Controls.Add(frmContainer);
frmContainer.Show();
frmContainer.BringToFront();
}
I didn't find a way in C# for creating a button which will support opacity - instead of just appearing when show method is called, to have the ability to slowly fade it into view.
I created my own button and I would like to know what you think of the implementation.
Basically, I created a Windows Form, which supports opacity property and handled all the corner cases regarding "adding" a form to another form, specifically:
- location changed event
- the owner form lost focus
The form consist of a label, which represents the button's text and that's all.
The form constructor gets the text for the label, the desired size of the button and the speed (based on an Enum) for the button appear.
In the form load method the label is being located in the middle of the button
when the label or the form itself clicked a simple graphic is performed and an event is raised to whoever catches it.
My Code:
Created a Form - named buttonForm
Constructor
InitializeComponent();
this.Owner = owner;
_buttonText = buttonText;
_buttonSize = buttonSize;
_usedSpeedOpacity = SelectSpeedOpacity(showSpeed);
A method to illustrate click command - being called when the user clicks on the form and on the label itself:
this.Location = new Point(this.Location.X + 1, this.Location.Y);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Thread.Sleep(50);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Location = new Point(this.Location.X - 1, this.Location.Y);
if (Button_Clicked != null)
{
Button_Clicked();
}
form load - locating the label in the middle of the control etc
labelButtonText.Text = _buttonText;
this.Size = _buttonSize;
double remainning = this.Width - labelButtonText.Size.Width;
Point labelNewLocation = new Point(
(int)(remainning / 2),
(int)(this.Height / 2 - this.Font.Height / 2));
labelButtonText.Location = labelNewLocation;
FadeShow (and FadeHide the same)
int tempCounter = 0;
Opacity = 0;
Show();
while (tempCounter <= 1000)
{
if (Opacity == 1.0)
{
break;
}
if (tempCounter % 10 == 0)
{
Opacity += _usedSpeedOpacity;
}
Refresh();
tempCounter++;
}
this.Visible = true;
this.BringToFront();
Update location method so when the parent form moves i call this method
this.Location = new Point(
this.Location.X - (ParentFormLocation.X - newLocation.X),
this.Location.Y - (ParentFormLocation.Y - newLocation.Y));
ParentFormLocation = newLocation;
An event of my button_click
Thanks in advance,
Oz.
I did something similar with a windows form that changed its height. The way I did it was to implement a timer within the code so every half a second the opacity changed.
While the below is not a 100% correct answer without your code its difficult to show you.
void timer_Tick(object sender, EventArgs e)
{
if (btnName.Opacity < 100)
{
btnName.Opacity++;
timer2.Stop();
timer2.Interval = 5000;
timer2.Start();
} else {
timer2.Stop();
}
}
In the following code, only the second method works for me (.NET 4.0). FormStartPosition.CenterParent does not center the child form over its parent.
Why?
Source: this SO question
using System;
using System.Drawing;
using System.Windows.Forms;
class Program
{
private static Form f1;
public static void Main()
{
f1 = new Form() { Width = 640, Height = 480 };
f1.MouseClick += f1_MouseClick;
Application.Run(f1);
}
static void f1_MouseClick(object sender, MouseEventArgs e)
{
Form f2 = new Form() { Width = 400, Height = 300 };
switch (e.Button)
{
case MouseButtons.Left:
{
// 1st method
f2.StartPosition = FormStartPosition.CenterParent;
break;
}
case MouseButtons.Right:
{
// 2nd method
f2.StartPosition = FormStartPosition.Manual;
f2.Location = new Point(
f1.Location.X + (f1.Width - f2.Width) / 2,
f1.Location.Y + (f1.Height - f2.Height) / 2
);
break;
}
}
f2.Show(f1);
}
}
This is because you are not telling f2 who its Parent is.
If this is an MDI application, then f2 should have its MdiParent set to f1.
Form f2 = new Form() { Width = 400, Height = 300 };
f2.StartPosition = FormStartPosition.CenterParent;
f2.MdiParent = f1;
f2.Show();
If this is not an MDI application, then you need to call the ShowDialog method using f1 as the parameter.
Form f2 = new Form() { Width = 400, Height = 300 };
f2.StartPosition = FormStartPosition.CenterParent;
f2.ShowDialog(f1);
Note that CenterParent does not work correctly with Show since there is no way to set the Parent, so if ShowDialog is not appropriate, the manual approach is the only viable one.
If you set the owner of the child form like so:
Form2 f = new Form2();
f.Show(this);
You can then center it easily like this:
Form2_Load(object sender, EventArgs e)
{
if (Owner != null)
Location = new Point(Owner.Location.X + Owner.Width / 2 - Width / 2,
Owner.Location.Y + Owner.Height / 2 - Height / 2);
}
I'm using this code inside my main form, hope it helps:
var form = new MyForm();
form.Show();
if (form.StartPosition == FormStartPosition.CenterParent)
{
var x = Location.X + (Width - form.Width) / 2;
var y = Location.Y + (Height - form.Height) / 2;
form.Location = new Point(Math.Max(x, 0), Math.Max(y, 0));
}
I found setting the location manually is the only reliable option in some more complex cases when form is auto-sized and dynamically modified.
However rather than computing the coordinates manually, I'd suggest using existing method:
this.CenterToParent();
I had the same problem, I eventually went with this:
protected override void OnActivated(EventArgs e) {
if(this.Modal == false && this.StartPosition == FormStartPosition.CenterParent) {
if(!(this.Owner is Form)) {
// Center to the last form opened before this one
int numforms = Application.OpenForms.Count;
this.Owner = Application.OpenForms[numforms - 2];
}
this.CenterToParent();
Application.DoEvents();
}
base.OnActivated(e);
}
Used as:
MyForm form = new MyForm();
form.Show(this); // with or without
The main advantage is that it does what your colleagues expect it to do, without requiring any hack in the calling form.
I found a solution that will center modeless window position to parent's position, and the child window can be still covered by parent window.
You just have to call
f2.Show(f1);
which will set f2 owner to f1, f2 will show over the f1 at it's center position.
Next you set
f2.Owner = null;
and there you go, f2 is a separate window, with correct startup position.
I realize this is an old question, but I was recently having the same problem and for reasons I won't get in to, I did not want to use the form.ShowDialog() method and my application was not an MDI application, therefore the CenterParent method was not having any effect. This is how I solved the problem, by computing the coordinates for the form that I wanted centered and triggering the new location in the main form's LocationChanged event. Hopefully this will help someone else having this problem.
In the example below, the parent form is called MainForm and the form I want centered in MainForm is called pleaseWaitForm.
private void MainForm_LocationChanged(object sender, EventArgs e)
{
Point mainFormCoords = this.Location;
int mainFormWidth = this.Size.Width;
int mainFormHeight = this.Size.Height;
Point mainFormCenter = new Point();
mainFormCenter.X = mainFormCoords.X + (mainFormWidth / 2);
mainFormCenter.Y = mainFormCoords.Y + (mainFormHeight / 2);
Point waitFormLocation = new Point();
waitFormLocation.X = mainFormCenter.X - (pleaseWaitForm.Width / 2);
waitFormLocation.Y = mainFormCenter.Y - (pleaseWaitForm.Height / 2);
pleaseWaitForm.StartPosition = FormStartPosition.Manual;
pleaseWaitForm.Location = waitFormLocation;
}
If you have a resizable parent form and you wanted your sub form to also be centered whenever the main form is resized, you should, in theory, be able to place this code in a method and then call the method on both the LocationChanged and SizeChanged events.
JYelton's answer worked for me, but the form is only centered the first time Show() is called.
If you want to Hide() the form, and then have it re-centered on the parent every time Show() is called you need use the following in your form:
public new void Show(IWin32Window owner)
{
base.Show(owner);
if (Owner != null)
Location = new Point(Owner.Location.X + Owner.Width / 2 - Width / 2,
Owner.Location.Y + Owner.Height / 2 - Height / 2);
}
Maybe this can help somebody.
Form frmMessage = new Form();
From experience, although they look similar, they behave different:
This variant doesn't work:
if (frmMessage.Parent != null)
frmMessage.CenterToParent();
else
frmMessage.CenterToScreen();
And this variant works
if (frmMessage.Parent != null)
frmMessage.StartPosition = FormStartPosition.CenterParent;
else
frmMessage.StartPosition = FormStartPosition.CenterScreen;
Using
form.Show(this);
throws an exception if you call it a second time. Manually setting the location seems to be the only reliable option :/ (wasn't it fairly recently that CenterParent used to work?)
just put the code in the constructor of your form.
public FrmSample()
{
InitializeComponent();
// must be after the InitializeComponent()
this.StartPosition = FormStartPosition.CenterParent;
}
Small Change to JYelton's answer
Form2_Load(object sender, EventArgs e)
{
if (Owner != null && Parent == null && StartPosition == FormStartPosition.CenterParent)
Location = new Point(Owner.Location.X + Owner.Width / 2 - Width / 2,
Owner.Location.Y + Owner.Height / 2 - Height / 2);
}
An old question, I know, but I had the same issue but for a different reason.
The Form I was opening had an overridden OnLoad method:
protected override void OnLoad(EventArgs e)
{
//... etc.
}
but was not calling the base implementation as it must do:
protected override void OnLoad(EventArgs e)
{
//... etc.
base.OnLoad(e);
}
When overriding OnLoad(EventArgs) in a derived class, be sure to call the base class's OnLoad(EventArgs) method so that registered delegates receive the event.
Building off the answer by deerchao,
if (form.StartPosition == FormStartPosition.CenterParent) {
form.Location = new Point(Location.X + (Width - form.Width) / 2, Location.Y + (Height - form.Height) / 2);
}
Do this after form.Show(), this will work on multi-monitors also
Is it possible to make a poup screen with high opacity around the popup screen in winform? If yes, how?
How do I make the pop up message or GUI screen to be in the middle of the computer screen?
Please remember that I don't have any source code yet.
An example:
To show your Form at center of screen, use StartPosition property of form to CenterScreen.
this.StartPosition = FormStartPosition.CenterScreen;
Now, to grey rest of the portion of screen.
Take a new form name it frmBlur and set these properties.
this.BackColor = SystemColors.ControlDark;
this.FormBorderStyle = FormBorderStyle.None;
this.Opacity = 0.8;
this.ShowInTaskbar = false;
this.TopMost = true;
this.WindowState = FormWindowState.Maximized;
Now, use the below code to display MessageBox or winform
private void button1_Click(object sender, EventArgs e)
{
using (frmBlur ob = new frmBlur())
{
ob.Show();
frmMessage f = new frmMessage();
f.TopMost = true;
f.ShowDialog();
}
}