"新手"我也想使用while循环 [英] "Newbie" wants to use a while loop as well

查看:63
本文介绍了"新手"我也想使用while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI

请协助重写以下代码(尤其是if语句部分),以便它使用while循环来检查是否捕获了足够的委托并填充在列表框中lstDelegates:



HI
Please assist in re-write the following code (especially the if statement part) such that it uses a while loop to check if the enough "delegates" were captured and populates on list box lstDelegates:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ingwenyama
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string OrganiserName, IDNumber, Contact, Nationality;
        int NumDays, NumDelegates;
       //int NumD;
        private void btnCapture_Click(object sender, EventArgs e)
        {

            populateDelegates(4);
            //radImbizo.Checked = false;
            //NumDelegates++;

        }

        private void radImbizo_CheckedChanged(object sender, EventArgs e)
        {
            txtDelegate.Enabled = true;

        }

        public int numDelegate(int num)
        {
            return NumDelegates = NumDelegates + 1;
        }

        public void populateDelegates(int maxDelegates)
        {
            lstDelegates.Items.Add(txtDelegate.Text);
            txtDelegate.Enabled = false;
            //NumDelegates = 0;
            if (NumDelegates < maxDelegates)
            {
                txtDelegate.Enabled = true;
                numDelegate(NumDelegates);
                txtDelegate.Clear();
                txtDelegate.Focus();

                //
            }
            else
            {
                txtDelegate.Enabled = false;
                btnCapture.Enabled = false;
            }
        }

       

}

推荐答案

代表来自哪里?您的代码只是将一个代表添加到列表中。不需要此方法:
Where is the text for Delegates coming from? Your code is just adding one Delegate to the list. This method isn't needed:
numDelegate(int num)



您的while循环是这样的:


Your while loop would be like this:

private void buttonCapture_Click(object sender, EventArgs e)
        {
            populateDelegates(4);
        }

        private void populateDelegates(int maxDelegates)
        {
            this.numDelegates = 0;
            this.listBox1.Items.Clear();
            while (numDelegates < maxDelegates)
            {
                this.listBox1.Items.Add("Delegate " + numDelegates.ToString());
                numDelegates++;
            }

        }


这篇关于&QUOT;新手&QUOT;我也想使用while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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