Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys. Im back again. Do you guys have any ideas how I can take inputs in a MessageBox-like dialog? I would like to get an integer input(Quantity) so I can multiply it with the price

What I have tried:

If I remember correctly there's something like that in java but I'm new to c# sorry for asking too many questions. Thanks in advance!
Posted
Updated 23-Sep-16 2:22am
Comments
[no name] 22-Sep-16 8:18am    
Sure, create a dialog that takes the input you want.

 
Share this answer
 
v2
Comments
Member 12724527 22-Sep-16 8:28am    
Thanks bro!
If you are using Windows Forms, consider two strategies:

1. if you only have a few values to get from the user, consider using a Panel on the Form which is hidden (InputPanel.Visible = false) until you want to show it and get the information. Or, you could use a UserControl in the same way.

2. if your design requires you to "go modal" ... to show a pop-up that will block the Application until it is closed ... in C# you need to use a Form. So, create the Form at design time, adding the input fields you need. Then you'll need to find a way to get the user-input data to the running Application when the user clicks the submit/enter Button, or, possibly, when the Form is closed by the user.

2.a. how to do that:

2.a.1 create the input form, 'InputForm
2.a.2. put Close and Submit Buttons on it, 'btnClose, 'btnSubmit

2.a.3 example code: this assumes that you want to know three possible outcomes of the user's actions on the input Form:

a. did the user close the Form, or Cancel ?
b. did the user enter invalid data
c. did the user enter valid data

The data input Form:"
C#
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class InputForm : Form
    {
        public InputForm()
        {
            InitializeComponent();
        }

        public Action<bool?, int> ReturnInputFormData { set; get; }

        private bool hasData = false;

        public void ValidateAndReturnData()
        {
            if (ReturnInputFormData == null) return;

            if (hasData)
            {
                int data = 0;

                bool dataOkay = Int32.TryParse(inputDataTextBox.Text, out data);

                ReturnInputFormData(dataOkay, data);
            }
            else
            {
                ReturnInputFormData(null, 0);
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            hasData = inputDataTextBox.Text != string.Empty;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            hasData = false;
            this.Close();
        }

        private void InputForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ValidateAndReturnData();
        }
    }
}
Example of how this is used on a Main Form in a Button Click EventHandler:
C#
private void GetUserData_Click(object sender, EventArgs e)
{
    InputForm inForm = new InputForm();

    inForm.ReturnInputFormData = ReturnInputFormData;

    inForm.Show();
}

private void ReturnInputFormData(bool? userEntryResult, int i)
{
    switch (userEntryResult)
    {
        case null:
            Console.WriteLine("user closed form, or clicked Cancel");
            break;
        case true:
            Console.WriteLine("user entry valid: {0}", i);
            break;
        case false:
            Console.WriteLine("user entered invalid value");
            break;
        default:
            break;
    }
}
Note:

1. we used a nullable bool return parameter to distinguish between no action or empty data on Form closing from the user clicking submit.

... to keep this sample code short ... we omitted doing some things you might want to handle in production code ...

2. this code does not detect/report something like the user entering some data, perhaps valid data, and then closing the Form by clicking on the cancel button, or closing the window. in some cases (naive users ?), you might want to handle that and put a message up to help the user.

3. this code does not specifically handle, as a separate case, the user entering nothing, or clearing the text input field, and then clicking the submit button. My preference is to make a submit button disabled by default, and only make it enabled when there is valid data in the input field.
 
Share this answer
 
Comments
Member 12724527 23-Sep-16 9:19am    
Wow, Dude you're very serious about giving a hand to newbies like me. I salute you bro. Very detailed and well explained. Thank you very much Ill remember you.
BillWoodruff 23-Sep-16 10:20am    
I'm glad you found my response helpful. fyi: use of the 'Action delegate syntax is a way to quickly define a Delegate; it's equivalent to writing:

internal delegate void GetInputFormData(bool? result, int value);

A delegate defined using Action (or Func) is, like all other Delegates, and Events, a multi-cast object, which can have multiple subscribers (users) whose compatible EventHandlers match its parameter Types.

internal GetInputFormData ReturnInputFormData { set; get; }

The usual technical term used for a block of executable code inserted/injected into another code-context in such a way that some event in that context sends a message and/or returns data to the context that inserted/injected the code ... is ... a "callback"

What I like about this way of handling communication between .NET objects is that it helps ensure "encapsulation:" the Form shown modally here has no "knowledge" of if it has any subscribers to its callback, no access to its subscribers; it can't possibly mess up the objects it notifies, and, if the programmer tries to make the callback return anything but valid instances of the Types it defines, there will be a compile-time error.
Member 12724527 23-Sep-16 11:08am    
Dude, Code Project should make a 'like' button just for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900