如何将Form2中输入的字符串保存到Form1中

本文关键字:字符串 保存 Form1 输入 Form2 | 更新日期: 2023-09-27 18:09:52

我从注册表中获得Steam安装的默认路径。但是如果有人将他们的游戏安装到不同的文件夹,用户就必须在Configure表单中输入它。当窗体关闭时,输入的路径(从文件夹浏览器或手动输入路径)应该被保存到主窗体中的字符串中,并且应该启用不同的组合框来打开不同的按钮。我以某种方式设法将保存到mainform字符串,但第二个组合框似乎没有打开。我怎样才能做得正确?

** 主表单**

    public string NewPath { get; set; }
    private ConfigForm otherForm;
    string InstallPath = (string)Registry.GetValue(@"HKEY_CURRENT_USER'SOFTWARE'Valve'Steam", "SteamPath", null);
    private void PortalHammerButton_Click(object sender, EventArgs e)
    {
        Process.Start(InstallPath + @"'SteamApps'common'Portal'bin'hammer.exe");
    }
    private void Gamedropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Gamedropdown.Text == "Portal") // When Portal is selected
        {
            // Enable the Portal SDK buttons
            PortalHammerButton.Visible = true;
            PortalModelViewerButton.Visible = true;
            PortalFacePoserButton.Visible = true;
            // Disable the CS:GO SDK buttons
            csgoFacePoserButton.Visible = false;
            csgoHammerButton.Visible = false;
            csgoModelViewerButton.Visible = false;
        }
        else if (Gamedropdown.Text == "CS:GO") // When CS:GO is selected
        {
            // Disable Portal SDK buttons
            PortalHammerButton.Visible = false;
            PortalModelViewerButton.Visible = false;
            PortalFacePoserButton.Visible = false;
            // Enable CS:GO SDK buttons
            csgoFacePoserButton.Visible = true;
            csgoHammerButton.Visible = true;
            csgoModelViewerButton.Visible = true;
        }
    }
    private void ConfigureButton_Click(object sender, EventArgs e)
    {
        var configdialog = new ConfigForm();
        configdialog.Show();
    }
    private void PortalDifferentHammerButton_Click(object sender, EventArgs e)
    {
        Process.Start(NewPath + @"'SteamApps'common'Portal'bin'hammer.exe");
    }
    private void NewDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (NewDropDown.Text == "Portal") // When Portal is selected
        {
            // Enable the Portal SDK buttons
            PortalDifferentHammerButton.Visible = true;
            PortalDifferentModelViewerButton.Visible = true;
            PortalDifferentFacePoserButton.Visible = true;
            // Disable the CS:GO SDK buttons
            DifferentCSGOFaceposerButton.Visible = false;
            DifferentCSGOHammerButton.Visible = false;
            DifferentCSGOModelViewerButton.Visible = false;
        }
        else if (NewDropDown.Text == "CS:GO") // When CS:GO is selected
        {
            // Disable the Portal SDK buttons
            PortalDifferentFacePoserButton.Visible = false;
            PortalDifferentHammerButton.Visible = false;
            PortalDifferentModelViewerButton.Visible = false;
            // Enable the CS:GO SDK buttons
            DifferentCSGOModelViewerButton.Visible = true;
            DifferentCSGOHammerButton.Visible = true;
            DifferentCSGOFaceposerButton.Visible = true;
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
        ConfigForm cfgfrm = new ConfigForm();
        cfgfrm.Close();
    }
}

}

* *配置形式* *

public partial class ConfigForm : Form
{
    public ConfigForm()
    {
        InitializeComponent();
        Form1 frm1 = new Form1();
        frm1.NewPath = NewPathBox.Text;
    }
    public void DifferentFolderBrowseButton_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        DialogResult result = fbd.ShowDialog();
        string newpath = fbd.SelectedPath;
        NewPathBox.Text = newpath;
        Form1 frm1 = new Form1();
        frm1.NewPath = NewPathBox.Text;
    }
    public void CloseButton_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form1 frm1 = new Form1();
        frm1.Gamedropdown.Visible = false;
        frm1.NewDropDown.Visible = true;
    }
}

}

如何将Form2中输入的字符串保存到Form1中

查看您的ConfigForm。这是你的问题:

public ConfigForm()
{
    InitializeComponent();
    Form1 frm1 = new Form1();
    frm1.NewPath = NewPathBox.Text;
}

你在Form1(我猜是你的主表单)上做的是创建一个新的ConfigForm实例并显示它。你在ConfigForm中所做的是创建一个新的主表单,并将NewPath =设置为在配置表单中输入的值。问题是这个新的Form1不是创建ConfigForm的Form1。创建配置表单的Form1不是由代码更新的表单,您创建的一些任意新Form1是更新的表单。这就是为什么你的代码没有像你期望的那样工作。

这是我要采取的方法。在ConfigForm中添加一个NewPath变量,就像在Form1中一样。然后在FormConfig中添加FormClosing方法。像这样做:

private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
{
     NewPath = NewPathBox.Text;
}
然后,将Form1上的代码更改为:
private void button1_Click(object sender, EventArgs e)
{
    ConfigForm cfgfrm = new ConfigForm();
    cfgfrm.ShowDialog();
    this.NewPath = cfgfrm.NewPath;
}

这段代码所做的是创建和显示一个新的ConfigForm在你的Form1当你点击button1。然后,当用户关闭FormConfig时,表单将文本框的值保存到FormConfig的NewPath变量中。然后,表单关闭后,Form1上的代码继续运行。然后,Form1查看用户关闭FormConfig时保存的NewPath值。Form1获取这个新的NewPath值,并将其放入自己的NewPath变量中。

编辑

显示/隐藏组合框:

private void button1_Click(object sender, EventArgs e)
{
    ConfigForm cfgfrm = new ConfigForm();
    cfgfrm.ShowDialog();
    this.NewPath = cfgfrm.NewPath;
    Gamedropdown.Visible = false; 
    NewDropDown.Visible = true
}

我不知道你为什么把二级表单弄得这么复杂。在使用它之后,你不需要指向它的指针,你应该关闭它,而不是隐藏它。试试这个:

class ConfigForm : Form 
{
    public string newPath = null;
    public void CloseButton_Click(object sender, EventArgs e)
    {
        newPath = NewPathBox.Text;
    }
    public void CloseButton_Click(object sender, EventArgs e)
    {
        Close();
    }
}

…在主表单中:

public partial class Form1 : Form
{
    string steamPath = null; // set to starting path
    private void ConfigureButton_Click(object sender, EventArgs e)
    {
        bool valueChanged = false;
        using (ConfigForm form = new ConfigForm())
        {
            form.newPath = null;
            form.ShowDialog();
            if (form.newPath != null)
            {
                steamPath = form.newPath;
                valueChanged = true;
            }
        }
        if (valueChanged)
        {
            // here is where you would handle reloading and changing the ComboBoxes
        }
    }
}

这将更清楚地返回新字符串。无论你想做什么作为改变路径的结果,可以在你处置配置表单后完成(带"using"条件会自动为你做处置)