Click here to Skip to main content
15,907,396 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code to my application. I made the radio buttons part of the Employee class so that they are linked and when selected, the Employee attribues are available for modification purposes (also I plan to implement a lightweight db for persistence eventually) but I'm missing some logic.

I can get the radio buttons to populate the groupbox, and I have an event handler that populates the textboxes with the Employee's name and function when a Radio Button is clicked, but it always selects the last Employee created. I'm not sure how to navigate through the List<employee> to make sure the Radio Button clicked is associated to the Employee object.

Any help is appreciated. Thanks.

C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace TaskManager.Forms {
    public partial class EmployeeForm : Form {

        int _id;
        int _y = -5;
        Employee _employee;
        List<Employee> emps = new List<Employee>();

        public EmployeeForm() {
            InitializeComponent();
        }
        public void EmpExitButton_Click(object sender, EventArgs e) {
            Close();
        }
        public void EmployeesGroupBox_Enter(object sender, EventArgs e) {

        }
        public void EmpAddButton_Click(object sender, EventArgs e)
        {
            _id += 1;
            _y += 20;

            _employee = new Employee(FNameBox.Text, LNameBox.Text, FunctionBox.Text);
            _employee.Id = _id;
            _employee.Rb.Click += new EventHandler(_rB_Click);
            _employee.Rb.Width = 185;
            _employee.Rb.Text = FNameBox.Text + " " + LNameBox.Text + ": " + FunctionBox.Text;
            _employee.Rb.Tag = _employee.Id;
            EmployeesGroupBox.Controls.Add(_employee.Rb);
            _employee.Rb.Location = new Point(5, _y);

            emps.Add(_employee);

            FNameBox.Clear();
            LNameBox.Clear();
            FunctionBox.Clear();
        }
        public void _rB_Click(object sender, EventArgs e) {

            //want to find Employee associated to clicked RadioButton            
            
            int x = 1;
            switch (_employee.Id)
            {
                case 1:
                    x = 1;
                    break;
                case 2:
                    x = 2;
                    break;
                case 3:
                    x = 3;
                    break;
                case 4:
                    x = 4;
                    break;
                default:
                    FNameBox.Text = "Unknown Employee";
                    LNameBox.Text = "Unknown Employee";
                    FunctionBox.Text = "Unknown Employee";
                    break;
            }
            
           //this causes an argument out of range exception. Not sure why.

            FNameBox.Text = emps[x].FName;
            LNameBox.Text = emps[x].LName;
            FunctionBox.Text = emps[x].Function;

            foreach(var Employee in emps) {
                _employee.Rb.Checked = false;
            }
        }
        public void EmpEditButton_Click(object sender, EventArgs e) {

        }
        public void FNameBox_TextChanged(object sender, EventArgs e) {
        }
        public void LNameBox_TextChanged(object sender, EventArgs e) {
        }
        public void FunctionBox_TextChanged(object sender, EventArgs e) {
        }
        private void EmpDeleteButton_Click(object sender, EventArgs e) {

        }
        private void FieldClearButton_Click(object sender, EventArgs e) {

            FNameBox.Text = "";
            LNameBox.Text = "";
            FunctionBox.Text = "";
        }
        private void EmpSaveButton_Click(object sender, EventArgs e) {

        }
    }
}


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskManager {
    public class Employee {
        public String FName { get; set; }
        public String LName { get; set; }
        public String Function { get; set; }
        public int Id { get; set; }
        public RadioButton Rb { get; set; }

        public Employee ( ) {
            Rb = new RadioButton();
            Rb.Width = 185;
        }

        public Employee(String first, String last, String func) {

            FName = first;
            LName = last;
            Function = func;
            Rb = new RadioButton();
            Rb.Width = 185;
        }
    }
}
Posted
Updated 2-Apr-14 4:14am
v8

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900