So I wanted to make a program where you can add a textbox or what ever for stuff.
The code typed in the textbox is also a part of result in the results.

But if the control doesn't exists, than the result is NOT related to the textbox.
If the control does exists, than the result is related to the textbox.

My question now is, how to check if a textbox (or a control, no matter which) does exists.
I thought about something like this:

Code:

if (Control::Exists("textBox1")
{
}

But this one doesn't work.

I also tried this:

Code:

if (textBox1 == NULL)
{
}

This one does neither work.

I got one simple solution, and that is the findControl stuff.
But I only can find those for VB or C#.

So someone got me a better solution or can explain the C# findControl stuff to me?

Thanks,
Miraclezz

April 6th, 2012, 08:34 PM

Eri523

Re: Find control or check if control exists

You already have noticed that there's no such thing as a static Control::Exists() method, and I, personally, have no idea of any "C# findControl stuff". Perhaps someone who knows C# better than I do can tell you about that. But to check whether a given control is present on a given form, you may inspect that form's Controls property. However, which particular criterium of the controls in there you're going to inspect depends on your concrete scenario, that I don't know of course.

Checking for textBox1 == NULL is basically a valid idea, except that (1) in C++/CLI you'd rather need to compare against nullptr instead of NULL (though I, myself, never actually tried to check against NULL in C++/CLI; maybe that even would work given you include a header that defines NULL) and (2) a control variale you created using the IDE named textBox1 would of course be != nullptr since it gets assigned a valid control in the form's InitializeComponent() method.

All the above pertains to checking for the existence of a given control on one given form. Checking whether a given control exists somewhere throughougt your entire app is possible as well, yet considerably more complicated.