How to find objects using Name property

  • Thread starter Thread starter Ishmael
  • Start date Start date
I

Ishmael

I'm trying to find all the objects in a form whose name begins with
some string, for example "textBox1", "textBox2", "textBox3".

I tried to modify the code from this reference
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.name(VS.85).aspx
but I'm having trouble adding new controls to a ControlCollection.

The error I'm getting is:
"Object reference not set to an instance of an object."

Here's my code:

Control::ControlCollection ^controls;

// Loop through all controls in the form's control collection.
IEnumerator^ myEnum = this->Controls->GetEnumerator();
while ( myEnum->MoveNext() )
{
Control^ tempCtrl = safe_cast<Control^>(myEnum->Current);

if (tempCtrl->Name->StartsWith("mystring")) {
controls->Add(tempCtrl);
}
}
return(controls);

There's something I'm missing here. Any help will be greatly
appreciated!
 
I'm trying to find all the objects in a form whose name begins with
some string, for example "textBox1", "textBox2", "textBox3".

I tried to modify the code from this reference
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.name(VS.85).aspx
but I'm having trouble adding new controls to a ControlCollection.

The error I'm getting is:
"Object reference not set to an instance of an object."

Here's my code:

Control::ControlCollection ^controls;

// Loop through all controls in the form's control collection.
IEnumerator^ myEnum = this->Controls->GetEnumerator();
while ( myEnum->MoveNext() )
{
Control^ tempCtrl = safe_cast<Control^>(myEnum->Current);

if (tempCtrl->Name->StartsWith("mystring")) {
controls->Add(tempCtrl);
}
}
return(controls);

There's something I'm missing here. Any help will be greatly
appreciated!

This line of code:
Control::ControlCollection ^controls;
creates a variable to hold a reference to an object, but you never set
it to anything. You need to allocate a new instance of
ControlCollection and store it into the variable. I am not very
familiar with C++, but my guess is you need to do something like:

Control::ControlCollection ^controls = gcnew
Control::ControlCollection();

Some other comments on this code:

Doesn't C++ have some sort of For Each construct that you could use
rather than explicitly messing with enumerators?

You will only find controls that are direct children of the form. If
you want to find nexted controls (such as a textbox in a panel) you
will need to recurse into the children of each control you find.

You may want to use a generic list rather than ControlCollection. I
suspect ControlCollection includes specialized code to deal with
collections of controls that are children of the control that "owns"
the collection, and that code may possibly do things you don't want
(such as changing the Parent property when you add controls to it).
 
Oops. Thanks for the help.

This fixes the error.
Control::ControlCollection ^controls = gcnew
Control::ControlCollection(this);

Still trying to get the code to work though. The 'foreach' command is
definitely much more elegant, but since I already started with C++, I
might as well stick it out.
 
Ok, got it working. Good call on the problems created by
ControlCollection. In case anyone is interested, here is how I did
it.


private: array<System::Windows::Forms::Control^,1>^
find_controls_starting_with(String ^ str) {

array<System::Windows::Forms::Control^,1>^ controls = gcnew
array<System::Windows::Forms::Control^,1>(0);
int k = 0;

// Loop through all controls in the form's control collection.
// Use enumerator to implement 'foreach' (doesn't exist in C++)
IEnumerator^ myEnum = this->Controls->GetEnumerator();
while ( myEnum->MoveNext() )
{
Control^ tempCtrl = safe_cast<Control^>(myEnum->Current);

if (tempCtrl->Name->StartsWith(str)) {
Array::Resize(controls, controls->Length+1);
controls[k] = tempCtrl;
k++;
}
}
return(controls);
}
 
Back
Top