回 帖 发 新 帖 刷新版面

主题:[讨论]大家讨论讨论下!(版主进来看看嘛!)

//如下是我写的创建动态控件的一些内容,是关于四则运算的内容,现在我是想建立一字符串类数组(红色部分),用来存储我在所有动态生成的TextBox控件内的数值,然后将这个字符串数组的每个值同正确值(RightAnswers)进行比较,最后在重新生成的控件lc内显示对与错!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PupilGradeSystem
{
    public partial class TestForm : Form
    {
        private UserClass qc;
        private UserClass tc;
        public TestForm(UserClass Send)
        {
            qc = Send;//从类中接收从主窗题发送的数据
            tc = Send;
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            int num1, num2;//随即产生两个相加的数
            System.Random Rd = new Random(System.DateTime.Now.Millisecond);
            num1 = Rd.Next(0, 10);
            num2 = Rd.Next(0, 10);
            this.label2.Text = qc.QuestionCounts.ToString();
            this.label4.Text = tc.TimeCounts.ToString();
            int n=int.Parse(this.label2.Text);//将接收的数据赋给要产生控件的个数
            Label[] lb = new Label[n];//创建动态控件组件
            lb[0] = new Label();
            lb[0].Top = 10;
            lb[0].Left = 10;
            lb[0].Width = 80;
            lb[0].Text = "题号1:" + num1.ToString() + "+" + num2.ToString() + "=";
            TextBox[] tb = new TextBox[n];
            tb[0] = new TextBox();
            tb[0].Top = 5;
            tb[0].Left = 100;
            tb[0].Width = 40;
            [color=800000]string[] str=new string[n];[/color[size=6][b]]/*建立一个字符串的数组,用来保存tb中的值*/[/b][/size]           
            for (int i = 1; i < n; i++)
            {
                num1 = Rd.Next(0, 10);
                num2 = Rd.Next(0, 10);
                lb[i] = new Label();
                lb[i].Top = lb[i - 1].Top + lb[i - 1].Height;
                lb[i].Left = lb[i - 1].Left;
                lb[i].Text = "题号" + (i + 1).ToString() + ":" + num1.ToString() + "+" + num2.ToString() + "=";
                lb[i].Width = 80;
                tb[i] = new TextBox();
                tb[i].Top = tb[i - 1].Top + tb[i - 1].Height+2;
                tb[i].Left = tb[i - 1].Left;
                tb[i].Width = 40;
            }
            
            this.Controls.AddRange(lb);
            this.Controls.AddRange(tb);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num1, num2, RightAnswer;
            System.Random Rd = new Random(System.DateTime.Now.Millisecond);
            num1 = Rd.Next(0, 10);
            num2 = Rd.Next(0, 10);
            RightAnswer = num1 + num2;
            int n = int.Parse(this.label2.Text);
            Label[] lc = new Label[n];
            lc[0] = new Label();
            lc[0].Top = 5;
            lc[0].Left = 150;
            for (int i = 0; i < n; i++)
            {

                lc[i] = new Label();
                lc[i].Top = 5;
                lc[i].Left = 150;
                if ([color=00FF00]str[i] [/color]== RightAnswer.ToString())//作比较
                {
                    lc[i].Text = "你做对了√!";
                }
                else
                {
                    lc[i].Text = "你做错了×!";
                }
            }
            this.Controls.AddRange(lc);
            //this.Controls.AddRange(tb);
        }
  }
}

回复列表 (共2个回复)

沙发

错了,全部搞红了!
//如下是我写的创建动态控件的一些内容,是关于四则运算的内容,现在我是想建立一字符串类数组(红色部分),用来存储我在所有动态生成的TextBox控件内的数值,然后将这个字符串数组的每个值同正确值(RightAnswers)进行比较,最后在重新生成的控件lc内显示对与错!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PupilGradeSystem
{
    public partial class TestForm : Form
    {
        private UserClass qc;
        private UserClass tc;
        public TestForm(UserClass Send)
        {
            qc = Send;//从类中接收从主窗题发送的数据
            tc = Send;
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            int num1, num2;//随即产生两个相加的数
            System.Random Rd = new Random(System.DateTime.Now.Millisecond);
            num1 = Rd.Next(0, 10);
            num2 = Rd.Next(0, 10);
            this.label2.Text = qc.QuestionCounts.ToString();
            this.label4.Text = tc.TimeCounts.ToString();
            int n=int.Parse(this.label2.Text);//将接收的数据赋给要产生控件的个数
            Label[] lb = new Label[n];//创建动态控件组件
            lb[0] = new Label();
            lb[0].Top = 10;
            lb[0].Left = 10;
            lb[0].Width = 80;
            lb[0].Text = "题号1:" + num1.ToString() + "+" + num2.ToString() + "=";
            TextBox[] tb = new TextBox[n];
            tb[0] = new TextBox();
            tb[0].Top = 5;
            tb[0].Left = 100;
            tb[0].Width = 40;
            [color=800000]string[] str=new string[n];[/color]/*建立一个字符串的数组,用来保存tb中的值*/          
            for (int i = 1; i < n; i++)
            {
                num1 = Rd.Next(0, 10);
                num2 = Rd.Next(0, 10);
                lb[i] = new Label();
                lb[i].Top = lb[i - 1].Top + lb[i - 1].Height;
                lb[i].Left = lb[i - 1].Left;
                lb[i].Text = "题号" + (i + 1).ToString() + ":" + num1.ToString() + "+" + num2.ToString() + "=";
                lb[i].Width = 80;
                tb[i] = new TextBox();
                tb[i].Top = tb[i - 1].Top + tb[i - 1].Height+2;
                tb[i].Left = tb[i - 1].Left;
                tb[i].Width = 40;
            }
            
            this.Controls.AddRange(lb);
            this.Controls.AddRange(tb);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num1, num2, RightAnswer;
            System.Random Rd = new Random(System.DateTime.Now.Millisecond);
            num1 = Rd.Next(0, 10);
            num2 = Rd.Next(0, 10);
            RightAnswer = num1 + num2;
            int n = int.Parse(this.label2.Text);
            Label[] lc = new Label[n];
            lc[0] = new Label();
            lc[0].Top = 5;
            lc[0].Left = 150;
            for (int i = 0; i < n; i++)
            {

                lc[i] = new Label();
                lc[i].Top = 5;
                lc[i].Left = 150;
                if ([color=00FF00]str[i] [/color]== RightAnswer.ToString())//作比较
                {
                    lc[i].Text = "你做对了√!";
                }
                else
                {
                    lc[i].Text = "你做错了×!";
                }
            }
            this.Controls.AddRange(lc);
            //this.Controls.AddRange(tb);
        }
  }
}

板凳

直接获取相应TextBox中的内容不就行了?

如果非要采用数组缓存的方法,那么把数组变成类全局变量,然后在修改TextBox内容(就是需要更新缓存)的时候将相关的数组元素更新一下。

我来回复

您尚未登录,请登录后再回复。点此登录或注册