-1

I am trying to make some code more concise and am throwing an exception when I try to, I suppose, dynamically define a control. Whichever way I try, a and b always return as disposed. I could separate the cases out, but that would make the code twice as long. Any suggestion/comments would be much appreciated.

foreach (string[] str in items)
            {
                Control box = new Control();
                CustomControlTypeA a = CustomControlTypeA();
                CustomControlTypeB b = new CustomControlTypeB();
                switch (str[4])
                {
                    case "0":
                        a.richTextBox1.Rtf = str[5];
                        box = a;
                        break;

                    case "1":
                        b.panel1.BackgroundImage = Image.FromFile(str[5]);
                        box = b;
                        break;
                }
                a.Dispose();
                b.Dispose();
                box.Location = new Point(x,y);
                box.Width = w;
                box.Height = h;
                panelBody.Controls.Add(box);
                box.BringToFront();
            } 

I also tried defining CustomControlTypeA inside the case statement, redefining box as CustomControlTypeA, and even tried casting like so:

case "0":
    (CustomControlTypeA)box.richTextBox1.Rtf = str[5];
    break;
  • 1
    It's because you have `a.Dispose();` & `b.Dispose();`. You're assinging one of them to `box` so it is also disposed. – Enigmativity Jan 31 '16 at 13:14

2 Answers2

0

It's because you have a.Dispose(); & b.Dispose();. You're assinging one of them to box so it is also disposed.

Oddly though, you're creating box as a new Control (which you don't need to do), but not disposing that.

If you're adding a control to the .Controls collection you shouldn't be disposing it at all.

This code should work though:

foreach (string[] str in items)
{
    Control box = null;
    switch (str[4])
    {
        case "0":
            CustomControlTypeA a = CustomControlTypeA();
            a.richTextBox1.Rtf = str[5];
            box = a;
            break;

        case "1":
            CustomControlTypeB b = new CustomControlTypeB();
            b.panel1.BackgroundImage = Image.FromFile(str[5]);
            box = b;
            break;
    }
    box.Location = new Point(x, y);
    box.Width = w;
    box.Height = h;
    panelBody.Controls.Add(box);
    box.BringToFront();
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Thanks for the response, but this still throws the same error. On debug, a.AccessibilityObject and a.Handle throw ObjectDisposedException. – Ophaedean Rhythm Jan 31 '16 at 13:26
  • @OphaedeanRhythm - How? I removed the `.Dispose()` calls? – Enigmativity Jan 31 '16 at 13:29
  • @OphaedeanRhythm - Did you get a chance to look again? – Enigmativity Feb 01 '16 at 01:22
  • Yes, I did. The .Dispose() does not appear to be the problem. I haven't figured out why, but the issue seems to be with accessing .Rtf - accessing .Text then calling a custom function that effectively states Rtf=Text is the work-around I'm using. Since Dispose is called after box=a, the disposal is not a problem. – Ophaedean Rhythm Feb 02 '16 at 17:24
  • @OphaedeanRhythm - If you call `a.Dispose()` after doing the `box = a` assignment then you are effectively also calling `box.Dispose()` as they are the same object. Controls are assigned by reference and not value. So it is a problem. – Enigmativity Feb 02 '16 at 23:11
-1

After lots of trial and error, I've now discovered that an aspect problem probably lies in the Rtf in my custom control, though who knows why that is. Enigmativity's method works if richtextbox.Text is used instead of richtextbox.Rtf.

EDIT: amended text to credit Enigmativity with solution. Either a typo elswhere in my code (setting str[5] to empty) or some other random code I changed in the meantime was exacerbating the problem.