Solved

Stop form Draging

Posted on 2010-08-17
17
589 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
  • 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 125 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now