×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Get an object by name

Get an object by name

Get an object by name

(OP)
Is there a way to get an object by name?

I have tabs that are identical except for how they are used.

So I have a list box on each tab named by function: lbSelectName, lbInsertName, lbUpdataName, e.t.c.

I also know which tab I am on by enums: Select, Insert Update, e.t.c.

Is there a way to get object (ListBox, TextBox, CheckedListBox) by name that I can than pass to a method that only knows the type of object?

So I would do something like:

CODE

ListBox lb;

switch(mode)
{
   case enum.Select:
      lb = findcontrol("lbSelectName");
      break;
   case enum.Insert:
      lb = findcontrol("lbInsertName");
      break;
   case enum.Update:
      lb = findcontrol("lbUpdateName");
      break;
}

ProcessSave(lb); 

Thanks,

Tom

RE: Get an object by name

Quote:

Is there a way to get an object by name?

CODE --> Yes

Control control = Controls.Find("lbSelectName", true); 

Quote:

I also know which tab I am on by enums: Select, Insert Update, e.t.c.

CODE

// You also know which tab you are on by:
var selectedTab = tabControl1.SelectedTab;

// And to get the ListBox in that selected tab:
ListBox lb = tabControl1.SelectedTab.Controls.OfType<ListBox>().FirstOrDefault(); 

RE: Get an object by name

(OP)
Perfect.

I assume I can change the last piece to:

// And to get the ListBox in that selected tab:
ListBox lb = tabControl1.SelectedTab.Controls.OfType<ListBox>().FirstOrDefault();

To

// And to get the ListBox in that selected tab:
ListBox lb = tabControl1.SelectedTab.Controls.Find("lbSelectName", true);

This would be the one I would use if there was more than one listbox on the page, correct?

Also, what does "true" denote in the find method?

Thanks

RE: Get an object by name

It won't work for 2 reasons
1) Controls.Find returns an array.
2) The array contains Control(s), not ListBox(es).
I don't know why it would return an array as you can't have more than 1 control with the same name.

RE: Get an object by name

(OP)
It works great.

But there was a small change I had to make.

Control control = Controls.Find("lbSelectName", true);

had to be changed to:

Control control = Controls.Find("lbSelectName", true).First();

So my actual code where I have 3 objects I am trying to get based on clauseType (enum), it looks like:

CODE

CheckedListBox clb = (CheckedListBox)this.Controls.Find("chk" + clauseType.ToString() + "StatementColumns", true).First();

ListBox lbStatementNames = (ListBox)this.Controls.Find("lb" + clauseType.ToString() + "StatementNames", true).First();

TextBox txtStatement = (TextBox)this.Controls.Find("txt" + clauseType.ToString() + "Statement", true).First(); 

Thanks,

Tom

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close