forms, classes and frustration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Treefrog

    #1

    forms, classes and frustration

    Hi,

    I'm developing a serious headache trying to get this to work. I want to
    write to a label on frmMain from another class but I can't for the life of
    me figure out how to do it. I'm pretty new to c# and the only other language
    I've used properly is PHP so a lot of the concepts are difficult for me to
    grasp. Why it's so difficult to do what I believed should be simple is
    beyond me but as I said, some concepts are difficult to understand. Any help
    would be very much appreciated.

    This code is currently on the form:
    ------------------------------------------------------------------
    public void updateStatus(st ring newText)
    {
    UpdateUIDelegat e uid = new UpdateUIDelegat e(UpdateUI);
    this.Invoke(uid , new object[] { newText });
    } // end updateStatus


    delegate void UpdateUIDelegat e(string str);
    private void UpdateUI(string uiInfo)
    {
    if (StatusLineCoun t >= StatusLineLimit )
    {
    this.lblStatus. Text = "";
    StatusLineCount = 0;
    }
    this.lblStatus. Text += uiInfo;
    this.lblStatus. Refresh();
    StatusLineCount ++;
    } // end UpdateUI
    -------------------------------------------------------------------

    When a button is pressed on the form I run

    testClass hello = new testClass("exam ple string");

    and then in there I try

    updateStatus("e xample string");

    doesn't work so I tried frmMain. but it's doesn't display updateStatus in
    the autofill.
    I tried making updateStatus static but that didn;t work because it had no
    reference to the instance of frmMain so couldn't use the label.

    Can anybody tell me how to do this? I'm sure it must be easy but I can't
    figure it out....

    thanks in advance,


    Treefrog



  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: forms, classes and frustration

    Hi,

    The testClass should be given a reference to the main form upon
    construction, like this:

    class testClass
    {
    // Some private members declared...

    frmMain _parentForm;
    string _statusText;

    public testClass(frmMa in form, string text)
    {
    _parentForm = form;
    _statusText = text;

    // Do the rest of initialization.
    }

    public void WorkerMethod()
    {
    // Do some useful stuff...
    // and now...
    parentForm.upda teStatus(_statu sText);
    }
    }

    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "Treefrog" <treefrog@nospa mspamnohatespam .mountainbikes. co.uk> wrote in
    message news:3fdf19c4$1 @news.1anetwork s.net...[color=blue]
    > Hi,
    >
    > I'm developing a serious headache trying to get this to work. I want to
    > write to a label on frmMain from another class but I can't for the life of
    > me figure out how to do it. I'm pretty new to c# and the only other[/color]
    language[color=blue]
    > I've used properly is PHP so a lot of the concepts are difficult for me to
    > grasp. Why it's so difficult to do what I believed should be simple is
    > beyond me but as I said, some concepts are difficult to understand. Any[/color]
    help[color=blue]
    > would be very much appreciated.
    >
    > This code is currently on the form:
    > ------------------------------------------------------------------
    > public void updateStatus(st ring newText)
    > {
    > UpdateUIDelegat e uid = new UpdateUIDelegat e(UpdateUI);
    > this.Invoke(uid , new object[] { newText });
    > } // end updateStatus
    >
    >
    > delegate void UpdateUIDelegat e(string str);
    > private void UpdateUI(string uiInfo)
    > {
    > if (StatusLineCoun t >= StatusLineLimit )
    > {
    > this.lblStatus. Text = "";
    > StatusLineCount = 0;
    > }
    > this.lblStatus. Text += uiInfo;
    > this.lblStatus. Refresh();
    > StatusLineCount ++;
    > } // end UpdateUI
    > -------------------------------------------------------------------
    >
    > When a button is pressed on the form I run
    >
    > testClass hello = new testClass("exam ple string");
    >
    > and then in there I try
    >
    > updateStatus("e xample string");
    >
    > doesn't work so I tried frmMain. but it's doesn't display updateStatus in
    > the autofill.
    > I tried making updateStatus static but that didn;t work because it had no
    > reference to the instance of frmMain so couldn't use the label.
    >
    > Can anybody tell me how to do this? I'm sure it must be easy but I can't
    > figure it out....
    >
    > thanks in advance,
    >
    >
    > Treefrog
    >
    >
    >[/color]

    Comment

    Working...