lagwagon23's picture

Winform, Accessing new Form causes lockup

I have a popup child form that the user can input the size of the grid they want and enable LOD. they can change these values:

public struct NewValues
        {
            public int intMapsize;
            public int intStep;
            public int intY;
            public bool boolLOD;
        }

When I call a new window the form locks up. I figure this has to do with me passing an instance of the Parent form to the new form while the Paint event is running like so:

 
// Parent Form menu click
private void newProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {         
           SubForms.frmNew frm = new SubForms.frmNew(this);
           frm.StartPosition = FormStartPosition.CenterParent;
           frm.ShowDialog();
        }
 
//this is how I initialize the child form
    public partial class frmNew : Form
    {
        Form1 frm = new Form1();
 
        public frmNew(Form1 frm1)
        {
            frm = frm1;
            InitializeComponent();
        }

I was able to stop the app from locking up by threading the function that calls the new app. Would you recommend doing it like that? Can I pause the paint event so I don't have to thread for performance?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
DonPElbers's picture

lol, tough crowd. did you not read a book about this in college? I won't share my solution with you, b/c I'm a made man. I never got any help in college with n00b questions like this. kill yourself.

the Fiddler's picture

This was uncalled for, DonPElbers.

public partial class frmNew : Form
    {
        Form1 frm = new Form1();
 
        public frmNew(Form1 frm1)
        {
            frm = frm1;
            InitializeComponent();
        }

This doesn't look right, you are creating two new forms here (one frmNew and one frm) then replace frm with frm1 (which means frm is leaked). I'm not sure what you are trying to do, but this should work:

private void newProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {         
           SubForms.frmNew frm = new SubForms.frmNew(); // remove the Form1 parameter from the constructor.
           frm.StartPosition = FormStartPosition.CenterParent;
           frm.Show(this); // make frm a child to this form.
        }
lagwagon23's picture

Hi Fiddler,
I am opening a child form on my map editor that I can change the size of the terrain with. I need to alter the Parent forms X and Z coordinate values through the child form. I had passed "this" so I could access the parent form variables on the child form and alter them at run time, but it caused freeze up. Your way doesn;'t cause freeze up anymore, but I can access the parent form variables can I? Thanks for your time :P

the Fiddler's picture

There are many ways to solve this issue. For example:

Form1 parent = (Form1)frm.Parent;

Of course, this will only work if the parent actually is of type Form1 (otherwise you'll get a runtime exception.)

Your original approach would also work, as long as you call Show(this) instead of ShowDialog() (the latter creates a modal message loop that temporarily stops the parent from receiving messages.)

lagwagon23's picture

Thanks that fixed it.