Click here to Skip to main content
15,887,413 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Consider there is a windows form.I need to give specific location(always) to this windows form(Ex: right top corner on desktop). If form will dragged by the user,after release the mouse, form must go to the same place. Please somebody help me.
Posted

First thing to do: change the Form property StartPostion to Manual.
Then, handle the LocationChanged event:
C#
private void frmMain_LocationChanged(object sender, EventArgs e)
    {
    Location = new Point(0, 0);
    }
You will get quick "flashes" when they try to move it, but the form will always be in the TLHC of the screen. For TRHC it's a little more complex:
C#
private void frmMain_LocationChanged(object sender, EventArgs e)
    {
    Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width, 0);
    }
And you will want to handle the Resize event as well.
 
Share this answer
 
Comments
NISHAN SANDEEPA 30-Nov-13 9:39am    
Thanks OriginalGriff...I need to get another thing.I need to give maximum width to the form(Ex:300px). When user try to maximize the form form height must be equal to the desktop height and form width must be equal to 300px.Please give me some advice or some chunk of code.
OriginalGriff 30-Nov-13 9:52am    
Handle the Resize event, and set the Width and Height properties!

(But be aware - you are going to annoy a lot of users like me, who like things where I put them, and the size I select...)
In this case I think you are not choosing the right design option for the task at hand.

If you want a Form to never be moved, then why allow it to be moved: why not get rid of the TitleBar through using various Form properties like FormBorderStyle, setting 'Text of the Form to an empty string, set the MinimizeBox, MaximizeBox, ControlBox, etc., to the appropriate values.

If the Form has a TitleBar, and you leave the ControlBox, MinimizeBox, and MinimizeBox, decorations in place, there is no way you are going to be able to cancel the user's ability to move the Form around: yes, you can "snap it" back in place by over-riding the Move, or LocationChanged EventHandlers, but the user is still going to be able to drag-around an empty Rectangle. This is not a good thing.

fyi: when a Form is moved, the 'Move Event is raised before the LocationChanged Event.

You could use the Windows API/pInvoke to prevent Form movement: less than 5 seconds using Google turned up this: "Creating a immovable windows’ form in c#" [^]. Note: I haven't tried this code.

Other issues you raise can be addressed by simply setting the Form's MinimumSize and MaximumSize Properties to be identical to the desired Size parameters of the Form.

But, as (I think) my esteemed mentor, and colleague, OriginalGriff, says, you are really going against the grain of Windows UI conventions here.
 
Share this answer
 
v3
Comments
Behzad Sedighzadeh 20-Sep-18 5:25am    
Very nice and elegant solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900