tags:

views:

44

answers:

2

well, the title pretty much says it all.

here's a little info: i have a text file, and inside that textfile contains:

Label
"this text will be associated with the Text property of the label once the label has been added to the Panel Control at runtime."
20, 45
Trebuchet MS
14.0

explanation of the above the above file may have only one label-info, or it may have a thousand of them. here's how it works:

Control
Text Property
Locatoin
Font Name
Font Size

...Now, all is well if i leave out the font information from the file. but as soon as i modified my regex to suit for the font name and font size, my program no longer adds the control to the Panel at runtime. there are no debugging errors or ugly squigly lines either. It just doesn't show anything.

So, my guess is the last part of the regex that tries to match font information/ can somebody please help me understand/solve this problem?

the code i have is:

public partial class Form1 : Form
{
    private void FillCanvas()
    {
        canvas.Controls.Clear();
        canvas.Visible = true;
        canvas.BringToFront();
        txt.Visible = false;

        MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\r\n""([^""]+)""\r\n(\d+), (\d+)\r\n""(\w*)\r\n" +
            @"(\d+)");
        foreach (Match match in lines)
        {
            string control = match.Groups[1].Value;
            string text = match.Groups[2].Value;
            int x = Int32.Parse(match.Groups[3].Value);
            int y = Int32.Parse(match.Groups[4].Value);

            string fname = match.Groups[6].Value;
            float fsize = float.Parse(match.Groups[7].Value);

            switch (control)
            {
                case "Label":
                    Label label = new Label();
                    label.Text = text;

                    label.AutoSize = true;
                    label.IsAccessible = true;

                    label.Font = new Font(fname, fsize);
                    label.MouseMove += new MouseEventHandler(label_MouseMove);
                    label.MouseDown += new MouseEventHandler(label_MouseDown);
                    label.MouseUp += new MouseEventHandler(label_MouseUp);
                    label.Click += new EventHandler(label_Click);
                    label.DoubleClick += new EventHandler(label_DoubleClick);

                    canvas.Controls.Add(label);
                    ControlCount++;
                    label.Name = ControlCount.ToString();
                    label.Location = new Point(x, y);
                    SelectedControl = label.Name;
                    break;
+2  A: 

Have you set a breakpoint inside the foreach and debugged? Since you are iterating over the results of a MatchCollection, the first place I'd check is to see if you have zero matches.

Rex M
yeah i have. still nothing. nothing at all.
baeltazor
thanks for your help Rex M. Much appreciated . :)
baeltazor
+2  A: 

Your regex pattern is incorrect. Change it to this:

@"(.+?)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\r\n(\d+)"

Your original pattern has 2 problems when it attempts to match the font name. First, it has unnecessary quotation marks, but there are none for the font name section. Second, the font name can have a space in it, and your pattern didn't account for it.

Your pattern:

@"(.+?)\r\n""([^""]+)""\r\n(\d+), (\d+)\r\n""(\w*)\r\n(\d+)"
                                            ^   ^
                                            |   |
                           1st problem   ___|   |
                               2nd problem   ___|

BTW, the current pattern does not correctly capture the "14.0" font size. It will capture "14" only. If you need to capture the rest of it, consider using (\d+(?:\.\d+)?) instead if it's an optional format. If you always expect it to be in that format then use (\d+\.\d+).

EDIT: if it's within your control I suggest switching the text file / regex approach to an XML format instead. Some nice LINQ to XML will make this simpler to maintain.

Ahmad Mageed
OH THANK YOU SO MUCH Ahmad Mageed, I'm so happy right now I think i'm gonna cry lol.
baeltazor