Show Dialog box at center of its parent - c#

It's been a mess to show a DialogBox at the center of its parent form. Here is a method to show a dialog.
I am positioning its parent to center but not able to center the DialogBox
private void OpenForm(Object point, Object height, Object width)
{
FormLoading frm = new FormLoading();
Point temp = (Point)point;
Point location = new Point(temp.X + (int)((int)width) / 2,
temp.Y + (int)((int)height) / 2);
frm.Location = location;
frm.ShowDialog();
}
private void btnView_Click(object sender, EventArgs e)
{
try
{
ThreadStart starter= delegate { OpenForm(currentScreenLocation,
this.Height, this.Width); };
Thread t = new Thread(starter);
t.Start();
////// Some functionality here...
t.Abort();
}
catch (Exception)
{
}
}

You might want to check the Form.StartPosition property.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx
something along the lines of:
private void OpenForm(Form parent)
{
FormLoading frm = new FormLoading();
frm.Parent = parent;
frm.StartPosition = FormStartPosition.CenterParent;
frm.ShowDialog();
}
This of course requires setting the form's parent.

form1.StartPosition = FormStartPosition.CenterScreen;
See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition(v=vs.110).aspx

if you are making a custom MessageBox,you can simply put this:
CenterToParent();
in your custom MessageBox formload() method.

In addition, if you want to set up arbitrary location you can use this
FormLoading frm = new FormLoading();
Point location = new Point(300, 400);
frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
frm.Location = location;
frm.ShowDialog();

NewForm.Show();
NewForm.Top = (this.Top + (this.Height / 2)) - NewForm.Height / 2;
NewForm.Left = (this.Left + (this.Width / 2)) - NewForm.Width / 2;

Related

Open a Form under a DataGridView Cell

I try to open a form just below a DataGridView header cell. I have this (and it is not working):
private void button1_Click(object sender, EventArgs e)
{
Form aForm = new Form();
aForm.Text = #"Test";
aForm.Top = this.Top + dataGridView1.Top - dataGridView1.GetCellDisplayRectangle(0, 0, false).Height;
aForm.Left = this.Left + dataGridView1.GetCellDisplayRectangle(0, 0, false).Left;
aForm.Width = 25;
aForm.Height = 100;
aForm.ShowDialog();
}
I don't see how to get the right top and left based on the DataGridView cell.
Should you consider to use a Form, you have to calculate its Location using Screen coordinates:
Form form = new Form();
form.StartPosition = FormStartPosition.Manual;
form.FormBorderStyle = FormBorderStyle.FixedSingle;
form.Size = new Size(dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Width, 100);
Point c = dataGridView1.PointToScreen(dataGridView1.GetCellDisplayRectangle(
dataGridView1.CurrentCell.ColumnIndex,
dataGridView1.CurrentCell.RowIndex, false).Location);
form.Location = new Point(c.X, c.Y);
form.BringToFront();
form.Show(this);
If you find youself in trouble using a Form, you might consider using a Panel instead:
Point c = dataGridView1.PointToScreen(dataGridView1.GetCellDisplayRectangle(
dataGridView1.CurrentCell.ColumnIndex,
dataGridView1.CurrentCell.RowIndex, false).Location);
Point r = this.PointToClient(c);
panel1.Location = new Point(r.X, r.Y);
panel1.BringToFront();
Take also a look at How do I to get the current cell position x and y in a DataGridView?
and Position new form directly under Datagridview selected row of parent

Set form location c#

I would like to know how to set my form position. i have tried to do the following:
this.Location = Point;
or:
Form2.Left = Point.X;
Form2.Top = Point.Y;
Form2.ShowDialog();
This is does not work. What do I do wrong?
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form();
form2.StartPosition = FormStartPosition.Manual;
form2.Left = 500;
form2.Top = 500;
form2.ShowDialog();
}
to set the position programmatically , you should set StartPosition to FormStartPosition.Manual as shown below :
Form myform = new Form()
{
Size = new Size(200,200),
StartPosition = FormStartPosition.Manual,
Location = new Point(10,10) // or Cursor.Position if you want to set it to cursor position
}
Try this:
private void Form_Load(object sender, EventArgs e)
{
this.SetDesktopLocation(x, y);
}
The first approach of you works anyways.
this.Location = new Point(/*XPosition*/, /*YPosition*/);
You can set it like this:
form1.Location = new Point(4, 370);
// Point(specify location of x, specify location of y)
// with object initializer
var frmUsers = new FrmUsers
{
StartPosition = FormStartPosition.Manual,
Location = new Point(0, 0)
};
// or
var frmUsers = new FrmUsers();
frmUsers.StartPosition = FormStartPosition.Manual;
frmUsers.Location = new Point(0, 0);

Cursor Position relative to Application

I know how to get the cursor's position :
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
But this is relative to the screen. How do i get the coordinates relative to my Form?
Use the Control.PointToClient method. Assuming this points to the form in question:
var relativePoint = this.PointToClient(new Point(X, Y));
Or simply:
var relativePoint = this.PointToClient(Cursor.Position);
I would use PointToClient like this:
Point p = yourForm.PointToClient(Cursor.Position);
//if calling it in yourForm class, just replace yourForm with this or simply remove it.
How about trying like this using the Control.PointToClient:-
public Form()
{
InitializeComponent();
panel = new System.Windows.Forms.Panel();
panel.Location = new System.Drawing.Point(90, 150);
panel.Size = new System.Drawing.Size(200, 100);
panel.Click += new System.EventHandler(this.panel_Click);
this.Controls.Add(this.panel);
}
private void panel_Click(object sender, EventArgs e)
{
Point point = panel.PointToClient(Cursor.Position);
MessageBox.Show(point.ToString());
}

How do i close the Form?

I have this code which Show/open a new Form :
In the gkh_Keydown event when i click on Ctrl + M it's showing/opening the new Form .
Now i want to do that when i click again Ctrl + M it will close the new Form .
When i click once to open the new Form it's going to this Form first :
public MagnifierMainForm(bool showMain)
{
InitializeComponent();
if (showMain == true)
{
GetConfiguration();
//--- My Init ---
FormBorderStyle = FormBorderStyle.None;
TopMost = true;
StartPosition = FormStartPosition.CenterScreen;
mImageMagnifierMainControlPanel = Properties.Resources.magControlPanel20061222;
if (mImageMagnifierMainControlPanel == null)
throw new Exception("Resource cannot be found!");
Width = mImageMagnifierMainControlPanel.Width;
Height = mImageMagnifierMainControlPanel.Height;
HotSpot hsConfiguration = new HotSpot(new Rectangle(50, 15, 35, 30));
hsConfiguration.OnMouseDown += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseDown);
hsConfiguration.OnMouseUp += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseUp);
hsConfiguration.OnMouseMove += new HotSpot.MouseEventDelegate(hsConfiguration_OnMouseMove);
HotSpot hsMagnfier = new HotSpot(new Rectangle(10, 15, 30, 30));
hsMagnfier.OnMouseMove += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseMove);
hsMagnfier.OnMouseDown += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseDown);
hsMagnfier.OnMouseUp += new HotSpot.MouseEventDelegate(hsMagnfier_OnMouseUp);
HotSpot hsExit = new HotSpot(new Rectangle(95, 20, 15, 15));
hsExit.OnMouseUp += new HotSpot.MouseEventDelegate(hsExit_OnMouseUp);
mHotSpots.Add(hsConfiguration);
mHotSpots.Add(hsMagnfier);
mHotSpots.Add(hsExit);
ShowInTaskbar = false;
this.Show();
}
else
{
GetConfiguration();
int x = mLastCursorPosition.X;
int y = mLastCursorPosition.Y;
MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPosition);
magnifier.Show();
}
}
And since i did false it's doing the else part :
GetConfiguration();
int x = mLastCursorPosition.X;
int y = mLastCursorPosition.Y;
MagnifierForm magnifier = new MagnifierForm(mConfiguration, System.Windows.Forms.Cursor.Position);//mLastCursorPosition);
magnifier.Show();
magnifier.Show(); do show the new Form .
Now i want that if i do Ctrl + M again it will close the Form magnifier.Show();
So in the Form in the gkh_KeyDown event in the else part i did :
magnifierform.Close();
Added a new variable this time only for magnifierform and try to close it.
So in magnifierform i did :
public MagnifierForm(Configuration configuration, Point startPoint)
{
InitializeComponent();
//--- My Init ---
mConfiguration = configuration;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = mConfiguration.ShowInTaskbar;
TopMost = mConfiguration.TopMostWindow;
Width = mConfiguration.MagnifierWidth;
Height = mConfiguration.MagnifierHeight;
// Make the window (the form) circular
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
mImageMagnifier = Properties.Resources.magnifierGlass;
mTimer = new Timer();
mTimer.Enabled = true;
mTimer.Interval = 20;
mTimer.Tick += new EventHandler(HandleTimer);
mScreenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
mStartPoint = startPoint;
mTargetPoint = startPoint;
if (mConfiguration.ShowInTaskbar)
ShowInTaskbar = true;
else
ShowInTaskbar = false;
}
public MagnifierForm()
{
}
Added another instance that does nothing sincei only want to close it.
But it never closed .
magnifierform is a variable of the MagnifierForm form i want to close it directly and not as before i used the other form MagnifierMainForm.
I just want to close it but it never closed. I used a breakpoint on the line :
magnifierform.Close();
On second Ctrl + M it's getting there but it dosen't close the MagnifierForm . Just does nothing.
EDIT
Tried something else now in the MagnifierForm i added :
public MagnifierForm()
{
this.Close();
}
In Form1 in the gkh_KeyDown event in the else side i changed it to :
else
{
magnifierform = new MagnifierForm();
}
So the instance i'm doing it on the second Ctrl + M
And again the breakpoint stop there but when i do continue it's not closing the Form .
So, if MagnifierMainForm() is already open you want to close it?...otherwise create a new instance and show it? In your code that fires when Ctrl+M is pressed, do something like:
Form frmToClose = null;
foreach (Form frm in Application.OpenForms)
{
if (frm is MagnifierMainForm)
{
frmToClose = frm;
break;
}
}
if (frmToClose != null)
{
frmToClose.Close();
}
else
{
// create a new instance of MagnifierMainForm() and display it
}
use this method form.Dispose();

How can I keep my first button from disappearing as a create a new one?

I'm trying to draw out pictureboxes during runtime as I can do right from the toolbox. That is, set the location at the mouselocation, resize it as I hold down the button and drag it across the form. All that I've accomplished in the code. But as I start the draw the second picturebox the first one disappears, I want to keep adding more pictureboxes to the form, if I remove the MouseMove event and move PictureBox pb1 = new PictureBox(); down to the MouseDown event it lets me add more buttons, but then I can't resize them obviously.
int cellSize = 10;
int numOfCells = 500;
PictureBox pb1 = new PictureBox();
int Mx, My;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
}
public void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
Mx = p.X;
My = p.Y;
int xSnap = (Mx / cellSize) * cellSize;
int ySnap = (My / cellSize) * cellSize;
pb1.BackColor = (Color.Red);
if (e.Button == MouseButtons.Left)
{
pb1.Size = new Size(xSnap - pb1.Left, ySnap - pb1.Top);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
Mx = p.X;
My = p.Y;
int xSnap = (Mx / cellSize) * cellSize;
int ySnap = (My / cellSize) * cellSize;
pb1.Location = new Point(xSnap, ySnap);
pictureBox1.Controls.Add(pb1);
}
You're always re-using the same PictureBox instance.
You need to create a new instance every time you want to add a new one, by writing new PictureBox().

Categories

Resources