views:

424

answers:

3

hello all,

i have a text file that contains font information like this:

Arial, 12.5

...and I need to read that info into label.Font like this:

label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));

... but I always get the following error:

Index was outside the bounds of the Array.

Can somebody please help me with this?

Thanks jason.

Code I have:

    MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\r\n""([^""]+)""\r\n(\d+), (\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 cfont = match.Groups[5].Value;

            String fontName = cfont;
            String[] fontNameFields = fontName.Split(',');


            label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));
}

Stacktrace


   at Tabbed.Form1.FillCanvas() in C:\Documents and Settings\jay\My Documents\Visual Studio 2008\Projects\MYPROGGY\ MYPROGGY\Form1.cs:line 574
   at Tabbed.Form1.openbtn_Click(Object sender, EventArgs e) in C:\Documents and Settings\jay\My Documents\Visual Studio 2008\Projects\ MYPROGGY\ MYPROGGY\Form1.cs:line 802
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Tabbed.Program.Main(String[] args) in C:\Documents and Settings\jay\My Documents\Visual Studio 2008\Projects\ MYPROGGY\ MYPROGGY\Program.cs:line 27
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
+1  A: 

At first glance the code looks OK.

Have you checked that fontName is what you expect it to be (i.e. "Arial, 12.5") and what fontNameFields contains after the call to fontName.Split?

fontNameFields[0] -> "Arial"
fontNameFields[1] -> "12.5"

Without seeing the actual values I wouldn't like to suggest anything else.

EDIT

From your updated code I'd suggest that it's your regex that's failing. Try printing out the contents of the match.Groups array to check that you're parsing the input correctly.

ChrisF
thank you ChrisF. The actual values that I am trying to convert? they are in the question up the top, but ill write them here:Arial,12.5
baeltazor
@baeltazor - I meant have you double checked that `fontName` is actually the value you expect it to be. If it is then check that `fontNameFields` is what you expect it to be.
ChrisF
I added this line of code to see what was goingn on:MessageBox.Show(fontName); ...and it doesn't display anything in the messagebox. it just pops up an empty messagebox
baeltazor
@baeltazor - that's your problem then. The split will then fail and return an empty array which is the exception being raised. Can you post the code that defines and sets `cfont`
ChrisF
@chrisf - i have just updated my question with the requested info
baeltazor
thank you very much chrisf - i just did that and when it prints out the cfont line of the regex (the Groups[5] one) it's plain empty. so i can't see anything. i'm not very good with regex, so i am not too sure where the mistake is
baeltazor
@baeltazor - it might be best to ask a new question with the string you want to parse and the regex you have and ask why it's not returning anything. Refer to this question for the history.
ChrisF
i was just thinking that. i think i may have unintentionally caused alot of confusion for everybody here
baeltazor
+2  A: 

I suspect you're running into a line which doesn't contain a comma. Your sample data work okay:

using System;

class Test
{
    static void Main(string[] args)
    {
        String fontName = "Arial, 12.5";
        String[] fontNameFields = fontName.Split(',');
        String name = fontNameFields[0];
        float size = float.Parse(fontNameFields[1]);
        Console.WriteLine("{0}: {1}", name, size);
    }
}

Log (or pop up in a dialog box) fontName just before you try to create the font, and I'm sure that'll show you what's going on.

Jon Skeet
thank you mr. skeet, i showed a messagebox with the code you provided, but before i even get to display it the messagebox, and returns an error "Index wasoutside the bounds of the array" on line4 of that code.
baeltazor
"That code"? Which code? My code won't throw an error, and you've now edited your code.
Jon Skeet
oh no, i didnt mean that your code was causing the error, all i meant was that i still get the error, even after i tried your code (suggesting that the problem may be somewhere else?)
baeltazor
+1  A: 

Could it be your format contains a strange comma, or none at all?

Also could you provide code on the label as if it were a System.Web.UI.WebControls then Font has not got a setter.

Skeet's code above works fine for the given input. You're problem lies elsewhere.

dove
thanks for the answer, but no i've been checking over and over again and the font info in the file is:Arial,12.5
baeltazor
the code for the label is: label.Font = new Font(name, size);
baeltazor
@baeltazor that is still setting. what type of label is it? also i'd merely suggest you break up the code and maybe write some tests for the separate tasks you are trying to accomplish, i'd always hone in on checking the regex first.
dove