Is it possible to have a Search box on a form that searches through the labels in a panel with numerous tabs to then to go that label?
Asked
Active
Viewed 62 times
-1
-
Yes it's absolutely possible. Iterate through the form child controls and check if it's a label then the rest goes to you. – Formula12 Jun 18 '20 at 10:10
-
you made that sound easy. An y chance of code example please – Matthew Rodwell Jun 18 '20 at 10:37
2 Answers
2
Yes you can, you can search for the text in all the controls and then switch the selected Tab as below
foreach (TabPage tab in tabControl1.TabPages)
{
var controls = tab.Controls;
foreach(Control c in controls)
if (c.GetType() == typeof(Label))
{
//depends if you want to search by label text or name
string txt = c.Text;
string name = c.Name;
if (txt == textbox1.Text.Trim()) tabControl1.SelectedTab = tab;
}
}

Alaa Mohammed
- 382
- 2
- 14
-
-
-
is there anyway of getting it to be more includes the text or like? – Matthew Rodwell Jun 18 '20 at 11:53
-
Instead of `txt == txtBox1.Text` ... use [_Fuzzy Text Matching_](https://stackoverflow.com/questions/8218553/fuzzy-text-matching-c-sharp) – Wyck Jun 18 '20 at 13:22
-
0
Is there anyway of talking off case sensitive
private void button4_Click(object sender, EventArgs e)
{
foreach (TabPage tab in tabControl1.TabPages)
{
var controls = tab.Controls;
foreach (Control c in controls)
if (c.GetType() == typeof(Label))
{
//depends if you want to search by label text or name
string txt = c.Text;
//string name = c.Name;
//MessageBox.Show(txt);
if (txt.Contains (textBox2.Text.Trim())) tabControl1.SelectedTab = tab;
//MessageBox.Show(textBox2.Text.Trim());
}

Matthew Rodwell
- 11
- 5