labelTotal holds the value of class Keypad (C# WinForms). ToString has been overriden to return labelTotal.Text.
namespace Gui3
{
public partial class Keypad : Form
{
public Keypad()
{
InitializeComponent();
}
public override String ToString() {return labelTotal.Text;}
private void buttonOk_Click(object sender, EventArgs e)
{
this.Close();
开发者_Go百科 }
...
Why doesn't keypad.ShowDialog().ToString() return labelTotal.Text?
namespace Gui3
{
public partial class Setup : Form
{
public Setup()
{
InitializeComponent();
}
private void buttonStartDepth_Click(object sender, EventArgs e)
{
Keypad keypad = new Keypad();
////////// Not working as expected /////////
String total = keypad.ShowDialog().ToString();
...
Because the ShowDialog()
method returns a System.Windows.Forms.DialogResult
enum value, not the instance of your form. ToString()
will be called on the enum value returned by this function.
You might try something like the following (assumes keypad
will properly return DialogResult.OK
):
private void buttonStartDepth_Click(object sender, EventArgs e)
{
Keypad keypad = new Keypad();
if (keypad.ShowDialog() == DialogResult.OK)
{
String total = keypad.ToString();
}
}
Because you are not calling KeyPad.ToString() you are calling DialogResult.ToString(). ShowDialog() returns a DialogResult.
The method you are calling ShowDialog()
actually displays the dialog
I think that what you want to do is
keypad.ShowDialog();
String total = keypad.ToString();
ShowDialog returns a DialogResult, not a reference to the form.
Try changing to
String total;
if (keypad.ShowDialog() == DialogResult.OK)
{
total = keypad.ToString();
}
精彩评论