hello. i am new to programming in C# and i'm having trouble positioning a modal form. i basically have a main form (frmFormA) set as a mdi parent, then a button is pressed on frmFormA and i open another form (frmFormB) at a particular location. frmFormB is opened up as follows:
FormB = new frmFormB();
FormB.MdiParent = this;
FormB.Show();
Now from frmFormB, a button is pressed and i want to open another form, but i want it to require the user to do something before it can close. So I am opening it like this:
FormC = new frmFormB();
FormC.ShowDialog();
When i do this, FormC doesn't go exactly on top of FormB. It is slightly higher and to the left even though i'm setting it to the same position as FormB. If i change my code to open FormC the same way as I open FormB, it places FormC where i want it.
I hope i have explained my problem clearly. If not, please let me know. I am using Visual Studio 2008 with the 3.5 framework. Can anyone tell me how to get past this? Thanks.
16 4835
FormB knows its own location and size.
You could pass this information to FormC when you create it.
Inside FormC use the passed information to manually set it's location.
FormC = new frmFormB(this.Location.X, this.Location.Y, this.Width, this.Height)
thanks for your reply. I have tried this and it works fine when I use Show(), but when i change it to ShowDialog(), the position is slightly off. I'm assuming the difference is b/c when i use ShowDialog(), I can't declare a parent. So I guess the coordinates on the screen are different when a form is associated to a parent versus when it is not? Does anyone have any workarounds for this? I can use Show(), but i wanted my application to stop until it receives a response from FormB. Thanks for any help.
Does anyone have any workarounds for this?
Did you try my suggestion of having the form set it's own location based on the location passed to it? If that didn't work as expected, please post the code you are using to set the location.
I just tried your suggestion which was very similar to what i was doing and it gave me the same result. Here is the actual code I'm using:
The Main form has a groupbox on it that I want to cover up when Form B is opened. I store the X and Y coordinates of the groupbox in a table so i can retrieve it in other forms. so Form B is launched using the following code in Form A:
ArtistsForm = new Artists();
ArtistsForm.MdiParent = this;
ArtistsForm.Show();
Within ArtistsForm, I am grabbing the X and Y coordinates and doing this:
this.Location = new Point(X_coordinate, Y_coordinate);
This code works great. It positions Form B right over the groupbox in Form A.
Now, from Form B I want to call another form. If I call it this way, it works great:
TypeOfCommentForm = new TypeOfComment();
TypeOfCommentForm.MdiParent = this.MdiParent;
TypeOfCommentForm.Show();
Within TypeOfCommentForm I am grabbing the X and Y coordinates and doing this:
this.Location = new Point(X_coordinate, Y_coordinate);
However, I really want to launch this from modally since i want to get the input from the user before moving on, so i try this:
TypeOfCommentForm = new TypeOfComment();
TypeOfCommentForm.ShowDialog();
this.Location = new Point(X_coordinate, Y_coordinate);
When I do this, the TypeOfCommentForm is in a slightly different location - a little higher and a little to the left even though the X and Y coordinates are the same. I really feel it has to do with Show() versus ShowDialog(), but i just can't find documentation telling me why it's different (I've spent hours searching). Thanks again for trying to help. Let me know if you need more information from me.
I just tried your suggestion which was very similar to what i was doing and it gave me the same result.
I don't see anywhere in your code where you are trying to create a new form AND PASSING IT THE DETAILS OF THE OLD FORM as I suggested - FormC = new frmFormB(this.Location.X, this.Location.Y, this.Width, this.Height);
The form you are making would have to have a constructor that is taking the four int values and using them to programmatically set its location. This has nothing to do with the mdi parent thing that you are trying to make do the work for you. - using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
// Default constructor
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
-
// Call to have the new form automatically center on the calling form
-
public Form1(int ParentX, int ParentY, int ParentWidth, int ParentHeight)
-
{
-
// Lets say I want this form centered on the form that created me
-
int myWidth = this.Width;
-
int myHeight = this.Height;
-
-
// My new location would be 1/2 the difference in width and height between the calling
-
// form and myself, added onto the location of the calling form
-
-
int NewX = ParentX + ((ParentWidth - myWidth) / 2);
-
int NewY = ParentY + ((ParentHeight - myHeight) / 2);
-
-
this.Location = new Point(NewX, NewY);
-
-
-
InitializeComponent();
-
}
-
-
//Button on the form to make a new form of this same kind
-
//Since it is of the same kind, of the same size, centered on the original
-
//
-
private void btnNewForm1_Click(object sender, EventArgs e)
-
{
-
Form1 myNewForm = new Form1(this.Location.X, this.Location.Y, this.Width, this.Height);
-
myNewForm.BackColor = Color.Firebrick; // Just so you can see the new one on top of the old one.
-
myNewForm.ShowDialog();
-
}
-
}
-
}
-
hello. i have coded a new project based on my understaning of the code you provided. I may still be missing something (I apologize in advance), but it seems that the Form I'm showing with ShowDialog() is still off a bit and it seems like it is off by the border size and Titlebar size. Here is the code I used. Please let me know if I'm still missing something. Thanks again.
I coded a Parent form and added a button. When the button is clicked, here is the code I'm using. - Form2 myNewForm = new Form2(this.Location.X, this.Location.Y, this.Width, this.Height);
-
myNewForm.MdiParent = this;
-
myNewForm.Show();
Here is all the code inside Form2: - using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace WindowsFormsApplication5
-
{
-
public partial class Form2 : Form
-
{
-
public Form2()
-
{
-
InitializeComponent();
-
}
-
-
// Call to have the new form automatically center on the calling form
-
public Form2(int ParentX, int ParentY, int ParentWidth, int ParentHeight)
-
{
-
// Lets say I want this form centered on the form that created me
-
int myWidth = this.Width;
-
int myHeight = this.Height;
-
-
// My new location would be 1/2 the difference in width and height between the calling
-
// form and myself, added onto the location of the calling form
-
-
int NewX = ParentX + ((ParentWidth - myWidth) / 2);
-
int NewY = ParentY + ((ParentHeight - myHeight) / 2);
-
-
this.Location = new Point(NewX, NewY);
-
-
-
InitializeComponent();
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Form2 myNewForm = new Form2(this.Location.X, this.Location.Y, this.Width, this.Height);
-
myNewForm.BackColor = Color.Firebrick; // Just so you can see the new one on top of the old one.
-
myNewForm.ShowDialog();
-
}
-
}
-
}
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
but it seems that the Form I'm showing with ShowDialog() is still off a bit and it seems like it is off by the border size and Titlebar size.
Let me be sure I understand what you are saying...
When the new form is made, it is not centered on the original form.
It seems to be off center horizonally by about 5 pixels (border size) and vertically by about 25 pixels (Titlebar size).
Does that sound right?
Can you post a screen snapshot showing both the calling form (parent) and the called form (new form) so I can see exactly what you see?
Sure. Here is the screen shot. The pixels seem to be off horizontally by about 5 pixels exactly as you said and vertically by about 31 pixels (I got the bordersize and titlebar size and kind of played with the numbers to get the two forms exactly even). Let me know if i didn't do the attachment correctly. Thanks!
I think the difference is that the first Form2 (not the red one) is not centered on your Form1
You could consider putting in a couple labels to show x, y, width and height on your form1 and form2 forms.
That way you can see precisely where each form is and its size.
Thank you. I will try that. Another interesting note though: In the example I created, if i change the Parent form to use ShowDialog() instead of Show(), the Form2 forms line up exactly. It seems to me that Show() and ShowDialog() interpret coordinates differently.
That's weird... Then again, That's Microsoft.
Have you set the form's Start Position property to 'manual'?
If it is still set to "WindowsDefault" or "ParentCenter" then maybe there is a struggle going on for which location setting has higher priority.
yes very strange. I do have the forms set to 'manual' I even tried doing it in the code itself and that doesn't work either. I still wonder if it could be the fact that I'm launching form 2 as a child and with Show(), but when I try to launch form 3 from form 2 as ShowDialog(), i can't make it a chlld. so the differences are:
1. 1 form is a child, the other is not
2. 1 form is being invoked with Show() and the other with ShowDialog(0
I will keep working on this and post an answer if i ever figure it out. thanks again for all the help.
ok i think i have it figured out. Form 1 is a parent, Form 2 is a child of that parent, and Form 3 is NOT a child of the parent. If i was to set the X and Y axis to 0,0 when i launch Form 2 (the child form), it will position itself at the 0,0 coordinates INSIDE the parent form. When I launch Form 3 (not a child) and set its X and Y axis to 0,0, it will position itself at 0,0 based on the screen so it will overlay the parent form. So the difference between the two is the size of the Parent forms titlebar and borders. So if i can get those values, i can use them to calculate where to put Form 3 so it will be right on top of Form 2 (the child form). I have seen 2 different ways to get these values:
Using SystemInformation
RectangleToScreen
I can start a new thred to see what everyone thinks is the best way to get the sizes. thanks!
Stick with
SystemInformation.FrameBorderSize.Width
and the other related functions in that namespace. That's its purpose and is aware of things like system themes etc.
And may I say "Very well done!" It's nice to see someone spend the time to experiment and go through the trial-and-error process to learn something new.
Thanks I appreciate that. I spent many, many hours trying to figure this out b4 asking for help. I have found many answers on this site over the past year. Hopefully this thread can help someone else out. Thanks again for your patience and help with this. Mike
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hector |
last post by:
I have a ComboBox set up in a non-modal form. When a selection is made from the ComboBox, the handler code closes the form, but then the system crashes because of an unhandled NullReferenceException....
|
by: Andrew |
last post by:
I get a Null Reference Exception if I close a non-modal dialog (that is, a
form opened with Show()) when a selection is made from a ComboBox. The error
message refers to Unsafe Native Methods, but...
|
by: Chris Thunell |
last post by:
I have a form with information on it, when a button is clicked, another form
is opened with the following code:
Dim myForm As New ReqVendorSelect
myForm.Visible = True
Once i'm done...
|
by: Tim Marsden |
last post by:
Hi,
I have a menu form.
The user selects an item from then menu and it shows the maintenance form
for the item.
This maintenance form is modeless so the user can select another item to
edit...
|
by: hagarwal |
last post by:
Hello
Can a modeless form be used as the "owner" of a modal form?
Here's the situation, from my MainForm, I have created a modeless form
using "Show" - and showed a modal form using...
|
by: dan.c.roth |
last post by:
oForm.Close() vs this.Close() in a modal dialog.
oFrom.Close() calls Form.Dispose() but this.Close() ,say in the click
event of the Form, does not.
I can think of the reason for this but is...
|
by: bern11 |
last post by:
If Form1 opens Form2 modally, how do I capture clicks on Form1 when
Form2 is open? I want to click on Form1 and read the mouse co-ordinates
into Form2. Since Form2 is open modally, Form1...
|
by: Wolfling |
last post by:
I have a VB6 MDI Application with a few MDI child windows always open. When the application opens a modeless non-MDI child window, and that child opens a Modal window (eg common dialog FileOpen...
|
by: =?Utf-8?B?Z2luYWNyZXNzZQ==?= |
last post by:
I am trying to close/dispose multiple instances of a form but because they
are modal and hidden, they do not show up in My.Application.OpenForms. They
must be modal, so making them modeless is not...
|
by: Mohit |
last post by:
Hi all,
I am working on a windows based client server application with
multiple forms. All forms are having custom title bars with no default
bars. There is one main form. Some forms are opened up...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |