473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to position a modal form on top of a modeless form

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.
Jan 25 '10 #1
16 4835
tlhintoq
3,525 Expert 2GB
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)
Jan 25 '10 #2
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.
Jan 25 '10 #3
tlhintoq
3,525 Expert 2GB
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.
Jan 25 '10 #4
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.
Jan 26 '10 #5
tlhintoq
3,525 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace WindowsFormsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         // Default constructor
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.  
  20.         // Call to have the new form automatically center on the calling form
  21.         public Form1(int ParentX, int ParentY, int ParentWidth, int ParentHeight)
  22.         {
  23.             // Lets say I want this form centered on the form that created me
  24.             int myWidth = this.Width;
  25.             int myHeight = this.Height;
  26.  
  27.             // My new location would be 1/2 the difference in width and height between the calling
  28.             // form and myself, added onto the location of the calling form
  29.  
  30.             int NewX = ParentX + ((ParentWidth - myWidth) / 2);
  31.             int NewY = ParentY + ((ParentHeight - myHeight) / 2);
  32.  
  33.             this.Location = new Point(NewX, NewY);
  34.  
  35.  
  36.             InitializeComponent();
  37.         }
  38.  
  39.         //Button on the form to make a new form of this same kind
  40.         //Since it is of the same kind, of the same size, centered on the original
  41.         //
  42.         private void btnNewForm1_Click(object sender, EventArgs e)
  43.         {
  44.             Form1 myNewForm = new Form1(this.Location.X, this.Location.Y, this.Width, this.Height);
  45.             myNewForm.BackColor = Color.Firebrick; // Just so you can see the new one on top of the old one.
  46.             myNewForm.ShowDialog();
  47.         }
  48.     }
  49. }
  50.  
Jan 26 '10 #6
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.

Expand|Select|Wrap|Line Numbers
  1. Form2 myNewForm = new Form2(this.Location.X, this.Location.Y, this.Width, this.Height);
  2. myNewForm.MdiParent = this;
  3. myNewForm.Show();
Here is all the code inside Form2:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication5
  11. {
  12.     public partial class Form2 : Form
  13.     {
  14.         public Form2()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         // Call to have the new form automatically center on the calling form 
  20.         public Form2(int ParentX, int ParentY, int ParentWidth, int ParentHeight)
  21.         {
  22.             // Lets say I want this form centered on the form that created me 
  23.             int myWidth = this.Width;
  24.             int myHeight = this.Height;
  25.  
  26.             // My new location would be 1/2 the difference in width and height       between the calling 
  27.             // form and myself, added onto the location of the calling form 
  28.  
  29.             int NewX = ParentX + ((ParentWidth - myWidth) / 2);
  30.             int NewY = ParentY + ((ParentHeight - myHeight) / 2);
  31.  
  32.             this.Location = new Point(NewX, NewY);
  33.  
  34.  
  35.             InitializeComponent();
  36.         } 
  37.  
  38.         private void button1_Click(object sender, EventArgs e)
  39.         {  
  40.             Form2 myNewForm = new Form2(this.Location.X, this.Location.Y, this.Width, this.Height);
  41.             myNewForm.BackColor = Color.Firebrick; // Just so you can see the new one on top of the old one. 
  42.             myNewForm.ShowDialog(); 
  43.         }
  44.     }
  45. }
Jan 26 '10 #7
tlhintoq
3,525 Expert 2GB
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.
Jan 26 '10 #8
tlhintoq
3,525 Expert 2GB
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?
Jan 26 '10 #9
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!
Attached Files
File Type: zip WindowsForm.zip (74.6 KB, 110 views)
Jan 26 '10 #10
tlhintoq
3,525 Expert 2GB
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.
Jan 26 '10 #11
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.
Jan 26 '10 #12
tlhintoq
3,525 Expert 2GB
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.
Jan 26 '10 #13
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.
Jan 26 '10 #14
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!
Jan 27 '10 #15
tlhintoq
3,525 Expert 2GB
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.
Jan 27 '10 #16
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
Jan 27 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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....
3
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...
6
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...
3
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...
3
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...
1
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...
10
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...
1
Wolfling
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...
4
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...
1
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...
1
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...
0
isladogs
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...
0
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...
0
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,...
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.