Reputation: 1566
I have the following code as a popup dialog that takes input from inputBox. I pass in strings as reference hoping the ref string would change on dialog close, so I can get user input. But the strings passed in did not change on dialog close. What did I do wrong?
public static DialogResult ShowInputDialog(ref string input1, ref string input2)
{
var size = new System.Drawing.Size(520, 180);
var inputBox = new Form { ClientSize = size };
var panel = new TableLayoutPanel
{
Size = new System.Drawing.Size(460, 180),
Location = new System.Drawing.Point(25, 15),
ColumnCount = 2,
RowCount = 3
};
// Add ColumnStyles/RowStyles here
panel.Controls.Add(new Label { Text = "Input 1", TextAlign = ContentAlignment.BottomRight }, 0, 0);
panel.Controls.Add(new Label { Text = "Input2", TextAlign = ContentAlignment.BottomRight }, 0, 1);
panel.Controls.Add(new TextBox { Text = input1, Width = 280 }, 1, 0);
panel.Controls.Add(new TextBox { Text = input2, Width = 280 }, 1, 1);
var okButton = new Button{ DialogResult = DialogResult.OK};
var cancelButton = new Button {DialogResult = DialogResult.Cancel};
var buttons = new FlowLayoutPanel();
buttons.Controls.Add(okButton);
buttons.Controls.Add(cancelButton);
panel.Controls.Add(buttons, 1, 3);
inputBox.Controls.Add(panel);
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
var result = inputBox.ShowDialog();
return result;
}
The usage of the above code is:
string input1 = string.Empty;
string input2 = string.Empty;
ShowInputDialog(ref input, ref input2);
Upvotes: 0
Views: 552
Reputation: 82
After the user has click ok button, you have to assign the textbox.text value back to input1 and input2
Upvotes: 1
Reputation: 2304
I'm not too familiar with TableLayoutPanel
but maybe you could do something as simple as:
if (inputBox.ShowDialog() == DialogResult.OK)
{
input1 = (panel.GetControlFromPosition(1, 0) as TextBox).Text;
input2 = (panel.GetControlFromPosition(1, 1) as TextBox).Text;
return DialogResult.OK;
}
return DialogResult.Cancel;
Your problem at the moment is that you're not actually setting the value anywhere after the dialog is closed.
However, I agree with the comment. A type of MVVM pattern would probably make the maintaining (and creation) of these types of properties and their respective values much easier.
Upvotes: 0