Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为两个复选框设置不同的事件?_C#_Visual Studio 2012 - Fatal编程技术网

C# 如何为两个复选框设置不同的事件?

C# 如何为两个复选框设置不同的事件?,c#,visual-studio-2012,C#,Visual Studio 2012,我有两部分代码: private void Simulink_CheckedChanged_1(object sender, EventArgs e) { string installerfilename = path + "installer.ini"; string installertext = File.ReadAllText(installerfilename); var lin = File.ReadLines(Path.Combine(path, "inst

我有两部分代码:

private void Simulink_CheckedChanged_1(object sender, EventArgs e)
{
    string installerfilename = path + "installer.ini";
    string installertext = File.ReadAllText(installerfilename);
    var lin = File.ReadLines(Path.Combine(path, "installer.ini")).ToArray();

    CheckBox cb = sender as CheckBox;
    if (cb.Checked)
    {
        var product = lin.Select(line => Regex.Replace(line, "product=all", "#product=all"));
        var product_tool = product.Select(line => Regex.Replace(line, "#product=Simulink", "product=Simulink"));
        File.WriteAllLines(installerfilename, product_tool);
    }
    else if (!cb.Checked)
    {
        return;
    }
}

private void  AerospaceBlockset_CheckedChanged(object sender, EventArgs e)
{
    string installerfilename = path + "installer.ini";
    string installertext = File.ReadAllText(installerfilename);
    var lin = File.ReadLines(Path.Combine(path, "installer.ini")).ToArray();

    CheckBox cb1 = sender as CheckBox;
    if ( cb1.Checked )
    {

        var product = lin.Select(line => Regex.Replace(line, "product=all", "#product=all"));
        var product_tool = product.Select(line => Regex.Replace(line, "#product=AerospaceBlockset", "product=AerospaceBlockset"));
        File.WriteAllLines(installerfilename, product_tool);
    }

    else if (!cb1.Checked)
    {
        return;
    }
}
第二个选项与第一个选项相同,换句话说,如果我在installer.ini文件中选中Simulink复选框或AerospaceBlockset复选框或两者,将生成相同的内容:

product=all => #product=all
#product=Simulink=> product=Simulink
要正常工作,需要在instaler.ini文件中显示:

如果选中Simulink复选框并:

product=all => #product=all
    #product=AerospaceBlockset=> product=AerospaceBlockset
如果选中AerospaceBlockset复选框


我怎么能做到

您可以将两个复选框的tag属性设置为所需的字符串,然后更改行

var product_tool = product.Select(line => Regex.Replace(line, "#product=AerospaceBlockset", "product=AerospaceBlockset"));


最后,对两个复选框使用相同的函数。

在窗体的构造函数中,假设使用WinForms,您可以将复选框的事件连接起来,如下所示:

Simulink.CheckedChanged += Simulink_CheckedChanged_1;
AerospaceBlockset.CheckedChanged += AerospaceBlockset_CheckedChanged;
您可能必须删除设计器中的条目,这样就不会调用这些方法两次

这样,复选框将执行各自的事件。由于它们的操作基本相同,您可以考虑将该逻辑提取到另一个方法中,并使用合适的参数调用该方法:

private void Simulink_CheckedChanged_1(object sender, EventArgs e)
{
    ProcessIniFile("Simulink");
}

在ProcessinFile中,您可以执行现在在事件方法中所做的操作,但用传入的参数替换硬编码的值。

因此您有两个复选框和两个checkchanged事件。问题/错误是什么?他将第一个事件链接到两个复选框。您可以更明确地在构造函数中的何处?如果您使用WinForms,这真的不重要-只要是在调用InitializeComponents.ProcessInFile之后,它就是一个方法?
Simulink.CheckedChanged += Simulink_CheckedChanged_1;
AerospaceBlockset.CheckedChanged += AerospaceBlockset_CheckedChanged;
private void Simulink_CheckedChanged_1(object sender, EventArgs e)
{
    ProcessIniFile("Simulink");
}