Resolved Difference when opening form in panel

Joined
Aug 2, 2020
Messages
5
Programming Experience
3-5
I noticed the following behavior in a form

When I have a textbox in a common form and it allows me to position the cursor anywhere in the text for editing, when I open the same form inside a panel it does not allow the same thing to be done, its behavior is changed, someone has already gone through that ? if so how can i do so that it has the same behavior
 
My question would be, why are you opening a form in a Panel in the first place? It can be done but there's rarely a good reason to. Windows Forms has MDI functionality built in if you want to do that properly and you can design a user control to use groups of child controls as a group inside another container. I don't think that I've ever encountered a situation where one of those would not be a better option than what you're doing.
 
My question would be, why are you opening a form in a Panel in the first place? It can be done but there's rarely a good reason to. Windows Forms has MDI functionality built in if you want to do that properly and you can design a user control to use groups of child controls as a group inside another container. I don't think that I've ever encountered a situation where one of those would not be a better option than what you're doing.
Capturar.PNG





is an application that I created and has a panel where I open the forms and now I found this problem
 
That doesn't answer my question. I'm not seeing anything there that suggests that you should not be using MDI or a user control.
I did not understand what you meant I have an MDi form and I have a tablelayoutpanel where I add the open forms in his controls, are you saying it would not be the best way to do this?
 
If you had an MDI form then you wouldn't be opening a form in a Panel. MDI functionality is built into Windows Forms. You set the IsMdiContainer property of the parent form to true and then you set the MdiParent property of the child form(s). If you're not doing that then you're not really using MDI.
 
If you had an MDI form then you wouldn't be opening a form in a Panel. MDI functionality is built into Windows Forms. You set the IsMdiContainer property of the parent form to true and then you set the MdiParent property of the child form(s). If you're not doing that then you're not really using MDI.




I'm doing it this way


Capturar.PNG
 
In the future, post code in code tags, not screenshots.

You were on the right path of using MDI, until you added the form to the Controls collection. For MDI, you don't need to do that.
 
First things first, do not post pictures of code. Code is text so you should post it as text, formatted as code. I want to copy and paste some that code but now I can't because it's a picture.

Anyway, the problem is that you broke it yourself. Like I said, to use MDI in Windows Forms you simply set the IsMdiContainer property of the parent form to true and then you assign the parent form to the MdiParent property of the child forms. That's it, that's all. Get rid of your Panel, do NOT set the TopLevel to false and do NOT call Controls.Add on anything. You simply create the child form, set the MdiParent to this and call Show.
C#:
var formulario = this.MdiChildren.OfType<Forms>().FirstOrDefault();

if (formulario == null)
{
    formulario = new Forms {MdiParent = this};
    formulario.Show();
}
else
{
    formulario.WindowState = FormWindowState.Normal;
    ActiveMdiChild = formulario;
}
That code is untested but I think that it should do the job. Of course, that assumes that you have configured the parent form correctly.
 
Last edited:
Back
Top Bottom