I am using c# language with windows form. I open the child form on top of the main windows form. Is there a simple way to turn off the draggable feature in both forms? Or is it possible to move it at the same time as the main form?
Loading
I am using c# language with windows form. I open the child form on top of the main windows form. Is there a simple way to turn off the draggable feature in both forms? Or is it possible to move it at the same time as the main form?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Feb 12, 2024, 1:40 AM EST
Hey Mehmet Fatih, add the WndProc method in code behond file of your child forms.
Sam HobbsRank #107 · 20.5K reputationPosted Feb 10, 2024, 9:23 PM EST
I assume you are not using
MDIbut if you are then please say so. If the following does not apply then you probably need to be more specific about what you are doing.Is the child form owned by the main form? When you show the form are you using
Show()orShow(this)?Amit MohantyRank #112 · 20K reputationPosted Feb 12, 2024, 12:56 AM EST
Use below function in your child form to non draggable.
protected override void WndProc(ref Message m) { const int WM_NCLBUTTONDOWN = 161; const int WM_SYSCOMMAND = 274; const int HTCAPTION = 2; const int SC_MOVE = 61456; if ((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE)) { return; } if ((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION)) { return; } base.WndProc(ref m); }Mehmet FatihRank #1,474 · 1.1K reputationPosted Feb 11, 2024, 4:49 AM EST
Thanks for your intrest Sam. Yes, the child form is owned by the main form. I am using it like that
VeliGorusmeF frm = new VeliGorusmeF();
frm.ShowDialog();
I am using MDI but I am openeing the child forms over the main form not inside it.
Sam HobbsRank #107 · 20.5K reputationPosted Feb 11, 2024, 12:07 PM EST
No, the child form is not owned by the main form. You need to use
frm.ShowDialog();to get the child form owned by the main form. Or you can use the Owner property. However that is not likely to solve the problem.MDI is called Multiple-Document Interface. It is designed to support multiple documents, not multiple windows. When MDI is used for multiple general-purpose windows there are problems.
See the following article.
C# Create Child Forms | coding.vision
https://codingvision.net/c-create-child-forms
You can start with that and modify it as needed. The following shows the code from that article with some modifications. The commented out portions are suggestions, you can choose one of the values for `FormBorderStyle`. Since you are not specific with the requirements I cannot be specific with a solution.
Form2 frm = new Form2(); frm.TopLevel = false; Controls.Add(frm); //frm.FormBorderStyle = FormBorderStyle.None; //frm.FormBorderStyle = FormBorderStyle.FixedToolWindow; //frm.FormBorderStyle = FormBorderStyle.FixedSingle; frm.StartPosition = FormStartPosition.Manual; frm.Location = new Point(50, 50); frm.Show(); frm.BringToFront();Mehmet FatihRank #1,474 · 1.1K reputationPosted Feb 12, 2024, 1:34 AM EST
Thanks Amit. How can we add reference to it?
Mehmet FatihRank #1,474 · 1.1K reputationPosted Feb 12, 2024, 2:26 AM EST
Thanks Amit.
Mehmet FatihRank #1,474 · 1.1K reputationPosted Feb 11, 2024, 2:32 PM EST
Thank you Sam. I had tried it before but it didn't solve the problem. The child form is still draggable.
Sam HobbsRank #107 · 20.5K reputationPosted Feb 11, 2024, 9:35 PM EST
Read the cocumentation. There are values you can use for
FormBorderStyle. I was not sure what you meant by not draggable. I could not be sure that you meant that you wanted the child form to not be draggable outside the main form.