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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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

arrShowHands.ImageUrl=arrShowHands
arrShowHands.text=arrShowHands
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.