Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stop form Draging

Posted on 2010-08-17
17
Medium Priority
?
602 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
Industry Leaders: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

963 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