quick easy answer for this question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jimi_xyz@hotmail.com

    #1

    quick easy answer for this question

    I am kind of new to C sharp; I have a quick question I have multiple
    text boxes 12 of them to be exact. What I want to do is fill them with
    values that are in a array of integers. The way I am currently doing
    this is like this..

    input0.Text = Convert.ToStrin g(arr[0]);
    input1.Text = Convert.ToStrin g(arr[1]);
    input2.Text = Convert.ToStrin g(arr[2]);
    input3.Text = Convert.ToStrin g(arr[3]);
    input4.Text = Convert.ToStrin g(arr[4]);
    input5.Text = Convert.ToStrin g(arr[5]);
    input6.Text = Convert.ToStrin g(arr[6]);
    input7.Text = Convert.ToStrin g(arr[7]);
    input8.Text = Convert.ToStrin g(arr[8]);
    input9.Text = Convert.ToStrin g(arr[9]);
    input10.Text = Convert.ToStrin g(arr[10]);
    input11.Text = Convert.ToStrin g(arr[11]);

    What I am looking for is about 3 lines of code; something like this...

    for(i = 0; i< arr.Length; i++)
    {
    input[i].Text = Convert.ToStrin g(arr[i]);
    //NOTE: I am using [i] as the subscript for the texboxes
    }

    I doubt this can be done in C sharp; but any help or ideas will be
    appreciated. Also note that I posted the same message on the c-sharp
    message board; but it doesn't look like it gets as much traffic as this
    group does.

    Thanks,
    -Jimmie

  • Kevin Spencer

    #2
    Re: quick easy answer for this question

    > What I am looking for is about 3 lines of code; something like this...[color=blue]
    >
    > for(i = 0; i< arr.Length; i++)
    > {
    > input[i].Text = Convert.ToStrin g(arr[i]);
    > //NOTE: I am using [i] as the subscript for the texboxes
    > }[/color]

    What youu're talking about doing is using an indexer. In order to do that,
    the Controls have to be in an array or a Collection. Of course, you can put
    them into one if you wish.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    We got a sick zebra a hat,
    you ultimate tuna.


    <jimi_xyz@hotma il.com> wrote in message
    news:1139603503 .056063.87570@g 43g2000cwa.goog legroups.com...[color=blue]
    >I am kind of new to C sharp; I have a quick question I have multiple
    > text boxes 12 of them to be exact. What I want to do is fill them with
    > values that are in a array of integers. The way I am currently doing
    > this is like this..
    >
    > input0.Text = Convert.ToStrin g(arr[0]);
    > input1.Text = Convert.ToStrin g(arr[1]);
    > input2.Text = Convert.ToStrin g(arr[2]);
    > input3.Text = Convert.ToStrin g(arr[3]);
    > input4.Text = Convert.ToStrin g(arr[4]);
    > input5.Text = Convert.ToStrin g(arr[5]);
    > input6.Text = Convert.ToStrin g(arr[6]);
    > input7.Text = Convert.ToStrin g(arr[7]);
    > input8.Text = Convert.ToStrin g(arr[8]);
    > input9.Text = Convert.ToStrin g(arr[9]);
    > input10.Text = Convert.ToStrin g(arr[10]);
    > input11.Text = Convert.ToStrin g(arr[11]);
    >
    > What I am looking for is about 3 lines of code; something like this...
    >
    > for(i = 0; i< arr.Length; i++)
    > {
    > input[i].Text = Convert.ToStrin g(arr[i]);
    > //NOTE: I am using [i] as the subscript for the texboxes
    > }
    >
    > I doubt this can be done in C sharp; but any help or ideas will be
    > appreciated. Also note that I posted the same message on the c-sharp
    > message board; but it doesn't look like it gets as much traffic as this
    > group does.
    >
    > Thanks,
    > -Jimmie
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: quick easy answer for this question

      <jimi_xyz@hotma il.com> wrote:[color=blue]
      > I am kind of new to C sharp; I have a quick question I have multiple
      > text boxes 12 of them to be exact. What I want to do is fill them with
      > values that are in a array of integers. The way I am currently doing
      > this is like this..[/color]

      <snip>
      [color=blue]
      > What I am looking for is about 3 lines of code; something like this...
      >
      > for(i = 0; i< arr.Length; i++)
      > {
      > input[i].Text = Convert.ToStrin g(arr[i]);
      > //NOTE: I am using [i] as the subscript for the texboxes
      > }
      >
      > I doubt this can be done in C sharp; but any help or ideas will be
      > appreciated. Also note that I posted the same message on the c-sharp
      > message board; but it doesn't look like it gets as much traffic as this
      > group does.[/color]

      The best thing to do would be to have an array instead of 12 different
      fields. You won't get a lot of designer support for it, but it's not
      hard to do manually.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • David Barkol

        #4
        Re: quick easy answer for this question

        First, you should be using generics instead of an array. See:
        http://msdn.microsoft.com/library/de...p_generics.asp

        Now, for a WinForm app:

        for (int i = 0; i < list.Count; i++)
        {
        TextBox t = (TextBox)
        Controls[string.Format(" TextBox{0}", i)];
        t.Text = list[i].ToString();
        }

        For a WebForm app:
        for (int i = 0; i < list.Count; i++)
        {
        TextBox t = (TextBox)
        FindControl(str ing.Format("Tex tBox{0}", i));
        t.Text = list[i].ToString();
        }

        David Barkol
        We turn opportunity into reality—combining our industry expertise and AI innovation assets to drive growth, efficiency and competitive advantage


        Comment

        • jimi_xyz@hotmail.com

          #5
          Re: quick easy answer for this question

          Aight kool so basically search indexer + examples on the net?

          -Jimmie

          Comment

          • jimi_xyz@hotmail.com

            #6
            Re: quick easy answer for this question

            Ahh aight good deal guys, thanks for the info.

            -Jimmie

            Comment

            • Justin Swan (MSFT)

              #7
              RE: quick easy answer for this question

              Hi jimi,

              You actually can do this in C# with some careful control naming. Below is
              some sample code that should help:

              //There are 9 text boxes, what this will do is set the text in
              each of the
              //textboxes to the value of that textboxes index in the
              form.control array.
              for (int i = 1; i < 9; i++)
              {
              //this call will find the location of the textbox by name,
              so it's important
              //to note that my textboxes are named textBox1,textBo x2,etc.
              int controlLocation = this.Controls.I ndexOfKey("text Box" +
              i.ToString());

              //Now we access that control through the form.control array
              and change it's text
              this.Controls[controlLocation].Text = i.ToString();
              }

              That should help you out hopefully! If this is what you were looking for
              please remember to mark the thread as answered. Thanks!
              "jimi_xyz@hotma il.com" wrote:
              [color=blue]
              > I am kind of new to C sharp; I have a quick question I have multiple
              > text boxes 12 of them to be exact. What I want to do is fill them with
              > values that are in a array of integers. The way I am currently doing
              > this is like this..
              >
              > input0.Text = Convert.ToStrin g(arr[0]);
              > input1.Text = Convert.ToStrin g(arr[1]);
              > input2.Text = Convert.ToStrin g(arr[2]);
              > input3.Text = Convert.ToStrin g(arr[3]);
              > input4.Text = Convert.ToStrin g(arr[4]);
              > input5.Text = Convert.ToStrin g(arr[5]);
              > input6.Text = Convert.ToStrin g(arr[6]);
              > input7.Text = Convert.ToStrin g(arr[7]);
              > input8.Text = Convert.ToStrin g(arr[8]);
              > input9.Text = Convert.ToStrin g(arr[9]);
              > input10.Text = Convert.ToStrin g(arr[10]);
              > input11.Text = Convert.ToStrin g(arr[11]);
              >
              > What I am looking for is about 3 lines of code; something like this...
              >
              > for(i = 0; i< arr.Length; i++)
              > {
              > input[i].Text = Convert.ToStrin g(arr[i]);
              > //NOTE: I am using [i] as the subscript for the texboxes
              > }
              >
              > I doubt this can be done in C sharp; but any help or ideas will be
              > appreciated. Also note that I posted the same message on the c-sharp
              > message board; but it doesn't look like it gets as much traffic as this
              > group does.
              >
              > Thanks,
              > -Jimmie
              >
              >[/color]

              Comment

              • jimi_xyz@hotmail.com

                #8
                Re: quick easy answer for this question

                Not sure on how to mark as answered; but thank you everyone.

                -Jimmie

                Comment

                Working...