0

So, I'm adding Panels by code to my flowLayoutPanel:

Panel pnl = new Panel {Name = panelname}; //panelname is a name combined with a continous number
CopyControl(panel1,pnl, number);
pnl.Visible = true;
flowLayoutPanel1.Controls.Add(pnl);

(I already have a Panel in that flowLayoutPanel and I just copy its property's) CopyControl:

private void CopyControl(Control sourceControl, Control targetControl, int number)
{
    // make sure these are the same
    if (sourceControl.GetType() != targetControl.GetType())
    {
        throw new Exception("Incorrect control types");
    }
    foreach (PropertyInfo sourceProperty in sourceControl.GetType().GetProperties())
    {
        object newValue = sourceProperty.GetValue(sourceControl, null);
        MethodInfo mi = sourceProperty.GetSetMethod(true);
        if (mi != null)
        {
            sourceProperty.SetValue(targetControl, newValue, null);
        }
    }
    try
    {
        foreach (Control c in sourceControl.Controls)
        {
            if (c.GetType() == typeof(Button))
            {
                Button btn = new Button();
                btn.Name = "button" + number;
                btn.Click += CopyTextToClipboard;
                //get same location
                Button btnold = this.Controls.Find("button1", true).FirstOrDefault() as Button;
                btn.Text = btnold.Text;
                btn.Location = btnold.Location;
                btn.Size = btnold.Size;
                targetControl.Controls.Add(btn);
            }
            else if (c.GetType() == typeof(RichTextBox))
            {
                RichTextBox rtb = new RichTextBox();
                rtb.Name = "richTextBox" + number;
                RichTextBox rtbold = this.Controls.Find("richTextBox1", true).FirstOrDefault() as RichTextBox;
                rtb.Location = rtbold.Location;
                rtb.Size = rtbold.Size;
                targetControl.Controls.Add(rtb);
            }
            else if (c.GetType() == typeof(Label))
            {
                Label lbl = new Label();
                lbl.Name = "label" + number;
                Label lblold = this.Controls.Find("label1", true).FirstOrDefault() as Label;
                lbl.Text = lblold.Text;
                lbl.Location = lblold.Location;
                lbl.Size = lblold.Size;
                targetControl.Controls.Add(lbl);
            }
            else if (c.GetType() == typeof(TextBox))
            {
                TextBox tbox = new TextBox();
                tbox.Name = "textBox" + number;
                TextBox tboxold = this.Controls.Find("textBox1", true, FirstOrDefault() as TextBox;
                tbox.Location = tboxold.Location;
                tbox.Size = tboxold.Size;
                targetControl.Controls.Add(tbox);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show((ex.Message));
    } 
}

Also worth to add: On the pictures below, the first picture is on tabPage1 of a TabControl, and second picture is on tabPage2 of the TabControl where also the flowLayoutPanel is inside.

Everything works fine, except that it looks really ugly:

Program at the start

Program after all the code was done

If you click into the TextBox for example, you will see that the text is correct:

enter image description here

The question: How can I fix this ? That everything gets displayed as it should?

DatRid
  • 1,169
  • 2
  • 21
  • 46
  • Where is your question? – jbutler483 Sep 08 '14 at 16:05
  • I thought that is obvious sorry. It looks ugly and I would like to fix it. – DatRid Sep 08 '14 at 16:06
  • Fix its ugliness? In what way? adding formatting? changing background colour? changing font? removing/editing border? changing border icon? – jbutler483 Sep 08 '14 at 16:08
  • Yeah well, how should this help ? As far as I see it I am maybe missing something while copying the Panels e.g., but I don't find the error. Or do you want to tell me that this happens every time you add panels to a flowlayoutpanel that grey boxes show up over the textboxes & labels and it is just a formatting thing? – DatRid Sep 08 '14 at 16:10
  • 1
    Have you tried removing the Get/Set property loop in the CopyControl loop? There are a lot of properties on panel you don't need to and probably shouldn't copy (Parent, Name, ...) Manually setup the docking/size/color. – dodald Sep 08 '14 at 16:11
  • You're adding controls on top of each other in your form. I'm not sure what you're trying to accomplish with this, but try setting any panel you don't want seen to `.Visible = false` – JWiley Sep 08 '14 at 16:12
  • @JWiley Thanks, but I want all labels to be seen :) – DatRid Sep 08 '14 at 16:14
  • 1
    @DatRid, i think JWiley was talking about the DataGridView - once you've 'finished' with it, set its visible property to false. – jbutler483 Sep 08 '14 at 16:16
  • @dodald +1, that worked! Except that the button doesn't show up anymore, lets see if I need to add some special value at the button. – DatRid Sep 08 '14 at 16:16
  • I don't think you understand. Those "grey boxes" behind your list control is your datagridview control, you need to hide it. – JWiley Sep 08 '14 at 16:16
  • @JWiley controls behind other controls are not normally visible, this looks like a corruption of some sort. – dodald Sep 08 '14 at 16:17
  • @JWiley but the DataGridView is in a complete other tabPage of my TabControl. – DatRid Sep 08 '14 at 16:17
  • Ah, right. Have you tried using `Refresh` then? – JWiley Sep 08 '14 at 16:19
  • @JWiley just added it, doesn't have any effect. But as said, with removing the getting/setting of the property's, the grey boxes disappeared. Well and the "Copy to Clipboard" TextBoxes of the panels also ... – DatRid Sep 08 '14 at 16:21
  • @DatRid I can't see a TabControl? have you just enlarged it beyond the size of the form? – jbutler483 Sep 08 '14 at 16:21
  • @jbutler483 yes. Maybe on the top left you can see a little minimum of the TabControl. Its hidden, and the header minimized. – DatRid Sep 08 '14 at 16:22
  • Why don't you create a different form instead? that way, you wuoldn't have to worry about showing/hiding different elements (makes for easier editing/updating too!) – jbutler483 Sep 08 '14 at 16:23
  • @DatRid when you removed the loop, did you add something in it's place? Depending on how the FlowLayoutPanel is configured, you may have to set the Size, Dock, or Anchor properties. You may also need to copy the Anchor settings for the other controls. – dodald Sep 08 '14 at 16:25
  • Well sure it would be possible and I guess it would make more sense, but I don't think it has anything todo with the problem. Anyway I will think about it! – DatRid Sep 08 '14 at 16:25
  • @dodald not yet, but I will try to add all the specific properties I need tomorrow. Need to go now, sorry. More information if it worked tomorrow! And then you should post it as answer :) – DatRid Sep 08 '14 at 16:26
  • 1
    Get rid of the CopyControl routine. You need to replace it with a UserControl that has a Label, RichTextBox, Button and a TextBox: the controls you are trying to copy. – LarsTech Sep 08 '14 at 16:42
  • @LarsTech that is the best suggestion here. – dodald Sep 08 '14 at 18:57

1 Answers1

1

There is an obvious corruption going on so you'll have to debug the problem a little first.

You are copying all properties of the panel (the Get/Set loop in CopyControl), this may result some strange issues. Also, the loop calls the get_ on all fields (even the read-only ones), which may cause strange stuff on it's own. (That loop would be the first thing I'd change if I was debugging this problem.)

When you get the 'corruption' resolved, you may want to revisit the code where you copy the Buttons and other controls, you should probably assign the Dock and Anchor properties. without them the controls may 'float' off the control when the first layout occurs.

dodald
  • 603
  • 3
  • 8