Using Controls in an array


I need to reference an image control and a text box control in an array.

This is what I have:

   string[,] arrShowHands = new string [4,3];
        arrShowHands[1,1] = txtDealt1.Text;
        arrShowHands[1,2] = "imgPlayer1H1";

This works without an array:

imgPlayer1H1.ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt1.Text)] + strExt;
imgPlayer1H2.ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt2.Text)] + strExt;
imgPlayer2H1.ImageUrl = strImage etc....
However, I have several image and text box controls and wish to use a loop thru an array instead of having seperate statements.

This is what I would prefer:

 for (int intRowCtr = 1; intRowCtr < 4; intRowCtr++)
        {
             arrShowHands[intRowCtr,2] .ImageUrl = strImage +
             arrCardDeck[Convert.ToInt16( arrShowHands[intRowCtr,1] )] + strExt;
        }

arrShowHands[intRowCtr,2] needs to represent an image control in the statement.
arrShowHands[intRowCtr,1] needs to represent a text box control in the statement.

Thanks,
DovbermanAsked:
Who is Participating?
 
ddayx10Commented:
You cannot do this:

arrShowHands[intRowCtr,2] .ImageUrl =

Because the value you have there is a string and that can not be converted to an object of type Image which is what you need to use the .ImageUrl property.

Note you said you could do this:
imgPlayer1H1.ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt1.Text)] + strExt;

But what you cannot do is this:
arrShowHands[1,2].ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt1.Text)] + strExt;

If you want to be able to access the image and textbox and their values in a for loop then you need to change your model around a bit. It will require some re-thinking of how you are currently doing things but it is the direction you need to go if you want more automation.

You are not going to be able to convert a string id of a control into a control unless you create a variable in your loop and use .FindControl("id") to get it. I dont suggest that.

<see snippet>
//note you didnt provide these so I had to make them up
//you also didnt provide "arrCardDeck" so I didnt incorporate it into my sample
string strImage = "Some String";
TextBox txtDealt1 = new TextBox();
txtDealt1.Text = " Some TextBox Text";
Image imgPlayer1H1 = new Image();

//Make an array of a type that it can hold the controls you want to
//take values from and put values into
Control[,] arrShowHands = new Control [4,3];
arrShowHands[1,1] = txtDealt1;
arrShowHands[1,2] = imgPlayer1H1;

//This works without an array:
//imgPlayer1H1.ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt1.Text)] + strExt;
////This works with your array:
((Image)arrShowHands[1, 2]).ImageUrl = strImage;

for (int intRowCtr = 1; intRowCtr < 2; intRowCtr++)
{
    ((Image)arrShowHands[intRowCtr, 2]).ImageUrl = strImage + ((TextBox)arrShowHands[intRowCtr, 1]).Text;
}

Open in new window

0
 
nishant joshiTechnology Development ConsultantCommented:
check code...

regards,
nishant
string[,] arrShowHands = new string [4,2]; //2 for only two field
arrShowHands[0,0] = txtDealt1.Text;
arrShowHands[0,1] = "imgPlayer1H1";
Image[] imgPlayer=new Image[];

for(int i=0;i<arrShowHands.Length;i++)
{
    arrShowHands[i+1,2].ImageUrl=arrShowHands[i][1];
    arrShowHands[i+1,1].text=arrShowHands[i][0];
}

Open in new window

0
 
DovbermanAuthor Commented:
Image[] imgPlayer=new Image[];
        //Error      3      
        //Array creation must have array size or array initializer      
        //C:\Users\Admin\Documents\Visual Studio 2008\WebSites\HoldemyWay\PreFlopPractice.aspx.cs
        //2406      28      C:\...\HoldemyWay\

    //Error      5      
        //'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image'
        //and 'System.Drawing.Image'      
        //C:\Users\Admin\Documents\Visual Studio 2008\WebSites\HoldemyWay\PreFlopPractice.aspx.cs
        //2406      27      C:\...\HoldemyWay\
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
DovbermanAuthor Commented:
   //Error      7      
        //'string' does not contain a definition for 'ImageUrl'
        //and no extension method 'ImageUrl' accepting a first argument of type 'string'
        //could be found (are you missing a using directive or an assembly reference?)      
        //C:\Users\Admin\Documents\Visual Studio 2008\WebSites\HoldemyWay\PreFlopPractice.aspx.cs
        //2422      25      C:\...\HoldemyWay\

    //Error      8      Wrong number of indices inside []; expected '2'      
    //C:\Users\Admin\Documents\Visual Studio 2008\WebSites\HoldemyWay\PreFlopPractice.aspx.cs
    //2422      34      C:\...\HoldemyWay\


      //Error      9      
    //'string' does not contain a definition for 'text'
    //and no extension method 'text' accepting a first argument of type 'string' could be found
    //(are you missing a using directive or an assembly reference?)      
    //C:\Users\Admin\Documents\Visual Studio 2008\WebSites\HoldemyWay\PreFlopPractice.aspx.cs      
    //2436      25      C:\...\HoldemyWay\

}
0
 
DovbermanAuthor Commented:
I could not even put the following statements into a comment:

arrShowHands.ImageUrl=arrShowHands
arrShowHands.text=arrShowHands
0
 
DovbermanAuthor Commented:

The statement that creates an arraya of type Control was the secret.
Also the( (Image)arrayname[i,j]).ImageUrl was valuable help.

It works in an array loop now.

       System.Web.UI.Control[,] arrShowHands = new System.Web.UI.Control[4, 3];
        arrShowHands[1, 1] = txtDealt1;
        arrShowHands[1, 2] = imgPlayer1H1;
        ((System.Web.UI.WebControls.Image)arrShowHands[1, 2]).ImageUrl = strImage;
        imgPlayer1H1.ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt1.Text)] + strExt;
        ((System.Web.UI.WebControls.Image)arrShowHands[1, 2]).ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt1.Text)] + strExt;
               
Thank you,
0
 
DovbermanAuthor Commented:
The statement that creates an arraya of type Control was the secret.
Also the( (Image)arrayname[i,j]).ImageUrl was valuable help.

It works in an array loop now.

       System.Web.UI.Control[,] arrShowHands = new System.Web.UI.Control[4, 3];
        arrShowHands[1, 1] = txtDealt1;
        arrShowHands[1, 2] = imgPlayer1H1;
        ((System.Web.UI.WebControls.Image)arrShowHands[1, 2]).ImageUrl = strImage;
        imgPlayer1H1.ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt1.Text)] + strExt;
        ((System.Web.UI.WebControls.Image)arrShowHands[1, 2]).ImageUrl = strImage + arrCardDeck[Convert.ToInt16(txtDealt1.Text)] + strExt;
               
Thank you,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.