krik
krik

Reputation: 3

Checking multiple Radio buttons from different group boxes in c#

I am a newbie in C# I just have a doubt.I have two group boxes,each of them has three radio buttons in it,If I want to select each radio button from each groupbox and write a condition for that ,,How can I do that. Below is the code:

   public void SaveMyTextBoxContents()
        {

            string path = usbLetter +"MSREAD.txt";

            if (lbItems.SelectedIndex == -1)

            {

                        if (rdBtnMed.Checked)
                        {
                            using (StreamWriter File = new StreamWriter(filepath))
                            {
                                foreach (string item in lbItems.Items)
                                {
                                    saveAllText = medium + " " + item;
                                    outputFile.WriteLine(saveText);
                                }
                            }
                        }                        
}
                        else if (rdBtnMedium.Checked && rdBtnN.Checked)
                        {
                            using (StreamWriter File = new StreamWriter(filepath))
                            {
                                foreach (string item in lbItems.Items)
                                {
                                    saveAllText = mediumNo + " " + item;
                                    outputFile.WriteLine(saveText);
                                }
                            }
                        }



}
}  

Please help me I got stuck up with this.

Thanks Krik

Upvotes: 0

Views: 3616

Answers (2)

M. Suleman Shafique
M. Suleman Shafique

Reputation: 29

Make small panels to the size of radio button pairs and place those radio buttons on them (two each). E.g separate for male, female; separate for young, old etc. The panel won't show on run. This will work Insha'Allah in a group box too.

Upvotes: 2

Grant Winney
Grant Winney

Reputation: 66449

You're brackets are off, so it only checks rdBtnMedium and rdBtnN if lblItems.SelectedIndex != 1. Here's what I think you need:

if (lbItems.SelectedIndex == -1)
{
    if (rdBtnMed.Checked)
    {
        using (StreamWriter File = new StreamWriter(filepath))
        {
            foreach (string item in lbItems.Items)
            {
                 saveAllText = medium + " " + item;
                 outputFile.WriteLine(saveText);
            }
        }
    }                        
    else if (rdBtnMedium.Checked && rdBtnN.Checked)
    {
        using (StreamWriter File = new StreamWriter(filepath))
        {
            foreach (string item in lbItems.Items)
            {
                saveAllText = mediumNo + " " + item;
                outputFile.WriteLine(saveText);
            }
        }
    }
}

Upvotes: 0

Related Questions