类似于Messagebox的输入 [英] Messagebox-like that takes input

查看:99
本文介绍了类似于Messagebox的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们。我又回来了。你们有没有想法如何在类似MessageBox的对话框中输入输入?我想得到一个整数输入(数量),所以我可以将它与价格相乘



我尝试过:



如果我没记错的话,在java中有类似的东西,但我是c的新手#抱歉问了太多问题。在此先感谢!

解决方案

请参阅下面的内容。



c# - 带输入字段的消息框 - Stack Overflow [ ^ ]



什么是VB.net的C#版本&#inputDialog? - 堆栈溢出 [ ^ ]


如果您使用的是Windows窗体,请考虑两种策略:



1.如果您只有几个值来自用户,请考虑使用隐藏的表单上的Panel(InputPanel.Visible = false),直到您想要显示它并获取信息。或者,您可以以相同的方式使用UserControl。



2.如果您的设计要求您去模态...显示弹出窗口将阻止应用程序直到它关闭...在C#中你需要使用一个表单。因此,在设计时创建表单,添加所需的输入字段。然后,当用户单击提交/输入按钮时,或者可能在用户关闭表单时,您需要找到一种方法将用户输入数据提供给正在运行的应用程序。



2.a.怎么做:



2.a.1创建输入表单,'InputForm

2.a.2。关闭并提交按钮,'btnClose,'btnSubmit



2.a.3示例代码:这假设您想知道用户的三种可能结果输入表格上的行动:



a。用户是否关闭了表格,或取消?

b。用户输入了无效数据

c。用户是否输入了有效数据



数据输入表格:

 使用系统; 
使用 System.Windows.Forms;

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

public 操作< 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 数据);

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

私有 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();
}
}
}

如何在按钮的主窗体上使用它的示例单击EventHandler:

  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( 用户关闭表单,或单击取消);
break ;
case true
Console.WriteLine( 用户条目有效:{0},i);
break ;
case false
Console.WriteLine( 用户输入的值无效);
break ;
默认
break ;
}
}

注意:



1.我们使用了一个可以为空的bool返回参数来区分无动作或从用户点击提交关闭表单上的空数据。



...保持此示例代码简短...我们省略了你可能想要处理的一些事情生产代码......



2.此代码不检测/报告用户输入某些数据,可能是有效数据,然后通过单击关闭表单取消按钮,或关闭窗口。在某些情况下(天真的用户?),你可能想要处理它并发一条消息来帮助用户。



3.此代码没有专门处理,作为单独的案例,用户不输入任何内容,或清除文本输入字段,然后单击提交按钮。我的偏好是默认情况下禁用提交按钮,只有在输入字段中有有效数据时才启用它。


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!

解决方案

Please refer below .

c# - Messagebox with input field - Stack Overflow[^]

What is the C# version of VB.net&#39;s InputDialog? - Stack Overflow[^]


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:"

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:

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.


这篇关于类似于Messagebox的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆