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

如何从Form向User Control传递int值?游戏开发场景求助

Hey there! Let's work through this int value passing problem you're hitting between your game's UserControls when switching layouts. I've got a few tried-and-true solutions that should fit perfectly with your setup:

1. Use Custom EventArgs with EventHandler

The default EventHandler relies on EventArgs, which doesn't carry custom data—so creating your own derived class lets you pass ints (or any other values) easily.

First, define your custom event args class:

public class GameDataEventArgs : EventArgs
{
    public int Difficulty { get; }
    public int GameType { get; }

    public GameDataEventArgs(int difficulty, int gameType)
    {
        Difficulty = difficulty;
        GameType = gameType;
    }
}

Next, in the UserControl that's sending the data, declare the event:

// Declare the event in your source control
public event EventHandler<GameDataEventArgs> GameDataReady;

// When you're ready to switch layouts and pass data
private void TriggerLayoutSwitch()
{
    int selectedDifficulty = 2; // Replace with your actual value
    int selectedGameType = 1; // Replace with your actual value
    
    // Raise the event to pass the data
    GameDataReady?.Invoke(this, new GameDataEventArgs(selectedDifficulty, selectedGameType));
    
    // Now switch your layout (e.g., bring the target UserControl to front)
    targetUserControl.BringToFront();
}

In the UserControl that receives the data, subscribe to the event to grab the ints:

// Subscribe to the event when setting up the controls
sourceUserControl.GameDataReady += SourceUserControl_GameDataReady;

private void SourceUserControl_GameDataReady(object sender, GameDataEventArgs e)
{
    // Access the int values here
    int difficulty = e.Difficulty;
    int gameType = e.GameType;
    
    // Run your processing logic with these values
}
2. Expose Public Properties on the Target UserControl

A more straightforward approach for one-off data passing is to add public properties to the receiving UserControl, then set them directly before switching layouts.

In your target UserControl:

// Add public properties to hold the data
public int CurrentDifficulty { get; set; }
public int CurrentGameType { get; set; }

Then, in your main form/container where you manage the layouts:

// Get your target UserControl instance
TargetUserControl targetControl = yourTargetControlInstance;

// Set the properties before switching the layout
targetControl.CurrentDifficulty = 3;
targetControl.CurrentGameType = 2;

// Switch the layout (bring the target control to front)
targetControl.BringToFront();

If you need to trigger logic when these properties are set, you can expand the setter:

private int _currentDifficulty;
public int CurrentDifficulty
{
    get => _currentDifficulty;
    set
    {
        _currentDifficulty = value;
        // Run your setup logic here when the difficulty is set
        InitializeDifficultySpecificElements(value);
    }
}
3. Use a Centralized Data Service

If multiple parts of your game need access to the same data (like difficulty or game type), a singleton data service keeps everything centralized and easy to access.

Create the service:

public class GameDataService
{
    // Singleton instance to ensure only one exists
    private static GameDataService _instance;
    public static GameDataService Instance => _instance ??= new GameDataService();

    // Properties to store your game data
    public int Difficulty { get; set; }
    public int GameType { get; set; }

    // Private constructor to enforce singleton pattern
    private GameDataService() {}
}

In your source control, set the values:

// Update the service with your current data
GameDataService.Instance.Difficulty = 1;
GameDataService.Instance.GameType = 0;

In your target control, retrieve the values whenever you need them:

// Grab the data from the service
int difficulty = GameDataService.Instance.Difficulty;
int gameType = GameDataService.Instance.GameType;

// Use the values for your processing
Choosing the Right Approach
  • Go with custom EventArgs if you want an event-driven, clean separation between controls (great for when you need to pass data exactly when the layout switches)
  • Use public properties for simple, one-off data passing where you directly manage the controls from a container
  • Opt for a centralized data service if multiple components need access to the same game data (scalable and keeps your data organized)

Any of these should solve your problem of passing int values between UserControls when switching layouts!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:09:00