[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 604
  • Last Modified:

Stop form Draging

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
RakeshBhandari
Asked:
RakeshBhandari
  • 9
  • 8
1 Solution
 
DhaestCommented:
Singleton pattern for MDI child forms (only allow 1 child to be open)
http://www.codeproject.com/KB/architecture/singletonforms.aspx
0
 
RakeshBhandariAuthor Commented:
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
 
DhaestCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
RakeshBhandariAuthor Commented:
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
 
DhaestCommented:
>>  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
 
DhaestCommented:
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
 
RakeshBhandariAuthor Commented:
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
 
DhaestCommented:
What language are you using ? VB.NET or C# ?
0
 
RakeshBhandariAuthor Commented:
I using c# and vs 2008
0
 
DhaestCommented:
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
 
RakeshBhandariAuthor Commented:

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
 
DhaestCommented:
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
 
RakeshBhandariAuthor Commented:
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
 
DhaestCommented:
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
 
RakeshBhandariAuthor Commented:
I has further question related to this . tell me i have to accept this as solution or continue asking question ?
0
 
DhaestCommented:
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
 
RakeshBhandariAuthor Commented:
good comments
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now