Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get an object by name 1

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
0
0
US
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
 
Is there a way to get an object by name?
Code:
Control control = Controls.Find("lbSelectName", true);

I also know which tab I am on by enums: Select, Insert Update, e.t.c.
Code:
[green]// You also know which tab you are on by:[/green]
var selectedTab = tabControl1.SelectedTab;

[green]// And to get the ListBox in that selected tab:[/green]
ListBox lb = tabControl1.SelectedTab.Controls.OfType<ListBox>().FirstOrDefault();
 
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
 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top