我有一个面板,其中包含多个动态添加的用户控件(全部相同),我需要查看是否选中了一个复选框,以及每个控件中的label.text是什么。不确定如何获得这些值?
这是将用户控件添加到面板的地方
private void Employee_Add_Load(object sender, EventArgs e)
{
List<string> VehicleTypes = DAL.LicenseDAL.GetAllVehicleTypes();
foreach (string Vehicle in VehicleTypes)
{
User_Controls.Vehicle_Bar VTB = new Vehicle_Bar(Vehicle);
pnlVehicleChecks.Controls.Add(VTB);
}
}
这是用户控件(实际上只是一个标签和复选框)
public partial class Vehicle_Bar : UserControl
{
public string Vehicle = "";
public Vehicle_Bar(string vehicle)
{
Vehicle = vehicle;
InitializeComponent();
}
private void Vehicle_Bar_Load(object sender, EventArgs e)
{
lblType.Text = Vehicle;
}
}
所有这些复选框均适用于员工的车辆执照,具体取决于选中的复选框取决于允许驾驶的车辆
答案 0 :(得分:1)
您可以处理UserControl中复选框数据的检索并将其存储在可公开访问的数据对象中
public class VehicleInfo
{
public bool allowdrive {get; set; }
public string vehicle {get; set; }
}
public partial class Vehicle_Bar : UserControl
{
public VehicleInfo vehicleInfo;
public Vehicle_Bar(string vehicle)
{
vehicleInfo = new VehicleInfo(){
vehicle = vehicle,
allowdrive = false
};
InitializeComponent();
}
private void Vehicle_Bar_Load(object sender, EventArgs e)
{
lblType.Text = vehicleInfo.vehicle;
}
//Handle checkbox click event to set the value of "allowdrive"
}
字符串和复选框的组合数据存储在vehicleInfo对象中,可以使用UserControl Vehicle_Bar的实例进行访问
示例:
Console.WriteLine(VTB.vehicleInfo.allowdrive);