Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically Created User Controls In C#
    primarykey
    data
    text
    <p>I am working in a C# winforms project and I have a user control which gets loaded upon its selection from a tool-strip menu. I have a dictionary lookup set to occur upon form load of the user control for other functionality. Also, when I close the user control I am just using the ".Hide();" method. I noticed that when I load the user control the first time everything is fine, but when I close it and choose to open it again the 2nd time it creates a new instance of the object thus throwing off my dictionary lookup. Therefore, I wrote some code in an attempt to fix the issue. </p> <p>What I need to do is to somehow say that if an instance of the user control already exists, do not create a new instance of that object. Instead, just make the user control visible again. Therefore I have written code in an attempt to accomplish this purpose. When I select the item the first time, everything is fine. When I hide the user control and try to re-open it again nothing happens.</p> <p>The following is the code I have written for this purpose which occurs upon the selection of the item from the tool-strip menu:</p> <pre><code> if (Controls.ContainsKey("CheckAvailUserControl")) { Controls["CheckAvailUserControl"].Dock = DockStyle.Fill; Controls["CheckAvailUserControl"].Visible = true; Controls["CheckAvailUserControl"].Show(); Controls["CheckAvailUserControl"].Refresh(); } else { UserControl checkAvailUserControlLoad = new CheckAvailUserControl(); Controls.Add(checkAvailUserControlLoad); checkAvailUserControlLoad.Dock = DockStyle.Fill; checkAvailUserControlLoad.Visible = true; checkAvailUserControlLoad.Show(); } </code></pre> <p>When I trace through my code in the debugger it is in fact hitting the right parts of the above if/else statement. Its just not displaying the user control on the screen the 2nd time I attempt to load it.</p> <p>The question is: How do I get the user control to load correctly after I close it, then select it from the tool-strip menu again?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload