Windows Develop Bookmark and Share   
 index > Windows Forms General > Forms Location
 

Forms Location

Hello everyone;

All of my forms should be opened just at specific location (x,y) related to my base form. (All forms are opening showdialog)
I should get the location of my base form(I configured it to open at the center of windows regardless of screen resolution). So I just want to get location X and Y of my base form. I've done this but not applied;



MainForm MF = new MainForm();

xoff = MF.Location.X;

yoff = MF.Location.Y;

 


How can I cath its location and where do I write this snippet?

Thanks.

Ya_aa  Sunday, July 31, 2005 5:19 AM
Ya_aa,

Well, there is a way through PInvoke, but I have to say that I don't like the idea.

It's an user preference setting and a software should respect that and try to adaptate your needs following those settings, like resolution screen, colors, fonts, etc.

Think about that, maybe the best choice would be turning your controls position flexible or adaptate your forms when a special condition is found.

But, if you want to change the resolution anyway, there is an example above:

http://www.codeproject.com/csharp/CSDynamicScrRes.asp

Cheers,
Francisco B. Cerqueira Junior  Monday, August 01, 2005 3:49 AM
Ya_aa,

Every form has a location setting in the InitializeComponent() method, so when it runs, it sets the location configured for the derived form even if you didn't change this form's property.

You don't need to create another instance of the MainForm if your form inherits from it, so, just put the following code on the constructor of the derived form:

// It will save the location before InitializeComponent change it
Point loc = base.Location;
InitializeComponent();
// And it will restore the correct location configured on your base form
this.Location = loc;

You said:
"...I configured it to open at the center of windows regardless of screen resolution..."

Another way to solve your problem is configure it in every form of your project:

this.StartPosition = FormStartPosition.CenterScreen;

You can write it on the constructor, after InitializeComponent, or in the Load event of your derived form, or just changing this property in the property window. Actually, I think it's the best way to perform this action.

Cheers,
Francisco B. Cerqueira Junior  Sunday, July 31, 2005 11:04 AM
FBCJunior

Thanks for your help. But I've got another question and it's about screen resolution. In fact I want to show my app at specific resolution of screen.
How can I change windows screen resolution programmatically?(for example 1024,768).

Any suggestion would be truly appreciated,
Thanks again.
Ya_aa  Sunday, July 31, 2005 6:08 PM
Ya_aa,

Well, there is a way through PInvoke, but I have to say that I don't like the idea.

It's an user preference setting and a software should respect that and try to adaptate your needs following those settings, like resolution screen, colors, fonts, etc.

Think about that, maybe the best choice would be turning your controls position flexible or adaptate your forms when a special condition is found.

But, if you want to change the resolution anyway, there is an example above:

http://www.codeproject.com/csharp/CSDynamicScrRes.asp

Cheers,
Francisco B. Cerqueira Junior  Monday, August 01, 2005 3:49 AM

You can use google to search for other answers

Custom Search

More Threads

• New to DataGrids - Simple Question!
• How do i disable Taskmanager(Ctl+Atl+Del), Task keys (Atl-TAB, Ctl+Esc, Atl+Esc),StartMenu, Taskbar in .Net?
• Problem calling event from control
• open form events
• KeyUp event incorrectly sent to form 'beneath' a MessageBox
• How to create a DLL file of a bunch of Text files or DTD files in C# ?
• Upgrading from VB.NET 2005 beta 1 to 2 - Threading & Invoking errors - Urgent
• Form Transparency and DirectX
• datagrid textbox to combo box
• Windows Form Text Box - Automatic Pasting to Notepad & Displaying Line Numbers