0

First of all, I would like to say that english is not my mother language. I have this Form in windows forms:

Form

with this code:

public partial class FormInput : Form
{
    public FormInput(string Content)
    {
        InitializeComponent();

        this.Text = Content;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //confirm
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //cancel
    }
}

Now, I try to implement it in a WPF window. I want to use it like that:

string foo;

if (FormInput.Show("abc") == true)
{
    foo = [Form output as a string];
}

So, I need one input value as a string and two output value as a bool and string. The user have to enter something in the TextBox (textBox1) end click confirm or press enter and the window close. Alternativly, the user can press escape or click cancel. How can I do that?

6
  • That's not a MessageBox. It seems you want to replicate VB6's InputBox. Forms just don't work like this in .NET, going back all the way to 2002. VB 6's static form instance was a "convenience" trick that cause a lot of trouble when people realized the single form kept the old data. Commented May 1, 2024 at 13:47
  • So what is your question? Where are you encountering problems? Commented May 1, 2024 at 13:51
  • okay, but I need any way to make a function with the features I need. Commented May 1, 2024 at 13:52
  • I need a function that open a window with a cancel button, a confirm button and a textBox. The text in the textBox and the output value from the window (cancel/confirm) should given back. I already tried a return, but that did ot work with more than one value and across the window. Commented May 1, 2024 at 13:57
  • You have to create that form just like any other form. That's shown in all Windows Forms tutorials. You create an instance of the form with new, set its properties and call Show() if you want it to be modeless, ShowDialog() if you want it to be modal. If the user clicks OK, you read the properties with the new data. Don't make the textbox a public property to read or set its Text property though Commented May 1, 2024 at 13:59

2 Answers 2

0

Okay, I have the answer; Main window:

string output;

FormInput input = new FormInput("abc");
input.ShowDialog();
if (input.ReturnValueConfirm == true)
    output = input.ReturnValueText;

Second window:

public string ReturnValueText { get; set; }
public bool ReturnValueConfirm { get; set; }

public FormInput(string Content)
{
    InitializeComponent();

    this.Text = Content;
}

private void button1_Click(object sender, EventArgs e) //confirm
{
    ReturnValueText = textBox1.Text;
    ReturnValueConfirm = true;
    this.Close();
}

private void button2_Click(object sender, EventArgs e) //cancel
{
    ReturnValueText = textBox1.Text;
    ReturnValueConfirm = false;
    this.Close();
}

Thanks to the comments...

Sign up to request clarification or add additional context in comments.

5 Comments

Instead of ReturnValueConfirm and this.Close(), you should set the form's DialogResult instead. Then it would be if (input.ShowDialog() == DialogResult.Ok) ...
I tried that, but that didn´t work. Form1 is in wpf, form2 in win forms.
Your post didn't mention anything about WPF.
You are right, I thought, I have said it... Thank you!
I've changed the question.
0

You can create a static method that will create and open a form. Let it return a Boolean telling whether the user clicked OK or not, you can the return the string value through an out parameter.

Set the button's DialogResult property as following (I called the buttons btnOK and btnCancel):

// You can do this in the properties window.
btnOK.DialogResult = DialogResult.OK;
btnCancel.DialogResult = DialogResult.Cancel;

And in the form (this will close the form automatically):

// You can do this in the properties window.
AcceptButton = btnOk;
CancelButton = btnCancel;

With the TextBox being called txtValue, you get:

using System;
using System.Windows.Forms;

public partial class InputBox : Form
{
    public InputBox()
    {
        InitializeComponent();
    }

    public static bool TryGetInput(string title, out string value, string defaultValue = null)
    {
        var dlg = new InputBox();
        dlg.Text = title;
        dlg.txtValue.Text = defaultValue;

        if (dlg.ShowDialog() == DialogResult.OK) {
            value = dlg.txtValue.Text;
            return true;
        } else {
            value = null;
            return false;
        }
    }
}

This is really all the code. There is no need for button Click event handlers.

Now, you can call the input box like this:

if (InputBox.TryGetInput("Input required", out string value)) {
    //TODO: use 'value' here.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.