If you are using Windows Forms, consider two strategies:
1. if you only have a few values to get from the user, consider using a Panel on the Form which is hidden (InputPanel.Visible = false) until you want to show it and get the information. Or, you could use a UserControl in the same way.
2. if your design requires you to "go modal" ... to show a pop-up that will block the Application until it is closed ... in C# you need to use a Form. So, create the Form at design time, adding the input fields you need. Then you'll need to find a way to get the user-input data to the running Application when the user clicks the submit/enter Button, or, possibly, when the Form is closed by the user.
2.a. how to do that:
2.a.1 create the input form, 'InputForm
2.a.2. put Close and Submit Buttons on it, 'btnClose, 'btnSubmit
2.a.3 example code: this assumes that you want to know three possible outcomes of the user's actions on the input Form:
a. did the user close the Form, or Cancel ?
b. did the user enter invalid data
c. did the user enter valid data
The data input Form:"
using System;
using System.Windows.Forms;
namespace YourNameSpace
{
public partial class InputForm : Form
{
public InputForm()
{
InitializeComponent();
}
public Action<bool?, int> ReturnInputFormData { set; get; }
private bool hasData = false;
public void ValidateAndReturnData()
{
if (ReturnInputFormData == null) return;
if (hasData)
{
int data = 0;
bool dataOkay = Int32.TryParse(inputDataTextBox.Text, out data);
ReturnInputFormData(dataOkay, data);
}
else
{
ReturnInputFormData(null, 0);
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
hasData = inputDataTextBox.Text != string.Empty;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
hasData = false;
this.Close();
}
private void InputForm_FormClosing(object sender, FormClosingEventArgs e)
{
ValidateAndReturnData();
}
}
}
Example of how this is used on a Main Form in a Button Click EventHandler:
private void GetUserData_Click(object sender, EventArgs e)
{
InputForm inForm = new InputForm();
inForm.ReturnInputFormData = ReturnInputFormData;
inForm.Show();
}
private void ReturnInputFormData(bool? userEntryResult, int i)
{
switch (userEntryResult)
{
case null:
Console.WriteLine("user closed form, or clicked Cancel");
break;
case true:
Console.WriteLine("user entry valid: {0}", i);
break;
case false:
Console.WriteLine("user entered invalid value");
break;
default:
break;
}
}
Note:
1. we used a nullable bool return parameter to distinguish between no action or empty data on Form closing from the user clicking submit.
... to keep this sample code short ... we omitted doing some things you might want to handle in production code ...
2. this code does not detect/report something like the user entering some data, perhaps valid data, and then closing the Form by clicking on the cancel button, or closing the window. in some cases (naive users ?), you might want to handle that and put a message up to help the user.
3. this code does not specifically handle, as a separate case, the user entering nothing, or clearing the text input field, and then clicking the submit button. My preference is to make a submit button disabled by default, and only make it enabled when there is valid data in the input field.