C# 如何在函数中使用MessageBox
我试图让函数接受两个字符串并显示消息框:C# 如何在函数中使用MessageBox,c#,C#,我试图让函数接受两个字符串并显示消息框: public void Myfunction (string str1, string str2) { MessageBox.Show("your message was " + str1 + Environment.NewLine + str2); } private void Btn_Display_Click(object sender, RoutedEventArgs e) { st
public void Myfunction (string str1, string str2)
{
MessageBox.Show("your message was " + str1 + Environment.NewLine + str2);
}
private void Btn_Display_Click(object sender, RoutedEventArgs e)
{
string today;
today = DateTime.Now.ToString("dd/MM/yyyy");
myfunction(TextBox_Msg.Text, today);
}
现在还不清楚发生了什么,比如说,
myfunction
应该做什么?函数计算值。
我建议一次性显示信息:
如果您坚持提取方法,那么让我们首先将其重命名-ShowMessageLines
:
// Let's show arbitrary many lines (whe only 2?)
// Let's accept all types (not necessary strings)
public static void ShowMessageLines(params object[] lines) {
// public method - input arguments validation
if (null == lines)
throw new ArgumentNullException(nameof(lines));
// Combine all lines into textToShow
// First line must have a prefix - "your message was "
string textToShow = string.Join(Environment.NewLine, lines
.Select((text, index) => index == 0
? $"your message was {text}"
: text?.ToString()));
MessageBox.Show(textToShow);
}
...
private void Btn_Display_Click(object sender, RoutedEventArgs e) {
ShowMessageLines(
TextBox_Msg.Text,
DateTime.Now.ToString("dd/MM/yyyy"));
}
MessageBox.Show(string.Join(Environment.NewLine,$“您的消息是{TextBox\u Msg.Text})”,DateTime.Now.ToString(“dd/MM/yyyy”)代码>此问题不包含文本,仅包含源代码。有什么不起作用吗?除了卷曲的妄想症,一切看起来都很好。
// Let's show arbitrary many lines (whe only 2?)
// Let's accept all types (not necessary strings)
public static void ShowMessageLines(params object[] lines) {
// public method - input arguments validation
if (null == lines)
throw new ArgumentNullException(nameof(lines));
// Combine all lines into textToShow
// First line must have a prefix - "your message was "
string textToShow = string.Join(Environment.NewLine, lines
.Select((text, index) => index == 0
? $"your message was {text}"
: text?.ToString()));
MessageBox.Show(textToShow);
}
...
private void Btn_Display_Click(object sender, RoutedEventArgs e) {
ShowMessageLines(
TextBox_Msg.Text,
DateTime.Now.ToString("dd/MM/yyyy"));
}