?
Solved

Stop form Draging

Posted on 2010-08-17
17
Medium Priority
?
600 Views
Last Modified: 2012-05-10
I developing windows application using c# for codig. I has two questions

1) How stop draging of form inside parent(MDI)  form ?
2)How prevent  Opening  multiple form same time. I mean if already one form opened and i try  to open second form i need to minimize first form. How i achive this ?
0
Comment
Question by:RakeshBhandari
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33452853
Singleton pattern for MDI child forms (only allow 1 child to be open)
http://www.codeproject.com/KB/architecture/singletonforms.aspx
0
 
LVL 2

Author Comment

by:RakeshBhandari
ID: 33453030
Thanks for your response,

I think you not geting my question. Consider i has 2 from like   Form1 and Form2. Form1 is already opened. when i try to open Form2,  Form1 should minimize. and i expecting answer for first question.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33453069
1. Easiest solution:
On Form Properties, Set FormBorderStyle = None.
It will make your form haven't border and not able to move.

Creating a immovable windows’ form in c#
http://vaibhavgaikwad.wordpress.com/2006/06/05/creating-a-immovable-windows-form-in-c/

2. Minimize all MDI-childs (and then show th screen you want)

//Gets forms that represent the MDI child forms
//that are parented to this form in an array
Form[] charr= this.MdiChildren;
     
//For each child form set the window state to Maximized
foreach (Form chform in charr)
   chform.WindowState=FormWindowState.Minimized;

Source: http://www.codeproject.com/KB/cs/mdiformstutorial.aspx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:RakeshBhandari
ID: 33453248
Thanks for your Response,

I did your second option for moving window . But the problem is  window displaying top -left
only. i need  in center. how will do ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33453270
>>  did your second option for moving window

What did you try ?


System.Drawing.Point initialLocation;  
private void Form1_Load(object sender, System.EventArgs e)
{
this.StartPosition = FormStartPosition.CenterParent
initialLocation=this.Location;
} 
private void Form1_LocationChanged(object sender,System.EventArgs e) 
{
Me.Location = initLocation;
}

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33453277
If you try above, probably you'll need to add an test in the locationChanged-event
System.Drawing.Point initialLocation;  
private void Form1_Load(object sender, System.EventArgs e)
{
initialLocation = null;
this.StartPosition = FormStartPosition.CenterParent
initialLocation=this.Location;
} 
private void Form1_LocationChanged(object sender,System.EventArgs e) 
{
  if (!initialLocation is null) 
      Me.Location = initLocation;
}

Open in new window

0
 
LVL 2

Author Comment

by:RakeshBhandari
ID: 33461516
I geting following error when i building my application. related to your code. I using VS 2008      

Error      1      Type expected      
Error      2      ; expected
Error      3      Invalid expression term ')'
Error      4      ; expected
Error      7      Cannot convert null to 'System.Drawing.Point' because it is a non-nullable value type
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33461908
What language are you using ? VB.NET or C# ?
0
 
LVL 2

Author Comment

by:RakeshBhandari
ID: 33461926
I using c# and vs 2008
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33461954
Adjusted the code
        System.Drawing.Point? initialLocation;
        private void Form1_Load(object sender, System.EventArgs e)
        {
            initialLocation = null;
            this.StartPosition = FormStartPosition.CenterParent;
            initialLocation=this.Location;
        }
        private void Form1_LocationChanged(object sender, System.EventArgs e) 
        {
        if (initialLocation.HasValue)
            this.Location = initialLocation.Value;
        }

Open in new window

0
 
LVL 2

Author Comment

by:RakeshBhandari
ID: 33462655

Now this working fine. Initialy i asked two questions one is solved now. in second question i did as per   url. But In that we have to do explicit click to minimize window. I dont need that.
my requirment is when open Form2, Form1 should minimize ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33462723
I think that you'll need to loop through all your forms, to see what childform is of type form1 to minimize it


For Each f As Form In Me.MdiChildren
    If f.GetType Is GetType(Form1) Then
       f.WindowState=FormWindowState.Minimized
    End If
Next


FYI: actually it's not the purpose of asking multiple questions in one question !!!
0
 
LVL 2

Author Comment

by:RakeshBhandari
ID: 33462846
I did that also. But stll one problem  is remaining. Form1 is minimising when i open Form2. But
when i maximize(restore)  Form1 Form2 should minimize. which event i have to write this code?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33462879
you could use the Resize event of the form

(In the form code click the drop down at the top left and choose base class events, then on the right shoose resize)

Or, you could override the resize event
        Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        If Me.WindowState = FormWindowState.Minimized Then
            MsgBox("test")
        End If
    End Sub


You keep asking the one question after the other in one topic. That's not the purpose !!!
0
 
LVL 2

Author Comment

by:RakeshBhandari
ID: 33463003
I has further question related to this . tell me i have to accept this as solution or continue asking question ?
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 33463027
If the original question is solved, you should accept a solution. If you have a related question, you can always ask a new question, with a link to this question
0
 
LVL 2

Author Closing Comment

by:RakeshBhandari
ID: 33463088
good comments
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question