在 C# 中使用 ShowDialog() 显示消息

Using ShowDialog() in C# to display a message

我想使用 show dialog() 以另一种形式显示消息。我如何显示消息?这是我试过的。

//return total 
return total; 

//change total to string 
total = total.ToString;

//create an instance of the MessageForm class
MessageForm myMessageForm = new MessageForm();

//Display the form
myMessageForm.ShowDialog(total);

这给了我一条错误信息。我不知道如何以其他形式显示总数。有什么建议吗?

您传递的 total 值应该是这个新表单的所有者。

你可能想要实现这个:

// remove the return line

total = total.ToString();

//create an instance of the MessageForm class
MessageForm myMessageForm = new MessageForm();

// set the total value which is now a property on message form
myMessageForm.Total = total;

//Display the form
myMessageForm.ShowDialog();

您还必须将这个新的 属性 添加到 MessageForm class

// decide the visibility yourself (e.g. public vs internal)
internal string Total { get; set; }

您还应该考虑是否要在构造函数中传递总数而不是 属性。如果您有其他 classes 调用 MessageForm,这可能需要一些重构。