Windows Develop Bookmark and Share   
 index > Windows Forms General > Accessing a control from outside the form class (in form.h)
 

Accessing a control from outside the form class (in form.h)

Hi,
Hopefully someone knows a little programming trick here. :-) A USB device connected to my computer periodically executes a function in a provided DLL file which in turn executes a function in my program. Within my function, I'd like to write some text to textBox1 in the form. Since the function is outside the form class in the form.h file, I have to pass the textbox in as an argument. HOWEVER..... I cannot do this because I cannot edit the parameters of my function (they are dictated by the DLL file).

So how can I access this textbox from a function outside the form's class without passing it into the function as an argument?
--------------------
myFunction(argument1, argument2) //cannot add a textbox argument here :-(
{
//some stuff
}

public ref class Form1 : public System::Windows::Forms::Form
{
public: System::Windows::Forms::TextBox^ textBox1;
//etc.
}
------------------
Is there a way I can make the textbox global to the form.h file instead of global to just the form class? (when I try this, i get a compile error though)
~Daniel
Scraniel  Thursday, March 23, 2006 4:05 AM

I had a similar problem. I solved it by using the Windows Event handling mechanism.

You need to read about delegates and Event handlers in the help (I am assuming you are using Visual Studio or one of the Visual C++, C# or VB editions).

Basically, your function that is called from the DLL has to "raise an Event". You can define a custom Event, and a custom Event handler in your Windows Form1.hmuch like you would handle any other Event from a control in the form.

You have to "wire in" your event handler. This takes a bit of getting used to and a good deal of trial and error.

Hope this helps.

David Harrison.

DavidHarrison  Thursday, March 23, 2006 9:17 PM

Can you guarentee that there will only be one instance of the Form1 class?

if so then you can use a static member on the Form1 class.

myFunction(argument1, argument2) //cannot add a textbox argument here :-(
{
Form1::static_TextBox1->Text = L"Hello world";
}

public ref class Form1 : public System::Windows::Forms::Form
{
public:
static TextBox ^ static_TextBox1;
System::Windows::Forms::TextBox^ textBox1;
//etc.
Form1()
{
textbox1 = gcnew TextBox();
static_TextBox1 = textbox1;
}
}

Remember, this will only work if you have only one instance of Form1.

Regards

Jero

JeroGrav  Thursday, March 23, 2006 9:32 PM

You can use google to search for other answers

Custom Search

More Threads

• Casting an Instance of Class by Type
• How do close Execl sheet?
• post a problem
• ComboBox very slow adding items when in userControl
• Applying skins in windows application
• Print settings
• Questions w/Web Broswer & Windows Form App
• Late binding/Invoke Script
• When is MTAThread safe?
• Ways of integration for 2 Windows Forms executables ( .Net 2.0 )