AnLa061095
AnLa061095

Reputation: 11

Custom dialog box control in c#

I have a question I hope some of you might be able to answer, I haven't found any ways to do this on google or here.

What I want: - A custom control that functions just like an input box. (But it has to be a winform control that can be added to a form. Not a form.) - It has to be able to grab the value from its text box and send it to the parent in the function it was called in.

Here is how I want to call it:

string str = MyBox.GetString("control title");

Can anyone help?

I don't know if this is event possible in c#. I couldn't figure it out, but if anyone can please answer!

Upvotes: 1

Views: 6202

Answers (2)

Bala R
Bala R

Reputation: 109017

You want something like this

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

    public string ResultText { get; set; }

    public static string GetString(string title)
    {
        var box = new MyBox {Text = title};

        if (box.ShowDialog() == DialogResult.OK)
        {
            return box.ResultText;
        }

        return string.Empty;
    }

    private void okButton_Click(object sender, EventArgs e)
    {
        this.ResultText = txtUserInput.Text;
        this.DialogResult = DialogResult.OK;
    }
}

where MyBox would be a Form with TextBox - txtUserInput and an okay button linked to the okButton_Click event.

And you can make calls from other forms like this:

string userInput = MyBox.GetString("Title for MyBox");

Upvotes: 2

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19347

If you want the box to reside on a form, you can just use a regular TextBox to get the inupt. Maybe eclose it in a GroupBox to give a "title", add a description label.

Lastly, and most importantly, add an "Update" Button to the GroupBox. Inside this button's Click handler, you can retrieve the value of the textbox with string str = textbox.Text.

Upvotes: 0

Related Questions