【发布时间】:2014-12-14 06:35:44
【问题描述】:
我有一个类可以构造一种特殊的消息框。 在该类中,参数之一是我在构造函数中引用的变量 但是,我需要从构造函数中识别出该变量,更具体地说,当我单击按钮时。
我查看了方法的行为,并注意到为了让方法从变量中识别值,我必须像这样引用它。
static void SetString1(ref string value)
{
if (value == "cat") // Test parameter value
{
Console.WriteLine("Is cat");
}
value = "dog"; // Assign parameter to new value
}
我也想做同样的事情,但是在按钮单击方法中,但是如果我尝试引用变量“variavelcaixa”,它会给我“buttonRight_click 匹配委托 System.eventhandler 没有重载”。这是什么意思,我应该如何成功引用变量?
private void buttonRight_Click(object sender, System.EventArgs e, ref int variavelcaixa)
{
if (checkBox1.Checked == true)
{ variavelcaixa = 1; }
else { variavelcaixa = 0; }
}
编辑: 我在特殊消息框类中的代码如下:
public partial class BetterDialog : Form
{
public int variavelcaixa;
static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation,
string leftButton, string rightButton, Image iconSet, ref int variavelcaixa)
{
using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton,
rightButton, iconSet, ref variavelcaixa))
{
DialogResult result = dialog.ShowDialog();
return result;
}
}
/// <summary>
/// The private constructor. This is only called by the static method ShowDialog.
/// </summary>
private BetterDialog(string title, string largeHeading, string smallExplanation,
string leftButton, string rightButton, Image iconSet, ref int variavelcaixa)
{
this.Font = SystemFonts.MessageBoxFont;
this.ForeColor = SystemColors.WindowText;
InitializeComponent();
//A bunch of graphic design
}
在构造函数之外还有按钮点击方法
private void buttonRight_Click(object sender, System.EventArgs e)
{
if (checkBox1.Checked == true)
{ variavelcaixa = 1; }
else { variavelcaixa = 0; }
}
在主类中,我只需添加 ref variavelcaixa,并在消息框对象上附加一个特定变量
MsgBoxCheck.MessageBox dlg = new MsgBoxCheck.MessageBox();
string icone = "C:\\warning.png";
DialogResult result = BetterDialog.ShowDialog("Alert",
"main message in message box",
"some secondary message",
"", "Ok", System.Drawing.Bitmap.FromFile(icone), ref Variables.checkbox53naomostrarnovamente);
【问题讨论】:
-
即使您作为
ref传入,您实际上也没有分配给变量。在参数和变量中具有相同名称不会自动赋值。 -
@shree.pat18 那么如何链接它们呢?
-
赋值给
ShowDialog中的变量! -
但是 ShowDialog 已经有 ref int variavelcaixa 作为参数
-
@shree.pat18 你能提供更详细的信息吗?没听懂,抱歉:\
标签: c# methods buttonclick ref