3 Replies - 8623 Views - Last Post: 09 December 2010 - 10:11 AM Rate Topic: -----

#1 BabyShark011  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 20
  • Joined: 10-July 10

C# substitute string variable for control name

Posted 08 December 2010 - 11:10 PM

In the code below I am trying to substitute the control name of a picture box with a string variable
so I can eventually index through a number of the picture boxes and change properties on them.

This code generates a compiler error which is below.

            string myPictureBox = "pictureBox1";
           
            this.myPictureBox.Visible == true;  


Error 1 'WindowsFormsApplication_Cards_using_collections.CardImages' does not contain a definition for 'myPictureBox ' and no extension method 'myPictureBox ' accepting a first argument of type 'WindowsFormsApplication_Cards_using_collections.CardImages' could be found (are you missing a using directive or an assembly reference?)

This post has been edited by modi123_1: 08 December 2010 - 11:17 PM
Reason for edit:: fixed code tags..


Is This A Good Question/Topic? 0
  • +

Replies To: C# substitute string variable for control name

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9056
  • View blog
  • Posts: 33,997
  • Joined: 12-June 08

Re: C# substitute string variable for control name

Posted 08 December 2010 - 11:16 PM

As your code is stated you have a string called 'my picture box'... I am certain the string class does not have '.visible' as a member so you can't use that. You would have to reference the object itself before you can use the members associated with it. Basically you are trying to magic wand a string to a picturebox object and that won't fly.

You might be best trying to reference through the control's object list and find the one with the name of your string...
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# substitute string variable for control name

Posted 09 December 2010 - 05:59 AM

You could do this though...

string controlName = "pictureBox1";

PictureBox picBox = this.Controls.Find(controlName, true) as PictureBox;
picBox.Visible = true;


Was This Post Helpful? 1
  • +
  • -

#4 BabyShark011  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 20
  • Joined: 10-July 10

Re: C# substitute string variable for control name

Posted 09 December 2010 - 10:11 AM

Thank you to all who responded.

The following is an example of what will work.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication_Cards_using_collections
{
    public partial class CardImages : Form
    {
        public PictureBox[] myCard = new PictureBox[52];

        private void CardImages_Load(object sender, EventArgs e)
        {
            myCard[0] = card0;
            myCard[1] = card1;
            myCard[2] = card2;
            myCard[3] = card3;
            myCard[4] = card4;
            myCard[5] = card5;
            myCard[6] = card6;
            myCard[7] = card7;
            myCard[8] = card8;
            myCard[9] = card9;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                this.myCard[i].Visible = false;
                this.myCard[i].Visible = true;
            }
        }

    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1