You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms如何通过Ref传递值类型变量并修改,无需设为公有

解决方法

你当前的代码没法实现预期效果,问题出在:构造函数里的ref bool accept参数,你只是把它的值复制给了Form2的accept字段,并没有保留引用关联——值类型的ref参数仅在方法执行期间有效,构造函数执行完后,Form2的字段和Form1的局部变量就没关系了,后续修改accept字段不会影响Form1里的delete。

不用把delete设为公有,有几种更合理的实现方式:

方式一:用引用类型包装值类型

自定义一个简单的包装类,把bool值放在里面,这样传递的是引用,修改包装类的属性会直接影响原变量:

// 自定义包装类
public class BoolWrapper
{
    public bool Value { get; set; }
}

// Form1代码修改
private void deleteMem_Click(object sender, EventArgs e)
{
    BoolWrapper deleteWrapper = new BoolWrapper { Value = false };
    choiceCheck choiceCheck = new choiceCheck(deleteWrapper,"Supprimer le membre", "Êtes-vous sûr de vouloir supprimer le membre (" + CDMDisplay.Text + ") " + FNDisplay.Text + " " + LNDisplay.Text, "Supprimer", "Annuler");
    choiceCheck.ShowDialog();

    if (!deleteWrapper.Value) return;
    // 后续逻辑
}

// Form2代码修改
public class choiceCheck : Form
{
    private BoolWrapper _resultWrapper;

    public choiceCheck(BoolWrapper resultWrapper, string title, string message, string yesText, string noText)
    {
        InitializeComponent();
        this.Text = title;
        this.message.Text = message;
        this.yesButton.Text = yesText;
        this.noButton.Text = noText;
        _resultWrapper = resultWrapper;
    }

    private void yesButton_Click(object sender, EventArgs e)
    {
        _resultWrapper.Value = true;
        this.Close();
    }

    private void noButton_Click(object sender, EventArgs e)
    {
        _resultWrapper.Value = false;
        this.Close();
    }
}

方式二:使用委托传递修改逻辑

Form2不需要持有变量引用,而是接收一个可以修改Form1变量的委托,更灵活:

// Form1代码修改
private void deleteMem_Click(object sender, EventArgs e)
{
    bool delete = false;
    choiceCheck choiceCheck = new choiceCheck(val => delete = val,"Supprimer le membre", "Êtes-vous sûr de vouloir supprimer le membre (" + CDMDisplay.Text + ") " + FNDisplay.Text + " " + LNDisplay.Text, "Supprimer", "Annuler");
    choiceCheck.ShowDialog();

    if (!delete) return;
    // 后续逻辑
}

// Form2代码修改
public class choiceCheck : Form
{
    private Action<bool> _setResult;

    public choiceCheck(Action<bool> setResult, string title, string message, string yesText, string noText)
    {
        InitializeComponent();
        this.Text = title;
        this.message.Text = message;
        this.yesButton.Text = yesText;
        this.noButton.Text = noText;
        _setResult = setResult;
    }

    private void yesButton_Click(object sender, EventArgs e)
    {
        _setResult?.Invoke(true);
        this.Close();
    }

    private void noButton_Click(object sender, EventArgs e)
    {
        _setResult?.Invoke(false);
        this.Close();
    }
}

方式三:遵循WinForms标准对话框模式(推荐)

自定义对话框最好和系统MessageBox一样,通过DialogResult返回结果,Form1根据返回值设置变量,这是最符合WinForms设计习惯的方式:

// Form1代码修改
private void deleteMem_Click(object sender, EventArgs e)
{
    choiceCheck choiceCheck = new choiceCheck("Supprimer le membre", "Êtes-vous sûr de vouloir supprimer le membre (" + CDMDisplay.Text + ") " + FNDisplay.Text + " " + LNDisplay.Text, "Supprimer", "Annuler");
    DialogResult result = choiceCheck.ShowDialog();

    if (result != DialogResult.Yes) return;
    // 后续逻辑(这里不需要delete变量,直接根据result判断即可,也可以设置delete = result == DialogResult.Yes;)
}

// Form2代码修改
public class choiceCheck : Form
{
    public choiceCheck(string title, string message, string yesText, string noText)
    {
        InitializeComponent();
        this.Text = title;
        this.message.Text = message;
        // 设置按钮的DialogResult属性,这样点击按钮时会自动设置窗体的DialogResult并关闭
        yesButton.DialogResult = DialogResult.Yes;
        noButton.DialogResult = DialogResult.No;
        yesButton.Text = yesText;
        noButton.Text = noText;
    }
}

这种方式不需要传递任何变量引用,完全遵循WinForms的对话框设计模式,代码更简洁易维护。

内容的提问来源于stack exchange,提问作者Salmy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 00:10:23