Drew
Drew

Reputation: 23

Radio buttons not working to select something

I am trying to create a dialogue box that contains 3 radio buttons and a "Present" button to be used with some projector software we have. What I want to do is, after clicking "Present", open a file specific to which room was chosen with the radio buttons.

The issue is, after clicking "Present" nothing happens.

I know Process.Start and present_Click are correct; in a test project I did the "Present" button successfully opens a file. I am sure the CheckedChanged parts are probably incorrect, but I am confused as to what to do with them.

Here's my code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;

    namespace WindowsFormsApplication1
    {
       public partial class Form1 : Form
       {
           public Form1()
           {
               InitializeComponent();
           }

           public void present_Click(object sender, EventArgs e)
           {
               if (room1.Checked)
               {
                    System.Diagnostics.Process.Start(@"room1.txt");
               }
               else if (room2.Checked)
               {
                    System.Diagnostics.Process.Start(@"room2.txt");
               }
               else if (room3.Checked)
               {
                   System.Diagnostics.Process.Start(@"room3.txt");
               }
           }

           private void room1_CheckedChanged(object sender, EventArgs e)
           {
               room1.Checked = true;
           }

           private void room2_CheckedChanged(object sender, EventArgs e)
           {
               room2.Checked = true;
           }

           private void room3_CheckedChanged(object sender, EventArgs e)
           {
               room3.Checked = true;
           }
       }

Can someone please help me with these radio buttons?

Upvotes: 0

Views: 734

Answers (2)

Vlad
Vlad

Reputation: 1919

I think you need to remove the events which trigger upon checking or unchecking. If I understand properly, you only want 'something' to happen when you click Present. So once someone clicks Present, then verify which checkboxes are on/off and do the appropriate thing:

Sorry if I'm making the wrong assumptions, feel free to correct.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {       
        public Form1()
        {
            InitializeComponent();
        }

        public void present_Click(object sender, EventArgs e)
        {
            if (room1.Checked)
            {
                System.Diagnostics.Process.Start(@"room1.txt");
            }
            else if (room2.Checked)
            {
                System.Diagnostics.Process.Start(@"room2.txt");
            }
            else (room3.Checked)
            {
                System.Diagnostics.Process.Start(@"room3.txt");
            }
        }
    }
}

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61379

Your CheckedChanged event handlers are definitely messed up.

In english, your logic says

Anytime a radio button's checked status is changed, set its checked property to true

Honestly I'm surprised you don't get into some infinite loop where every control is fighting to be the "selected one". Removing that logic altogether may well fix your problem. It also seems odd that you are "starting" a text file, but I assume that is intentional.

The important knowledge here is that a user clicking the radio button automatically sets the Checked property. There is no need to do it yourself in code-behind.

Upvotes: 3

Related Questions