C# 使用WinForms for TableLayoutPanel在C中创建动态控制器名称
在开发一个从SQL Server数据库中提取测试用例、运行和结果的小应用程序时,我在尝试在WinForms的TableLayoutPanel中创建动态控制器名称的方法中遇到了一个难题。当用户选择特定的测试用例时,我将动态创建行,从那里TableLayoutPanel将打开另一个窗口,其中预加载了测试步骤和两个单选按钮,以指示测试是否通过。我的问题是,当我选择步骤右侧的一个单选按钮时,每次都会得到相同的控制台读数。我需要能够确定用户按下了哪一个单选按钮,这样我就可以确定它在哪一行,以及随后哪个测试通过或失败。我的主要代码如下: 添加到TableLayoutPanel时的FormManualTest.cs部分 或者类似的东西,在循环过程中动态创建控件的名称,然后每个控件将是一个完全独立的控件,我可以从那里开始。任何帮助都将不胜感激!我来自一个很重的网络背景,所以我在JS领域解决这个问题的常规技能现在还不能派上用场。再次感谢您的帮助 属性是可选的,不需要指定,也不需要唯一。您可以将该属性用于自己的目的,您可以在其中指定某个对象的ID或事件实例C# 使用WinForms for TableLayoutPanel在C中创建动态控制器名称,c#,.net,winforms,visual-studio,C#,.net,Winforms,Visual Studio,在开发一个从SQL Server数据库中提取测试用例、运行和结果的小应用程序时,我在尝试在WinForms的TableLayoutPanel中创建动态控制器名称的方法中遇到了一个难题。当用户选择特定的测试用例时,我将动态创建行,从那里TableLayoutPanel将打开另一个窗口,其中预加载了测试步骤和两个单选按钮,以指示测试是否通过。我的问题是,当我选择步骤右侧的一个单选按钮时,每次都会得到相同的控制台读数。我需要能够确定用户按下了哪一个单选按钮,这样我就可以确定它在哪一行,以及随后哪个测试
但是,您也可以创建自己的控件/usercontrol来封装整行,并且您可以为自己的目的声明自己的属性。谢谢您的评论!我在第一列中使用了一个带有步骤标签的自定义控制器,最后用您的建议封装了整行。第一次就很有魅力。再次感谢!
private void addRowToolStripMenuItem_Click(object sender, EventArgs anotherEvent)
{
tableLayoutTest.RowStyles.Clear(); // Clear row styles to ensure a clean start when adding to the TableLayoutPanel
List<RadioButton> listOfRadioControls = new List<RadioButton>(); // Create array of radio buttons
List<UserCustomStep> listOfStepControls = new List<UserCustomStep>(); // Create array of custom controls
for (int i = 0; i < 5; i++)
{
UserCustomStep step = new UserCustomStep(Counter, "Step: " + i + " Push the button to elicit a response."); // Creates new user custom step control instance
RadioButton pass = new RadioButton();
pass.Text = "Pass";
pass.AutoSize = true;
RadioButton fail = new RadioButton();
fail.Text = "Fail";
fail.AutoSize = true;
fail.Margin = new Padding(3,3,20,3); // Needed to see the fail button without having to scroll over
listOfStepControls.Add(step); // Add step to UserCustomStep array
listOfRadioControls.Add(pass); // Add radio buttons to the RadioButton array
listOfRadioControls.Add(fail);
listOfRadioControls[i * 2].CheckedChanged += (s, e) => // Subscribes the pass radio button to listen for when a user has clicked on it
{
Console.WriteLine("Pass " + i + " was clicked");
};
listOfRadioControls[(i * 2) + 1].CheckedChanged += (s, e) => // Subscribes the fail radio button to listen for when a user has clicked on it
{
Console.WriteLine("Fail " + i + " was clicked");
};
tableLayoutTest.Controls.Add(listOfStepControls[i], 0, i); // Adds CustomStep to first column
tableLayoutTest.Controls.Add(listOfRadioControls[i*2], 1, i); // Adds Pass Radio Button to second column
tableLayoutTest.Controls.Add(listOfRadioControls[(i * 2) + 1], 2, i); // Add Fail Raido Button to third column
Counter++; // Increment couter to add subsequent steps underneath the previous ones.
}
}
RadioButton (pass+id) = new RadioButton();