Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Communication between components

Started by MCeltic33 Aug 8, 2006 at 10:34 PM 9 replies 1.2k views
Original Post
MCeltic33
MCeltic33
I have a component with a button on it, a form with that component on it, and a textbox on that same form. I want to be able to click the button on the component, and change the text on the textbox. Any ideas?
try_catch_this
try_catch_this
1) Make an event handler that processes the button click.

To make an event handler double click the button its also available in the properties toolbar.

2) In the event handler update the text of the textbox.
MCeltic33
MCeltic33
That's rather difficult since the button is on a component, and the textbox is on the form. The form also houses the component.

_Form_________________|                     || [__-Textbox__]      ||                     || |----------------|  || |component       |  || |                |  || |    [Button]    |  || |                |  || |                |  || |----------------|  |_______________________
doctorsixstring
doctorsixstring
You would need to have give the component a delegate member that could be set when you initialize the form. In your case, the delegate would reference a "set text" method on the form. When the button is clicked, the delegate would be called and your text would be updated.

Why exactly are you designing your UI this way? Components are supposed to be self-contained and reusable, so you should be very careful when creating dependencies with external data (like your textbox).

- Mike
MCeltic33
MCeltic33
I am attempting to create an onscreen keyboard, for use in multiple forms.

If you have a better suggestion, I am open to new ideas.
Machaira
Machaira
Clicky
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development
Justaddwater
Justaddwater
So I would do it like

component has a public event KeyClicked(String key)

And each button click would go to a button click event that calls that public one

OnButtonClick()
Button x = sender as Button;
OnKeyClicked(x.text);

Then in your form you just capture it

component myComponent = new component;
myComponent.KeyClicked += new KeyClick(HandleIt);

void HandleIt(String key)
this.textbox1.text += key;
MCeltic33
MCeltic33
Quote:
Original post by Justaddwater
So I would do it like

component has a public event KeyClicked(String key)

And each button click would go to a button click event that calls that public one

OnButtonClick()
Button x = sender as Button;
OnKeyClicked(x.text);

Then in your form you just capture it

component myComponent = new component;
myComponent.KeyClicked += new KeyClick(HandleIt);

void HandleIt(String key)
this.textbox1.text += key;



This sounds like its down the right path, but I have never created a new event - how exactly would I go about doing this?
Justaddwater
Justaddwater
Your component class

namespace Whatev{    public delegate void KeyButtonPush(String button);    public partial class HandyThing : UserControl    {        public event KeyButtonPush ButtonPushThing;        void MyKeyButtonPush(string button)        {            if (ButtonPushThing != null)            {                ButtonPushThing(button);            }        }        public HandyThing()        {            InitializeComponent();        }        private void button2_Click(object sender, EventArgs e)        {            Button x = sender as Button;            MyKeyButtonPush(x.Text);        }    }}



Then in your main application you add the following to get it...


namespace Whatev{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            HandyThing handyThing = new HandyThing();            handyThing.Dock = Dockstyle.Fill;            Controls.Add(handyThing);            handyThing.ButtonPushThing += new KeyButtonPush(handyThing_ButtonPushThing);        }        void handyThing_ButtonPushThing(string button)        {            MessageBox.Show(button);        }    }}
MCeltic33
MCeltic33
Quote:
Original post by Justaddwater

...code



This almost worked, but for whatever reason - it will display the message box twice (or any code I insert there will execute twice)...and I am not double clicking the button or anything.

Any ideas?
Justaddwater
Justaddwater
You use the exact code? I tested it in VS 2005 and it works fine.

That being said, I assume you broke it down a little different, make sure there is no way the event handler is being wired up more then once.

  handyThing.ButtonPushThing += new KeyButtonPush(handyThing_ButtonPushThing);


That is most likly the culprit...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.