blah blah blah is here! blah blah » Close

up0down
link

I'm trying to add a new picturebox and label to my form form each match of a regex. Right now it seems to only add one label and one picturebox no matter how many matches there are. What am I doing wrong?
My code:
private void Form_Load(object sender, EventArgs e)
{
int matchcount = 1;
Form1 f1 = (Form1)Application.OpenForms["Form1"];
RichTextBox rtb = (RichTextBox)f1.Controls["richTextBox1"];
Regex regex = new Regex(@"\w+");
MatchCollection matches = regex.Matches(rtb.Text);
foreach (Match match in matches)
{
PictureBox newPictureBox = new PictureBox();
newPictureBox.BackColor = settingsClass.paperColor;
newPictureBox.Location = new System.Drawing.Point(12, ((27*matchcount) + 25));
newPictureBox.Size = new System.Drawing.Size(227, 89);
Label newLabel = new Label();
newLabel.Text = "" + matchcount;
newLabel.BackColor = settingsClass.paperColor;
newLabel.ForeColor = settingsClass.textColor;
newLabel.Location = new System.Drawing.Point(204, ((27 * matchcount) + 25));
newLabel.Size = new System.Drawing.Size(35, 13);
this.Controls.Add(newPictureBox);
this.Controls.Add(newLabel);
matchcount++;
}

last answered 2 years ago

1 answers

up0down
link

The code is in fact generating multiple controls but the picturebox is so big that it's overlapping the previous one and making it look as though there's just one big picturebox.
If you temporarily make the pictureboxes the same size as the labels, then you'll (hopefully) see what I mean:
newPictureBox.Size = new System.Drawing.Size(35, 13);

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback