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
Solved

Stop form Draging

Posted on 2010-08-17
17
592 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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