将inputForm数据传递回mainForm [英] Passing inputForm data back to mainForm

查看:68
本文介绍了将inputForm数据传递回mainForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.net和c#编程的新手。我需要从用户那里得到一个字符串输入,并发现.net c#并不是很容易支持这个(就像VB一样)。经过多次搜索后,我建议我只需创建一个表单并将其用作输入对话框,这样我就完成了。我无法从我的mainForm(调用表单)访问第二种形式(inputForm)中的文本框字符串。





这是我的mainForm中的代码,它从我的mainForm上的菜单选项创建inputForm(称为GetUserInput):



I'm new to .net and c# programming. I needed to get a string input from the user and found out that .net c# doesn't really support this very easily (like VB does). After much searching around it was suggested that I simply create a form and use it like an input dialog, so I have done that. I can't access the text box string in the second form (the inputForm) from my mainForm (the calling form).


This is my code in my mainForm that creates the inputForm (called GetUserInput) from a menu selection on my mainForm:

private void newMenuItem_Click(object sender, EventArgs e)
{
  GetUserInput f = new GetUserInput();
  f.Show();
  MessageBox.Show(str_input, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
<snip rest of code>
}





这是第二种形式(GetUserInput表格)。我正在尝试使用名为str_input的公共字符串来保存文本框数据。但是,在形式一,mainForm,我无法访问str_input。



任何帮助表示赞赏...





Here is the second form (the GetUserInput form). I'm trying to use a public string called str_input to hold the text box data. However, in form one, the mainForm, I cannot access str_input.

Any help appreciated...

public partial class GetUserInput : Form
{
        public string str_input = "";

        public GetUserInput(string str_input)
        {
            InitializeComponent();
        }

        public GetUserInput()
        {
            // TODO: Complete member initialization
        }

        private void GetUserInput_Load(object sender, EventArgs e)
        {
            txtBoxGetUserInput.Focus();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            str_input = txtBoxGetUserInput.Text;
            Close();
        }

        public string getUserInutString { get; set; }
}

推荐答案

你很接近,而不是:



You are close, instead of:

MessageBox.Show(str_input, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);





使用





use

MessageBox.Show(f.str_input, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);





注意f。,它引用f形式和。是成员访问令牌,它允许您访问f表单的公共成员(或任何类型的公共成员)。



Notice the "f.", which references the form "f" and the . is the member access token, which allows you to access public members of the "f" form (or any type for that matter).


您想要调用ShowDialog()。这将停止主窗体中的代码执行,直到GetUserInput窗体关闭。如果只调用Show(),则会显示GetUserInput表单,但在GetUserInput表单中输入任何内容之前,代码将继续在主表单中执行。此外,正如罗恩所说,你需要通过GetUserInput来引用你的属性,而不是像你那样。你不应该像你一样使用公共变量,创建一个映射到私有变量的属性。试试这个...



在你的newMenuItem_Click事件中这样做......

You want to call the ShowDialog(). This will stop the code execution in the main form until the GetUserInput form in closed. If you just call Show() the GetUserInput form is shown but code continues executing in the main form before anything is entered in GetUserInput form. Also, as Ron said, you need to reference your property via GetUserInput from and not like you did. And you shouldn't use public variables like you did, make a property that maps to a private variable. Try this...

In your newMenuItem_Click event do this...
private void newMenuItem_Click(object sender, EventArgs e)
        {
            GetUserInput f = new GetUserInput();
            f.ShowDialog(); // shows as a modal window; doesn't continue executing this code until GetUserInput form is closed.
            MessageBox.Show(f.UserInput);
            // dispose GetUserInput form after use;
            f.Dispose();
            f = null;
        }



在你的GetUserInput表格中,这样做......


And in your GetUserInput form, do this...

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

    string _userInput = string.Empty;
    public string UserInput
    {
        get { return _userInput; }
        set { _userInput = value; }
    }

    private void GetUserInput_Load(object sender, EventArgs e)
    {
        txtBoxGetUserInput.Focus();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        _userInput = txtBoxGetUserInput.Text;
        this.Close();
    }
}


@RLH



谢谢您的帮助。现在我唯一的问题是:下面的设定线做了什么?我会保留那条线,但我没有设置它(是吗?)。



@RLH

Thanks for the help. Now my only question is: what does the set line do below? I will keep the line there but I'm not setting it (am I?).

public string UserInput
{
    get { return _userInput; }
    set { _userInput = value; }
}


这篇关于将inputForm数据传递回mainForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆