3 Replies - 300 Views - Last Post: 15 June 2013 - 04:00 AM Rate Topic: -----

#1 Alwayzconfuzed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 17-May 13

Using a string list to find the name of a control to be removed

Posted 14 June 2013 - 04:04 PM

So I'm creating a "Tally'ing" program that creates and removes picture boxes to show the count.

The code to create the picture box and set its properties and then set its name into a list, works fine.

My Add Tally Button
Spoiler


My problem lies in removing the picture Box by name. I cannot get it to remove the last picture added whenever the button is clicked.
Spoiler


The error it gives me is:
Arguent 1: Cannot convert from 'string' to 'System.Windows.Forms.Control', and will not let me run it.

This is the full code
Spoiler


As well, whenever I use my little debugging button(last block), that unless I set "curTallyCount" to a specific number, it retrieves an invalid value despite that its value is set in the beginning, and can be visually seen in a label.

I've spent the last hour or so trying to phrase that one line of code to something useable to Controls.Remove.

Any help would be greatly appreciated.

AC

Is This A Good Question/Topic? 0
  • +

Replies To: Using a string list to find the name of a control to be removed

#2 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Using a string list to find the name of a control to be removed

Posted 14 June 2013 - 04:14 PM

http://msdn.microsof...emovebykey.aspx maybe?
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Using a string list to find the name of a control to be removed

Posted 15 June 2013 - 03:41 AM

Yes, the error message is very clear. This:

List<string> tallyGrid = new List<string>();

is an array of strings, and the Remove method's argument should be a Control.

So, as Witchking suggests, you might use RemoveByKey.

Quote

The Name property of a Control corresponds to the key for a Control in the Control.ControlCollection.

Was This Post Helpful? 2
  • +
  • -

#4 Alwayzconfuzed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 17-May 13

Re: Using a string list to find the name of a control to be removed

Posted 15 June 2013 - 04:00 AM

Yep that works. Thank you WitchKing and Andrewsw. When I tried RemoveByKey yesterday, I must have done something wrong. Though a fresh start helped, so thank you both again and now I know what RemoveByKey does. :)/>
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1