通过RadioButton单击获取对象值 [英] Get object values through RadioButton click
问题描述
这是我的应用程序的代码。我将单选按钮作为 Employee类的一部分,以便它们被链接,当选择时, Employee attribues可用于修改目的(我也计划实现轻量级数据库)最终持久性)但我错过了一些逻辑。
我可以通过单选按钮来填充组合框,我有一个事件处理程序,用于填充文本框单击单选按钮时,员工的姓名和功能,但始终选择最后创建的员工。我不确定如何浏览List< employee>确保单击的单选按钮与Employee对象关联。
任何帮助表示赞赏。谢谢。
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.
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) {
}
}
}
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;
}
}
}
推荐答案
看看这个..
http:// stackoverflow.com/questions/1958575/adding-new-eventhandler-in-radiobutton-checkedchanges-in-dynamic-table [ ^ ]
look at this..
http://stackoverflow.com/questions/1958575/adding-new-eventhandler-in-radiobutton-checkedchanges-in-dynamic-table[^]
这篇关于通过RadioButton单击获取对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!