在处理从SQL Server数据库中获取测试用例,运行和结果的小应用程序时,我在尝试在WinForms中的TableLayoutPanel中创建动态控制器名称的方法中遇到了两难。我在用户选择特定测试用例时动态创建行,然后TableLayoutPanel将打开另一个窗口,其中预加载测试步骤和两个单选按钮以指示测试是否通过。我的问题是,当我选择步骤右侧的一个单选按钮时,我每次都会读取相同的控制台。我需要能够确定用户按下了哪个确切的单选按钮,因此我可以确定它所在的行以及随后的测试通过或失败。我的主要代码如下:
FormManualTest.cs(添加到TableLayoutPanel时的部分)
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.
}
}
带控制台读数的应用的屏幕截图:
点击测试用例并按下单选按钮后 (从点击这个我希望控制台读取&#34;通过1点击&#34;)
控制台阅读:
点击“失败按钮:” (我从下面的图片中知道,由于Pass按钮没有被点击,我以某种方式为所有5个使用相同的控制器)
控制台阅读
因此,从我提出的所有这些问题中,我知道我以某种方式为所有5个实例使用相同的控制器,而不管我是否将所有内容都存储在控制器阵列并从那里抓取。 for循环必须稍后转换为a for each循环,但这仍然无法解决我的问题。我相信如果我可以这样说:
RadioButton (pass+id) = new RadioButton();
循环时动态创建控件的名称或类似的东西,然后每个控件都是一个完全独立的控件,我可以从那里开始。任何帮助将不胜感激!我来自一个沉重的网络背景,所以我在JS土地上解决这个问题的正常技能现在还没有派上用场。再次感谢您的帮助。