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

C#报错:无对应所需形参的实参,新手跨窗体传值求助

Fixing "No argument given for required formal parameter" Error in C# WinForms

Hey there! Let's work through this error together—it's a super common hiccup when you're just getting started with passing data between WinForms, so don't stress.

The error you're seeing means you're calling a method (or constructor) that expects one or more parameters, but you aren't providing them. Let's walk through two solid ways to pass the selected gender from your CharacterCreation form to the MainGame form, and fix that error along the way.

Option 1: Pass Data via the Main Game Form's Constructor

This is a clean approach if you want the gender to be set as soon as the main form loads.

Step 1: Update the MainGameForm Constructor

First, modify your MainGameForm to accept a string parameter for the gender, then assign it to your Label:

// Inside MainGameForm.cs
public partial class MainGameForm : Form
{
    // Add a constructor that takes the gender as a parameter
    public MainGameForm(string playerGender)
    {
        InitializeComponent();
        // Assign the passed gender to your Label control
        lblPlayerGender.Text = playerGender;
    }
}

Step 2: Pass the Gender from CharacterCreation Form

In your CharacterCreation form, when the user is ready to move to the main game (like clicking a "Start Game" button), grab the selected radio button's text and pass it when you create the MainGameForm instance:

// Inside CharacterCreation.cs
private void btnStartGame_Click(object sender, EventArgs e)
{
    // Get the selected gender text
    string selectedGender = string.Empty;
    if (rbMale.Checked)
    {
        selectedGender = rbMale.Text;
    }
    else if (rbFemale.Checked)
    {
        selectedGender = rbFemale.Text;
    }

    // Make sure to pass the gender parameter here—this fixes your error!
    MainGameForm mainGameForm = new MainGameForm(selectedGender);
    mainGameForm.Show();
    this.Hide(); // Or use this.Close() if you don't need the creation form anymore
}

Option 2: Use a Public Property or Method

If you don't want to modify the main form's constructor, you can add a public property or method to set the Label text after the form is created.

Step 1: Add a Public Property/Method to MainGameForm

Choose either a property or a method—both work well:

// Inside MainGameForm.cs
public partial class MainGameForm : Form
{
    // Option A: Public property
    public string PlayerGender
    {
        set { lblPlayerGender.Text = value; }
    }

    // Option B: Public method
    public void SetPlayerGender(string gender)
    {
        lblPlayerGender.Text = gender;
    }
}

Step 2: Set the Value from CharacterCreation Form

Again, in your button click event, grab the selected gender, create the main form, then set the property or call the method:

// Inside CharacterCreation.cs
private void btnStartGame_Click(object sender, EventArgs e)
{
    string selectedGender = string.Empty;
    if (rbMale.Checked)
    {
        selectedGender = rbMale.Text;
    }
    else if (rbFemale.Checked)
    {
        selectedGender = rbFemale.Text;
    }

    MainGameForm mainGameForm = new MainGameForm();
    // Use the property
    mainGameForm.PlayerGender = selectedGender;
    // OR use the method
    // mainGameForm.SetPlayerGender(selectedGender);
    
    mainGameForm.Show();
    this.Hide();
}

Quick Troubleshooting Check

Double-check these things if you still run into issues:

  • Did you forget to pass the selectedGender parameter when creating MainGameForm? That's the most common cause of your error.
  • Is your Label control in MainGameForm named correctly (like lblPlayerGender)? Typos here can cause hidden issues.
  • Add a quick check to make sure the user selected a gender—you can show a message box if neither radio button is checked to avoid empty text.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:20:34