Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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>
}


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...

C#
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; }
}
Posted

You are close, instead of:

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


use

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


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).
 
Share this answer
 
Comments
rfresh 3-Sep-13 1:19am    
Thank you Ron.
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...
C#
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;
        }

And in your GetUserInput form, do this...
C#
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();
    }
}
 
Share this answer
 
v2
@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?).

C#
public string UserInput
{
    get { return _userInput; }
    set { _userInput = value; }
}
 
Share this answer
 
Comments
idenizeni 3-Sep-13 2:11am    
In your code, no you are not using this as you are setting the value in the GetUserInput form via the private variable. If you only want the UserInput value to be readonly you could delete the set line. However, if you wanted to set the value from the main form when you create and show the GetUserInput form you could use it like so...

GetUserInput f = new GetUserInput();
f.UserInput = "inital value";
f.ShowDialog();
MessageBox.Show(f.UserInput);

... of course you would also have to add code in the GetUserInput form so when it loads the form the textbox.text property is set to the UserInput value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900