【问题标题】:C# Windows Forms App 'Parameter is not valid.'C# Windows 窗体应用程序“参数无效。”
【发布时间】:2020-11-27 14:12:31
【问题描述】:

在此示例中,我尝试从 Main.cs 传递参数

string head = "Error", msg = "Please chek username and password";

public Main()
{
    InitializeComponent();
}

private void bunifuThinButton4_Click(object sender, EventArgs e)
{
    Message message = new Message(head, msg);
    message.ShowDialog();
}

Message.cs

public Message()
{
    InitializeComponent();
}

public Message(string a, string b)
{
    InitializeComponent();
    bunifuCustomLabel1.Text = a;
    bunifuCustomLabel2.Text = b;
}

但我每次都尝试使用许多不同的示例得到此错误

System.ArgumentException: '参数无效。'

在这条线上

message.ShowDialog();

【问题讨论】:

  • 听不懂你
  • 在您的示例 Message.cs 中不包含名为 ShowDialog() 的方法
  • @Greg,“消息”类派生自 Form 类。所以来自“System.Windows.Forms”类的 ShowDialog()

标签: c# c#-4.0 c#-3.0 windows-forms-designer c#-2.0


【解决方案1】:

请将第二个表单从“消息”重命名为其他名称,例如“Form2”。 System.Windows.Forms.dll 中有一个内置结构消息。 我试着举个例子。希望对你有帮助:

     public partial class Form1 : Form
    {
        string head = "Error", msg = "Please chek username and password";
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 message = new Form2(head, msg);
            message.ShowDialog();
        }
    }

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    public Form2(string a, string b)
    {
        InitializeComponent();
        label1.Text = a;
        label2.Text = b;
    }
}

【讨论】:

  • 这一行被删除了我在这里写代码的时候忘记了
【解决方案2】:

它对我有用,我认为您的代码除了您共享的代码之外还缺少一些东西。好的,我正在分享工作示例。

using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Main : Form
{
    string head = "Error", msg = "Please chek username and password";
    public Main()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Message message = new Message(head, msg);
        message.ShowDialog();
    }

    private void Main_Load(object sender, EventArgs e)
    {

    }
 }
}

留言表格

public partial class Message : Form
{
    public Message()
    {
        InitializeComponent();
    }

    public Message(string a, string b)
    {
        InitializeComponent();
        label1.Text = a;
        label2.Text = b;
    }

    private void Message_Load(object sender, EventArgs e)
    {

    }
}

屏幕截图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2023-02-07
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多