I have created a custom message box with a textbox for input which appears under a certain condition in form1. I want form1 to hold the value of the textbox if the submit btn is clicked. I am not getting the desired result.
This is similar to this however I don’t want the processing to happen in the message box because the process requires so many variables that I would have to transfer to the messsagebox.
Form condition
}
else //NOT ALL APPROVE
{
string BtnClicked = DenyEmpRequest.ShowBox(AllDenied, EmpRequestID);
if (BtnClicked == "1") //SUBMIT BTN WAS CLICKED
{
DenyEmpRequest emp = new DenyEmpRequest();
string reason = emp.Reason_Txt.Text;
}
I know that it is because I am creating a new instance of the form that I used in the messagebox when I said “DenyEmpRequest emp = new DenyEmpRequest();”. I don’t know any other way to access the textbox in the messagebox.
Messagebox code
public static string ShowBox(string DenyEmp, string RequestID)
{
newMessageBox = new DenyEmpRequest();
newMessageBox.EmpToDeny_lbl.Text = DenyEmp;
EmpRequestID = RequestID;
newMessageBox.ShowDialog();
return Button_id;
}
private void SubmitBtn_Click(object sender, EventArgs e)
{
if (Reason_Txt.Text == string.Empty)
{
NoReason_Lbl.Visible = true;
}
else
{
Button_id = "1";
newMessageBox.Dispose();
}
Looks like you’re overcomplicating it. If you are just trying to retrieve a string from a custom MessageBox, just make a form with an OK/Cancel button and a text box. Make a public string property that wraps around the value of the text box’s “Text” property. And make the form set it’s DialogResult to DialogResult.OK if the OK button is clicked, DialogResult.Cancel if the cancel button is clicked.
Then you can call this form with code shown below:
This is the format you should be using.
EDIT:
In reference to your comment, if you have a form with a text box on it, just write the property like this: