简体   繁体   中英

c# how to show value box on a button click

I wish to know if it's possible/how you show a popup box where you can enter a value and use it in c# forms application (visual studio). There is probably already guides about this somewhere, however I were unable to find out as I most-likely don't know the correct term used for it.

Basically I need a box to show up where you enter a number and use that number somewhere else.

"""

EVENT:: Click button --> open a popup box --> enter value --> enter --> use value somewhere else.

"""" Not sure if this is possible or you have to use a new forms page.

Thank you for your answers.

Well, there is an API called TaskDialog, it looks like Message Box but you can add component to it, Example

But it will be better if you create your own form, because you will have more possibilities and less code to write, if you create your own form you can add a public property then you call your form using ShowDialog function and get the value at the end.

using(FormDialog fr= new FormDialog())
{
    if(fr.ShowDialog()==System.Windows.Forms.DialogResult.OK)
    {
    //get your property
    }
}

on your form (FromDialog) you should use this.DialogResult=System.Windows.Forms.DialogResult.OK; instead of this.close() or dispose(); then you can use an event to manage if you want to validate using enter ,most likely:

private void FormDialog_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                //set your property
                this.DialogResult=System.Windows.Forms.DialogResult.OK;
            }
        }

You create a form (MyForm) that inherits from Form and Exposes the data you need (FirstName, LastName) on entering. When you click your button the Form shows up.

public void eventClick(....)
{
    MyForm f = new MyForm();
    f.Closed + = ClosedHandler;
    f.Show();
}

In the MyForm you enter the data and populate the data as MyForm's public variable. Because you main form is subscribed to the Closing event it will trigger an eventhandler and get the data from it.

public void ClosedHandler(....)
{
    MyForm f = sender as MyForm
    if(f!=null)
    {
        Person p = new Person()
        {
            FirstName = f.FirstName;
            LastName = f.LastName;
        };
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM