CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2003
    Posts
    402

    Passing Data between Controls

    I have a question. I created 2 custom controls which my application uses. For simplicity lets call them Ctrl1 and Ctrl2.

    How do I pass data from Ctrl1 to Ctrl2.
    For instance lets say Ctrl1 changes a color. That same color must be passed to Ctrl2.


    OR:
    Is it possible to raise an event in Ctrl1 (OnColorChange...for example). This event gets called in the main application. From the main application the color can be passed to Ctrl2.

    I believe the Event is the way to do it. Any help would be appreciated


    Thanks

  2. #2
    Join Date
    Mar 2004
    Location
    Ukraine
    Posts
    170

    Re: Passing Data between Controls

    Well, event is correct idea, for example you created event on ColorChange, so to exchange data you need to create properties like
    Code:
     public Color MyColor
    {
       get{return this.color;}
       set{this.color=value;}
    }
    // and event ofcourse
    void OnColorChange(object Sender)
    {
       Ctrl1.MyColor=Ctrl2.SelectedColor;
    }
    Ofcourse you need to create properties and data fields to both controls
    God could improve essentially a
    human nature, but he
    was too anxious with compatibility
    with the monkey.
    (Eugeny Goldberg)

  3. #3
    Join Date
    Mar 2003
    Posts
    402

    Re: Passing Data between Controls

    Thanks, Actually I thought it was more compilcated. I guess I did not explain myself well. But if you goto the help and type Raising Events, there is an example that deals w/ an alarm clock.

    It does exactly what I wanted. raising an event that I created in one class and another class consumes the event!

    Either way...Thanks for all the help

  4. #4
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Passing Data between Controls

    There is a more simple way.
    In the form Load event you just add one line of code:
    Code:
    private void Form1_Load(object sender, System.EventArgs e)
    {
           label2.DataBindings.Add("ForeColor",label1,"ForeColor");
    }
    This way whenever the ForeColor of label1 changes, the ForeColor of label2 changes to the same color. No need for events.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured