Results 1 to 7 of 7

Thread: Radiobutton loop

  1. #1

    Thread Starter
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Radiobutton loop

    Hi

    I was just wondering if any of you guys know how to do this in c#

    Code:
    for (int cntr = 0; cntr < 2; cntr++)
    {
          rbtnAns[cntr].text = arrChoice[cntr];
    }
    Basically, i've got three radiobuttons (rbtnAns1, rbtnAns2 & rbtnAns3) which should display arrChoice that I've declared and created earlier as arrays.

    I've tried using this code in c#

    Code:
    for (int cntr = 0; cntr < 2; cntr++)
    {
    ((RadioButton)this.FindControl("rbtnAns" + (cntr+1))).Text = (arrChoice[cntr]).Tostring();
    }
    and include these header files:

    using System.Web;

    I was going to add using System.Web.UI----- but this option was not available

    this means i cant get the FindControl() thing to work

    Any help will be appreciated

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Radiobutton loop

    Is this a WinForms app or an ASP.NET app? If it's WinForms then of course not, because Web.UI is for Web applications. You've got three RadioButtons so why do you even need a loop? Also, are you sure that RadioButtons are what you want? Only one of them can be checked at a time. If that's what you want then cool but if not then you should use CheckBoxes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Radiobutton loop

    I'm using WinForms app...

    I chose to use the radiobuttons because i'm creating a multiple choice test program for Year 7 pipol....

    All the preset questions and answers are declared and saved as an array...

    i know it is possible to do using something like this:

    Code:
    public virtual Control FindControl (string id)
    {
    
          //not sure though what to write here!
    
    }
    but without using needing to use the header file (using system.Web) at all:

  4. #4

    Thread Starter
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Radiobutton loop

    referring to the one above:

    where id is the identifier for the control to be found

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Radiobutton loop

    I've posted code in then Codebank to visit every control on a form. You could use that and test the Name property until you find a match.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Radiobutton loop

    Can u pls explain on how im going to do that....

    Code:
    Control ctl = this.GetNextControl(this, true); // Get the first control in the tab order.
    
    while (ctl != null)
    {
        // Use ctl here. --> what to put in here in this case
    
        ctl = this.GetNextControl(ctl, true); // Get the next control in the tab order.
    }
    I'm still kinda new to this type of lang..

    sorz for the trouble

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Radiobutton loop

    You want to find a control that has a particular name, so compare the Name of the control to the name you're looking for. If they are the same then retrun that control. If you get to the end of the loop without finding a match return null.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width