I have a winform that I would like to open on the centre of the parent form, which is already a mdiChild (i.e. I cannot set is mdiContainer on the parent). Below is the code I use. The form I create always opens on the top-left corner of whichever parent I assign to it, which is frustrating...
loadingCircle = new Loading(Title);
loadingCircle.TopLevel = false;
loadingCircle.Parent = this;
loadingCircle.Show();
loadingCircle.BringToFront();
I have got the StartPosition switched to CenterParent in the designer, however it does not seem to do anything...
Am I missing something obvious?
To get to the center of the screen,
You can use either :
loadingCircle.StartPosition = FormStartPosition.CenterScreen;
Or :
loadingCircle.ShowDialog();
or try this code to find center position:
Form loadingCircle = new frmLoading();
loadingCircle.StartPosition = FormStartPosition.Manual;
loadingCircle.Location = new Point(this.Location.X + (this.Width - loadingCircle.Width) / 2, this.Location.Y + (this.Height - loadingCircle.Height) / 2);
loadingCircle.Show(this);
Related
I want to show usercontrol as popup at mouse location.
However below code doesn't work for this;
System.Drawing.Point mousePos = System.Windows.Forms.Cursor.Position;
System.Drawing.Point formPos = this.Bounds.Location;
popupChart1.ultraLabel1.Text = e.DataRow.ToString() ;
popupChart1.Location = new System.Drawing.Point(mousePos.X - formPos.X, mousePos.Y - formPos.Y);
popupChart1.Show();
It doesn't throws any error, it just show it self at another place. What could be the reason ?
You should set the location after the form is rendered.
And if you want it to show at your mouse cursor position then simply use Cursor.Position instead of the math you did
popupChart1.Show();
popupChart1.Location = Cursor.Position;
OR
if you want to set the location before you show the window you can do
popupChart1.Location = Cursor.Position;
popupChart1.StartPosition = FormStartPosition.Manual;
popupChart1.Show();
I am having a panel in Winforms which loads panels in it during a method call.
In the method call I have written following code:
//to get number of panel present in main panel so that new panel position can be set
int counT = panel1.Controls.Count;
Panel p = new Panel();
p.Location = new Point(3, 3 + (counT * 197));
p.Size = new Size(280, 150);
//To add panel to parent panel
panel1.Controls.Add(p);
Every time I call the method it will load a panel in the main panel. Everything works fine if i didn't scroll the scroll bar. Once I scroll the Scroll bar to down and after that i call the method, the distance between panels increases.
As per logic written the distance between two panel should be 197 pixel along Y axis, but it is increasing by more.
I have set AutoScroll=true
Any help !!!
That's quite strange behavior which I didn't know until now (and I have a lot experience in WF). It can be seen when the parent panel is scrolled when the code above is executed. I was thinking that child control positions are relative to the ClientRectangle, but it turns out that they are accounting the DisplayRectangle.
Shortly, instead of this
p.Location = new Point(3, 3 + (counT * 197));
use this
var parentRect = panel1.DisplayRectangle;
p.Location = new Point(parentRect.X + 3, parentRect.Y + 3 + (counT * 197));
Panel.AutoScrollPosition affects Location property of all child controls. Scrolling works in this way. So you should keep in mind that, for example you could store current scroll position, move position to (0,0), add new controls, and restore scroll position after all
//to get number of panel present in main panel so that new panel position can be set
int counT = panel1.Controls.Count;
var pos = this.panel1.AutoScrollPosition; // Whe are storing scroll position
this.panel1.AutoScrollPosition = new Point(Size.Empty);
Panel p = new Panel();
p.Location = new Point(3, 3 + (counT * 197));
p.Size = new Size(280, 150);
p.BorderStyle = BorderStyle.FixedSingle;
//To add panel to parent panel
panel1.Controls.Add(p);
this.panel1.AutoScrollPosition = new Point(Math.Abs(pos.X), Math.Abs(pos.Y)); // We are restoring scroll position
I'm new user and work with Winform. I have a problem to position the child form dialog on main form application. I want move it to bottom right corner of main form window but my code don't work. I don't understand. Please help me .
basketForm = new Basket();
basketForm.Owner = this;
basketForm.Show();
Point pt = new Point(0, 0);
pt.X = this.Right - basketForm.Width;
pt.Y = this.Bottom - basketForm.Height;
pt = this.PointToScreen(pt);
basketForm.Location = pt;
You need set the point configuration before showing the window.
I have a small tool window that normally has the FormBorderStyle to FixedDialog with no caption text and no control box so it looks like a border-less form with a raised 3d effect.
When the user moves the mouse over the tool window it changes from this border-less FixedDialog mode to a SizableToolWindow w/ caption text and a control box.
The result is the client area moving.
The following code works but i do not want to hard code the top/left delta and I assume it is different depending on what theme/os the user has
void Reposition()
{
var topDelta = 12; // this number is wrong, i have not found the right number for aero yet
var leftDelta = 3;
if (this.Bounds.Contains(MousePosition))
{
if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
{
this.Location = new Point(this.Location.X - leftDelta, this.Location.Y - topDelta);
this.ControlBox = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
}
}
else
{
if (this.FormBorderStyle == System.Windows.Forms.FormBorderStyle.SizableToolWindow)
{
this.Location = new Point(this.Location.X + leftDelta, this.Location.Y + topDelta);
this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
}
}
}
Look into SystemParameters class. You will find the values you are hard-coding in your code there.
I work on VS 2008 with C#. This below code does not work for me. My form was designed in 1024 x 768 resolution.
Our clients laptop is in 1366 x 768 resolution. To solve this problem, I set below code in Form Load event:
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
but the form does not resize as per screen resolution and bottom of my form gets hidden or cut or I miss the scroll bar.
Is there any way to solve this problem? Please show me the syntax.
Can't you start maximized?
Set the System.Windows.Forms.Form.WindowState property to FormWindowState.Maximized
If you want to set the form size programmatically, set the form's StartPosition property to Manual. Otherwise the form's own positioning and sizing algorithm will interfere with yours. This is why you are experiencing the problems mentioned in your question.
Example: Here is how I resize the form to a size half-way between its original size and the size of the screen's working area. I also center the form in the working area.
On computers with multiple monitors, the user probably expects the form to open on the same screen that the mouse pointer is on. We can get it with Screen.FromPoint(Cursor.Position).
public MainView()
{
InitializeComponent();
StartPosition = FormStartPosition.Manual;
Rectangle screen = Screen.FromPoint(Cursor.Position).WorkingArea;
int w = Width >= screen.Width ? screen.Width : (screen.Width + Width) / 2;
int h = Height >= screen.Height ? screen.Height : (screen.Height + Height) / 2;
Location = new Point(screen.Left + (screen.Width - w) / 2, screen.Top + (screen.Height - h) / 2);
Size = new Size(w, h);
}
Note that setting WindowState to FormWindowState.Maximized alone does not change the size of the restored window. So the window might look good as long as it is maximized, but when restored, the window size and location can still be wrong. So I suggest setting size and location even when you intend to open the window as maximized.
Probably a maximized Form helps, or you can do this manually upon form load:
Code Block
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
And then, play with anchoring, so the child controls inside your form automatically fit in your form's new size.
Set the form property to open in maximized state.
this.WindowState = FormWindowState.Maximized;
int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;
this.ClientSize = new Size(w , h);
You can simply set the window state
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
simply set Autoscroll = true for ur windows form.. (its not good solution but helpful)..
try for panel also(Autoscroll property = true)
You can always tell the window to start in maximized... it should give you the same result... Like this: this.WindowState = FormWindowState.Maximized;
P.S. You could also try (and I'm not recommending this) to subtract the taskbar height.