Passing data between class and form in C#

hi..
i have to pass some data from a class file to form which is having richTextBox .. any sort of help is appreciated.
thanks


Answer this question

Passing data between class and form in C#

  • ruju

    Does PJ van de Sande's solution work Isn't Message still null
  • none0

    Hi PJ, i try your solution but i want to access function between form instead class and form, everything compile correct, but my event is not fired, can you help me

    i use:

    private form2 _myClass;

    instead of:

    private MyClass _myClass;

    i dont really need to pass event to a control, i just need to execute function in my form1


  • vcboy

    i did that .. the data is passing from myClass to append_rtbox ..but its not appending it to richtextbox.. through debugging i can see that data is passed to that function.

  • ricoxxx

    thanx
    But i want to send data from a class to form which is having richtextbox.

    class myClass
    {
    public myClass()
    {
    }
    public void myFunc( )
    {
    Form1 obj=new Form1();
    obj.rtbox.AppendText("MY DATA");
    -----
    }
    }

    Something like this ... is this possible.

  • BMF

    Hello,

    I have some difficulties to use your code because some instructions aren't complete :

    _myClass.Message += new MyDelegate(

    and
    target.Invoike( Message,

    I have rewritten the last one to
    "target.Invoke( Message);"

    Can you help me in order to use your solution.

    Thanks
    Daniel


  • Alan M. Carroll

    jmccall3 wrote:
    class myClass
    {
    public myClass()
    {
    }

    public void myFunc( )
    {
    Form1 obj=new Form1();
    obj.append_rtbox("MY DATA");
    }
    }


    This doesn't work because you initialize a new instance for Form1. You don't use the visable instance.

    Here is the modified code from my last post, that should work.


    public delegate void MyDelegate( object sender, string message );

    public class MyClass
    {
    public event MyDelegate Message;

    public void MyFunction()
    {
    // TODO: Do something.
    OnMessage( "MY DATA" );
    }

    protected void OnMessage( string message )
    {
    // If event is wired, fire it!
    if( Message != null )
    {
    // Get the target and safe cast it.
    Control target = Message.Target as Control;

    // If the target is an control and invoke is required,
    // invoke it; otherwise just fire it normally.
    if( target != null && target.InvokeRequired )
    {
    object[] args = new object[] { this, message };
    target.Invoike( Message,
    }
    else
    {
    Message( this, message );
    }
    }
    }
    }





    In your Form you can wire the event and when you recieve the event you append the message:




    private MyClass _myClass;

    private void Form_Load( object sender, EventArgs e )
    {
    // Initialize member and wire event.
    _myClass = new MyClass();
    _myClass.Message += new MyDelegate( _myClass_Message );
    }

    private void _myClass_Message( object sender, string message )
    {
    // Append the message.
    rtbox.AddendText( message );
    }



  • Fizgig

    You refactor the class to it has properties:


    public class Customer
    {
    private string _name;

    public string Name
    {
    get
    {
    return _name;
    }
    set
    {
    _name = value;
    }
    }
    }



    Now in the Click event of a button or TextChanged event of the RithcTextBox you can set this property with the value you want:


    private Customer _customer = new Customer();

    private void button_Click( object sender, EventArgs e )
    {
    _customer.Name = richtTextBox.Text;
    }



  • Saleem Yusuf

    Charlemagne wrote:
    Does PJ van de Sande's solution work Isn't Message still null


    Only when the event isn't wired Message is null, but in the example post i have post a usage example where the event gets wired.


  • er1067

    If the class has a Get Property it can be done easelly, but you should not have UI logic in your classes.

    You should use events for that.


    public delegate void MyDelegate( object sender, string message );

    public class MyClass
    {
    public event MyDelegate Message;

    public void MyFunction()
    {
    // TODO: Do something.
    OnMessage( "MY DATA" );
    }

    protected void OnMessage( string message )
    {
    // If event is wired, fire it!
    if( Message != null )
    {
    // Get the target and safe cast it.
    Control target = Message.Target as Control;

    // If the target is an control and invoke is required,
    // invoke it; otherwise just fire it normally.
    if( target != null && target.InvokeRequired )
    {
    object[] args = new object[] { this, message };
    target.Invoike( Message,
    }
    else
    {
    Message( this, message );
    }
    }
    }
    }



    In your Form you can wire the event and when you recieve the event you append the message:



    private MyClass _myClass;

    private void Form_Load( object sender, EventArgs e )
    {
    // Initialize member and wire event.
    _myClass = new MyClass();
    _myClass.Message += new MyDelegate(
    }

    private void _myClass_Message( object sender, string message )
    {
    // Append the message.
    rtbox.AddendText( message );
    }





  • Yaron Lavi

    in your form1 put

    public void append_rtbox (string s)
    {
    rtbox.AppentText(s);
    }
    then try

    class myClass
    {
    public myClass()
    {
    }
    public void myFunc( )
    {
    Form1 obj=new Form1();
    obj.append_rtbox("MY DATA");
    -----
    }
    }


  • Maju V Poulose

    This is a bit a weird approach...
    You cann pass a form as an argument to your object:
    class MyClass
    {
    public void MyFunc( FormWithTextBoxType theForm )
    {
    theForm.AppendTextToRichTextBox ("bliep");
    }
    }

    where AppendTextToRichTextBox is a public method that is defined on your form.




  • dschon

    I applied the logic posted by PJ van de Sande above inside a seperate class that implements a FileSystemWatcher object and had it update text on a form each time it saw a new file come in and again when it completed some processing on the file. It worked perfectly. Very nice.
  • Youz

     Paul Deffinger wrote:
    I applied the logic posted by PJ van de Sande above inside a seperate class that implements a FileSystemWatcher object and had it update text on a form each time it saw a new file come in and again when it completed some processing on the file. It worked perfectly. Very nice.


    I am glad everything works perfectly! Please feel free to post whenever you have problems with implementing this!


  • Newbie2007

    Hi

    Im not sure if this is gonna help.

    Firstly I read data from a file in a specified format and pass it to a class it looks like

  • Passing data between class and form in C#