【问题标题】:What is the correct way to get strings from the Main Form in another form? C#从主窗体以另一种形式获取字符串的正确方法是什么? C#
【发布时间】:2015-07-10 03:05:51
【问题描述】:

我在将字符串从主窗体传递到另一个名为 unlockForm 的窗体时遇到问题。

在我的 mainForm 中,我像这样创建每个字符串

public string race
    {
        get;set;
    }

我一直在尝试从 unlockForm 访问它们,但是像这样创建了一个新的 mainForm

mainForm mainScreen = new mainForm();
unlockRace = mainform.race;

在第一行给我一个 StackOverflowException 是未处理的错误。

我在主窗体中创建新窗体时没有遇到这个问题,所以我想知道这样做的正确方法是什么。

编辑:

这是@deathismyfriend 要求的完整代码

这是 mainForm 构造函数

public mainForm()
    {
        InitializeComponent();
    }

这是 mainForm 中更新比赛字符串的代码。

public string race
    {
        get;set;
    }


private void raceUpdate(object sender, EventArgs e)
    {

        if (raceBox.Text == "Human")
        {
            if (infoText != humanText)
            {
                infoText = humanText;

                infoboxUpdate(sender, e);
            }
        }
        else if (raceBox.Text == "Troll")
        {
            if (infoText != trollText)
            {
                infoText = trollText;

                infoboxUpdate(sender, e);
            }
        }

        race = raceBox.Text;
        if (race == "") 
        {
            race = "Unspecified";
        }
    }

这是我的 unlockForm 中的代码

public unlockForm()
    {
        InitializeComponent();
        getStats();
    }

    mainForm mainScreen = new mainForm();

    private void getStats()
    {
        race = mainScreen.race;
    }

编辑#2:

即使我为 unlockForm 编写代码如下

public unlockForm()
    {
        InitializeComponent();
        //getStats();
    }

    mainForm mainScreen = new mainForm();

我仍然收到错误

【问题讨论】:

  • 在调用它之前你从来没有设置种族。
  • 即使我删除了调用竞赛,我仍然收到错误
  • 调用前需要设置mainform.race。
  • @deathismyfriend 我有,我已经从代码中完全删除了unlockRace = mainForm.race;。仅使用mainForm mainScreen = new mainForm(); 会导致错误。
  • 你能发布实际代码吗?还要发布 mainform 构造函数。

标签: c# forms winforms


【解决方案1】:

有两种方法 1:

UnlockForm.cs

private string _race;
public UnlockForm(string race)
{
 _race = race;
}

MainForm.cs

private void LuanchUnlockForm()
{
 var unlockForm = new UnlockForm("Human");
 unlockForm.ShowDialog();
}

第二种方式:

UnlockForm.cs

private MainForm _mainForm;
public UnlockForm(MainForm mainForm)
{
 _mainForm= mainForm;
}
private void GetRace()
{
 var myRace = _mainForm.race;
}

MainForm.cs

private void LuanchUnlockForm()
{
 var unlockForm = new UnlockForm(this);
 unlockForm.ShowDialog();
}

如果要发送多个字符串,请执行以下操作

像 as 一样创建新类

Human.cs

public class Human
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Address { get; set; }
            // or anything you want
        }

现在 在UnlockForm.cs

private Human _human;
public UnlockForm(Human human)
{
  _human= human;
}
private void GetHumanAttributes()
{
  var age = _human.Age; 
  //and others ...
}

MainForm.cs

private void LuanchUnlockForm()
{
 var human = new Human();
 human.Name = "name";
 human.Age = 19;
 // others
 var unlockForm = new UnlockForm(human);
 unlockForm.ShowDialog();
}

【讨论】:

  • 进行了一些修改,但我终于得到了这个工作!非常感谢所有的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
相关资源
最近更新 更多