在c#中验证每个文本框的每个文本框都是INT

时间:2017-10-05 00:48:43

标签: c# winforms

您好我在网上搜索此代码

{% extends 'app.twig' %}

{% block content %}
  <section class="loading" v-if="loading">
    One Moment
  </section>

  {% if item.image is defined %}
    <section class="item item--with-image" v-for="item in items" v-else>
      <img :src="item.image" :alt="item.service">

      <h3 class="item__title">
        <a :href="item.link" target="_blank">${ item.title }</a>
      </h3>

      <section class="item__service">
          <p>
            Source: ${ item.service }
            {% if item.author %}
              | Author: ${ item.author }
            {% endif %}
          </p>
      </section>

      <section class="item__content">
        <p>${ item.content }</p>
      </section>
    </section>
  {% else %}
    <section class="item" v-for="item in items" v-else>
      <h3 class="item__title">
        <a :href="item.link" target="_blank">
          <h3>${ item.title }</h3>
        </a>
      </h3>

      <section class="item__service">
          <p>
            Source: ${ item.service }
            {% if item.author %}
              | Author: ${ item.author }
            {% endif %}
          </p>
      </section>

      <section class="item__content">
        <p>${ item.content }</p>
      </section>
    </section>
  {% endif %}
{% endblock %}

就像在txtCCL.Text中一样,我如何调用多个文本框来检查

        con.Open();
        int parsedValue;
        if (!int.TryParse(txtCCL.Text, out parsedValue))// if not an integer return message below
        {
            MessageBox.Show("This is a number only field");
            return;
        }
        else
        {
        }

提前致谢

2 个答案:

答案 0 :(得分:1)

您必须迭代所有文本框。

TryParse无法将多个文本框作为输入。

    //LOOP ALL CONTROLS AND FIND TEXT BOXES
    foreach (TextBox txtBox in this.Controls.OfType<TextBox>())
    {
       int number;

       bool result = Int32.TryParse(txtBox.Text, out number);

       if (result)
       {
           //PRINT TEXT BOX NAME WHICH CAN BE CONVERTIBLE
           MessageBox.Show(txtBox.Name + " is a number only field");
       }
    }

答案 1 :(得分:0)

您可以将TryParse方法与if条件一起使用多次检查

int v1;
    if (int.TryParse(txtCCL.Text, out v1) && int.TryParse(txt2.Text,out v1) && int.TryParse(txt3.Text,out v1))
    {
            //is integer
    }