-2

So I need to hide one of the panels named imgX, X being a number from 1 to infinity. Every Image has a button next to it with the text x, x being the same number as the x from the img name. So I need to somehow use the number x to hide the corresponding img. Any help?

Edit: I managed to get it working, I used this piece of code and it worked:
string ctrlName = ((Control)sender).Text; Panel tbx = this.Controls.Find("Img" + ctrlName, true).FirstOrDefault() as Panel; tbx.Hide();

  • Winforms? Web? Wpf? What have you tried? – Crowcoder Jan 27 '18 at 12:12
  • I'm sorry for providing such little info. I'm using windows forms and I tried to do something like `string ctrlName = ((Control)sender).Text; MessageBox.Show("Img" + ctrlName); ("Img" + ctrlName).Hide();` which doesn't work. – Iustin Alex Jan 27 '18 at 12:17
  • You can get the control and hide it this way: `this.Controls.OfType().FirstOrDefault("Img" + ctrlName)?.Hide();` – Felix Almesberger Jan 27 '18 at 12:17
  • It tells me that I cannot convert the second argument from string to System.Func – Iustin Alex Jan 27 '18 at 12:21
  • 1
    Possible duplicate of [Get a Windows Forms control by name in C#](https://stackoverflow.com/questions/1536739/get-a-windows-forms-control-by-name-in-c-sharp) – Crowcoder Jan 27 '18 at 12:26
  • Just to correct my mistake: and as provided by the answer of nino. The correct code is `this.Controls.OfType().FirstOrDefault(x => x.Name == "Img" + ctrlName)?.Hide();` – Felix Almesberger Jan 27 '18 at 12:30
  • That gave no more errors but sadly it doesn't seem to do anything, and yes the button works as it shows a message box. – Iustin Alex Jan 27 '18 at 12:41
  • Did you read the linked question? BTW, the `Tag` property is usually a better choice than the `Name` property when you have to find a control by a string. – Crowcoder Jan 27 '18 at 12:55
  • Thank you, everyone, for the help. I know you tried your best and the problem was mostly me, I'll try to make it work in some other way. Thank you for your help. – Iustin Alex Jan 27 '18 at 13:08

1 Answers1

0

since you provided almost no information, i can only guess what you need. so maybe this can help you:

public Form1()
{
    InitializeComponent();
    //attach same click handler on *all *buttons.
    //that can be made like this, in form constructor, or via form designer
    button1.Click += Button_Click;
    button2.Click += Button_Click;
    button3.Click += Button_Click;
    button4.Click += Button_Click;
    //etc...
    //notice that right part is always the samme :)
}

//handler method
private void Button_Click(object sender, EventArgs e)
{
    //get button that called this method by casting it from object
    var btn = sender as Button;
    //use regex to get number from button and construct panel name
    var number = new Regex(@"\d+").Match(btn.Name);
    var targetPanel = "img" + number.Value.ToString();
    //find all controls of type panel, and then single one that has "targetPanel" name
    var pnl = this.Controls.OfType<Panel>().Single(pnl => pnl.Name == targetPanel):
    //make it invisible
    pnl.Visible = false;
}
Nino
  • 6,931
  • 2
  • 27
  • 42