0

Basically i got a form with a panel inside of it 2 buttons one called newClient , once the user clicks on it the Click_event kicks in (check the code ) !

I have tried to dispose cntrols inside of the panel but still nothing !

        private void newClient_Click(object sender, EventArgs e)
        {
            MainPanel.Dispose();
            this.Height += 300;
            this.CenterToScreen();


            string[] txtBoxNames = new string[] { "fullName", "cinRef", "mainAddr", "phoneNbr","gender","birthDate" };

            try
            {

                for (int i = 0; i < 4; i++)
                {
                    TextBox tb = new TextBox();
                    tb.Name = txtBoxNames[i];
                    tb.Location = new Point(20, 150);
                    this.Controls.Add(tb);
                }
            }
            catch (Exception ex )
            {
                MessageBox.Show(ex.Message);
            }

        }

In brief, what i'm trying to achieve here is to add some dynamic Controls to the form !

Saad Amrani
  • 95
  • 2
  • 11
  • Why are you trying to dispose the _MainPanel_? Notice that you are disposing the container of the same control that is in the middle of the event handler. I think you are looking for troubles. Side note, all textboxes are created at the same location. You will see only the one on the top of the stack – Steve May 13 '19 at 09:21
  • Why would you dispose of anything? What happens when you remove that line? Why are all 4 textboxes in the same place? – TaW May 13 '19 at 09:21
  • Can you tell again what are you trying to do? You are creating different names for the same value in a row, which should disable the others. Your `for (int i = 0; i < 4; i++)` is also to tiny to handle all the txtBoxNames you got there in your array – A.A. May 13 '19 at 09:21
  • @Steve i know about the sideNote , thats not what concerns me ! i tried to dispose the main panel cuz i just dont need it anymore i will replace all of that with only 4 textBoxs thats all ! – Saad Amrani May 13 '19 at 09:24
  • Why not just show\hide existing controls instead of creating them dynamically? – Martin May 13 '19 at 09:24
  • _i just dont need it anymore_ as steve noted it is still active as the parent of a button whose handel is running!! Better simply hide it! – TaW May 13 '19 at 09:26
  • @AA the last 2 in my array there are for somethinf else you can ignore them ! as for the textboxs being created in one spot u can ignore that too since it should work even if theyr in same spot (at least one will be visible) m in my case i get the error : Error Creating Handle on window (exception message ) , and nothing shows up in my form ! – Saad Amrani May 13 '19 at 09:27
  • 2
    Disposing the container will dispose its content. You are destroying the button while it is still serving the event handler. – Steve May 13 '19 at 09:27
  • @Martin thats the first thing i did man , still same error ! – Saad Amrani May 13 '19 at 09:27
  • 1
    Creating your own grid-like control by dynamically creating textboxes is always a mistake. You are guaranteed to always crash your program, even though there can be very little joy in clicking that button two thousand times. Many grid controls around, DataGridView is available in the toolbox. Or use ListView with a NewClient form. If it is in fact a dispose problem, the snippet is nonsense, then use Task Manager's Processes tab. Add the USER Object column. A steadily rising number spells doom. – Hans Passant May 13 '19 at 09:30
  • @Steve i dont think you got me well , first thing i have tried was to hide the mainPanel , didnt work and then i tried to dispose the control of the button didnt work and yeah ... here check this screenshot : [Link](https://prnt.sc/nnusgc) – Saad Amrani May 13 '19 at 09:33
  • @Steve the exception message basically says : Error Creating handle on window . – Saad Amrani May 13 '19 at 09:35
  • _hide the mainPanel , didnt work_ This is a useless description. What went wrong? – TaW May 13 '19 at 09:39
  • If you have this error just hiding the MainPanel perhaps there is something broken somewhere in your application. See this question https://stackoverflow.com/questions/88904/error-creating-window-handle and this one https://stackoverflow.com/questions/222649/winforms-issue-error-creating-window-handle/ – Steve May 13 '19 at 09:40
  • @Steve indeed i saw that post thats why i tried to dispose the mainPanel and its controls (still same exception tho) ! so yeah i dunno tbh what exactly the problem is , but everything works fine when creating / adding a controll inside the panel .. – Saad Amrani May 13 '19 at 09:44
  • @TaW nothing it gets hidden but still throws the exception when it gets to the line where it adds control to the form – Saad Amrani May 13 '19 at 09:45
  • So you get that exception even when you are disposing nothing at all? – TaW May 13 '19 at 09:52
  • @TaW Thats right ! – Saad Amrani May 13 '19 at 10:01
  • 1
    The code you posted (even with the dispose) works just fine. There must be other things going on you didn't show us.. – TaW May 13 '19 at 10:10

0 Answers0