如果缺少多个输入,如何创建消息框? [英] How do I make a message box when more than one input is missing?

查看:72
本文介绍了如果缺少多个输入,如何创建消息框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我目前正在尝试编写一个计算器程序,并在征求反馈时向我指出,目前尚不清楚只有一个输入框应为空,而其余输入框需要一个数字。我已经尝试了下面的代码,但我正在努力解决这个问题。



有没有人有任何可以提供的建议?



我感谢您对问候的任何建议关于这个主题。



非常感谢和许多亲切的问候。



我是什么尝试过:



Hi there,

I'm currently trying to write a calculator program and upon asking for feedback it was pointed out to me that it wasn't clear that only one input box should be empty while the rest require a number. I've tried the below code but am struggling to solve the problem.

Does anyone have any advice they could offer?

I appreciate any suggestions you might have with regards to this topic.

Thank you very much and many kind regards.

What I have tried:

 public Form1()
        {
            InitializeComponent();

            txt_P1In.Text = string.Empty;
            txt_P2In.Text = string.Empty;
            txt_Vol1In.Text = string.Empty;
            txt_Vol2In.Text = string.Empty;
            txt_U1In.Text = string.Empty;
            txt_U2In.Text = string.Empty;
            txt_vel1In.Text = string.Empty;
            txt_vel2In.Text = string.Empty;
            txt_h1In.Text = string.Empty;
            txt_h2In.Text = string.Empty;
            txt_QIn.Text = string.Empty;
            txt_WIn.Text = string.Empty;
            Z = 0;
            P1 = P2 = V1 = V2 = U1 = U2 = v1 = v2 = h1 = h2 = Q = W = Z;
            g = 9.81;
        }

private void CalcBtnOut_Click(object sender, EventArgs e)
        {
            if (radioBtnP1.Checked == true)
            {
                if (txt_P2In.Text == null || txt_Vol1In.Text == null || txt_Vol2In.Text == null || txt_U1In.Text == null || txt_U2In.Text == null || txt_vel1In.Text == null || txt_vel2In.Text == null || txt_h1In.Text == null || txt_h2In.Text == null || txt_QIn.Text == null || txt_WIn.Text == null)
                {
                    MessageBox.Show("You have a missing input");
                }
                else
                P1 = (((P2 * V2) + U2 + ((v2 * v2) / 2) + (g * h2) + W - Q - (g * h1) - ((v1 * v1) / 2) - U1) / V1);
                txt_P1Out.Text = P1.ToString();
                txt_P1In.Text = txt_P1Out.Text;
            }
            else
            if (CalcP1.Checked == false)
            {
                txt_P1Out.Text = null;
            }

推荐答案

TextBox的Text属性永远不会是 null - 它可以是空字符串,但它永远不会为空。

所以这个测试:

The Text property of a TextBox will never be null - it can be the empty string, but it's never null.
So this test:
if (txt_P2In.Text == null || txt_Vol1In.Text == null || txt_Vol2In.Text == null || txt_U1In.Text == null || txt_U2In.Text == null || txt_vel1In.Text == null || txt_vel2In.Text == null || txt_h1In.Text == null || txt_h2In.Text == null || txt_QIn.Text == null || txt_WIn.Text == null)

永远不会成功。

代替检查每个文本框:

will never succeed.
Instead check each textbox with:

if (string.IsNullOrWhitespace(txt_P2In.Text) || string.IsNullorWhiteSpace(...



就个人而言,我'使用方法:


Personally, I'd use a method:

private bool IsAnyFieldEmpty(params TextBox[] boxes)
    {
    foreach (TextBox box in boxes)
        {
        if (string.IsNullOrWhiteSpace(box.Text)) return true;
        }
    return false;
    }

并将其称为:

And call it like this:

if (IsAnyFieldEmpty(txt_P2In, txt_Vol1In, txt_Vol2In, txt_U1In, 
                    txt_U2In, txt_vel1In, txt_vel2In, txt_h1In, 
                    txt_h2In, txt_QIn, txt_WIn.Text))


这篇关于如果缺少多个输入,如何创建消息框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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