Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger radio button's event handler programmatically?

please have a look at the following code

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

namespace calc
{
    public partial class Form1 : Form
    {
        //code removed

        public Form1()
        {

            InitializeComponent();
            calculator = new Calculator();

            maleRadio.PerformClick();
            englishRadio.PerformClick();
        }


/*code removed*/

        //Action Listener for Female Radio button
        private void femaleRadio_CheckedChanged(object sender, EventArgs e)
        {
            //code removed
        }

        //Action Listener for English Radio button
        private void englishRadio_CheckedChanged(object sender, EventArgs e)
        {
            //code removed
        }



    }
}

I am somewhat new to c#. what I want to do here is to trigger the event handlers of radio buttons inside the constructor programatically. The way I followed maleRadio.PerformClick();do nothing. how can I call the event handlers inside the constructor programmertically ?

like image 670
PeakGen Avatar asked Jul 26 '13 05:07

PeakGen


2 Answers

You can call the event handlers like any other method:

public Form1()
{
   InitializeComponent();
   calculator = new Calculator();

   maleRadio_CheckedChanged(maleRadio, null);
   englishRadio_CheckedChanged(englishRadio, null);
}
like image 76
Eren Ersönmez Avatar answered Oct 06 '22 00:10

Eren Ersönmez


You can just call the function femaleRadio_CheckedChanged on your constructor. Or you can set the value of the selected index of the radiobutton to trigger the event:

femaleRadio_CheckedChanged(null, null);
or
femaleRadio.SelectedIndex = 0;
like image 37
whastupduck Avatar answered Oct 05 '22 23:10

whastupduck