Reputation: 303
So here is the problem, I want to make kind of a input dialog, that when a button in the main frame is clicked, it triggers the input dialog. And we can type words into the dialog, and then click the "Add" button on the input dialog so that the textbox on the main frame can display the string I just entered. However, I currently facing the difficulty to do this. Because the dialog and the main frame are two modules, which means that when I click the button on main frame to trigger the input dialog, it can't wait till I input the string, and the button event handler finish their job, so it can never read my input. Anyway, here is my code:
/* some code */
// add first name and last name
private void addButton_Click(object sender, EventArgs e)
{
Add add = new Add(); // declare a Add object
add.Visible = true; // show the Add frame
if (add.isOK)
{
firstName = add.getFirstName();
lastName = add.getLastName();
textArea.Text = "First Name: " + firstName + "\r\n"; // textArea is a multiple line textbox
textArea.Text += "Last Name: " + lastName;
}
}
/* some code */
Above is the Add button on the main frame that trigger the Add object (the input dialog). And here is my Add class:
namespace DatabaseToAPI
{
public partial class Add : Form
{
private string firstName;
private string lastName;
public bool isOK = false; // to check if name is provided
public Add()
{
InitializeComponent();
this.Visible = false; // only become visible when the main form's Add button is clicked
}
public string getFirstName()
{
return firstName; // return the first name
}
public string getLastName()
{
return lastName; // return the last name
}
// Add button event handler
private void addButton_Click(object sender, EventArgs e)
{
if (firstNameBox.Text != "" && lastNameBox.Text != "") // if name is provided
{
firstName = firstNameBox.Text;
lastName = lastNameBox.Text;
isOK = true; // name has provided
this.Close();
}
else
{
MessageBox.Show("Please provide both first name and last name", "Sorry", MessageBoxButtons.OK);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
As you can see, the Add class is basically like a input dialog, when the Add button is clicked on the main form, the Add form appears. The only problem is that the event handler for the input dialog trigger button on the main frame can't wait till I enter the name and click the button to signal I finish, it simply just go through the code without waiting the Add class finish their job. How can I do so that the main form knows when do I add the name so that they can read the text I put ?
Upvotes: 0
Views: 212
Reputation: 303
Ok, thanks to the people who answered, I succeeded. We basically just treat the form as a dialog. Here is the code:
// add first name and last name
private void addButton_Click(object sender, EventArgs e)
{
Add add = new Add();
add.ShowDialog(this);
try {
if (add.DialogResult == DialogResult.OK)
{
firstName = add.getFirstName();
lastName = add.getLastName();
textArea.Text = "First Name: " + firstName + "\r\n";
textArea.Text += "Last Name: " + lastName;
}
}
catch
{
}
}
Here is the Add class:
namespace DatabaseToAPI
{
public partial class Add : Form
{
private string firstName;
private string lastName;
public Add()
{
InitializeComponent();
this.Visible = false;
}
public string getFirstName()
{
return firstName;
}
public string getLastName()
{
return lastName;
}
private void addButton_Click(object sender, EventArgs e)
{
if (firstNameBox.Text != "" && lastNameBox.Text != "")
{
firstName = firstNameBox.Text;
lastName = lastNameBox.Text;
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("Please provide both first name and last name", "Sorry", MessageBoxButtons.OK);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
Upvotes: 1
Reputation: 29264
This is how you call a modal dialog in C#
Add dlg = new Add();
if(dlg.ShowDialog() == DialogResult.OK)
{
lastName = dlg.LastName;
firstName = dlg.FirstName;
}
To do this you need to return DialogResult.OK
when the form Add
closes. So in the form you need
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult=DialogResult.OK;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
public string FirstName { get { return textBox1.Text; } }
public string LastName { get { return textBox2.Text; } }
}
Upvotes: 1
Reputation: 787
Here's a general pattern about dialogs:
private void addButton_Click(object sender, EventArgs e)
{
using(var add = new Add()) // so that it will be disposed after usage
{
if (add.ShowDialog(this) == DialogResult.OK)
{
firstName = add.getFirstName();
lastName = add.getLastName();
textArea.Text = "First Name: " + firstName + "\r\n";
textArea.Text += "Last Name: " + lastName;
}
}
}
And in your dialog code, you should remove isOK and use DialogResult = DialogResult.OK / DialogResult.Cancel in relative button events.
Upvotes: 2