如何在C#中访问表单控件 [英] How do I access form controls in C#

查看:70
本文介绍了如何在C#中访问表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我正在用C#写一个测验程序,我对如何比较用户对答案的答案感到有些失落键。基本上,我希望有2个数组(如果我的方法效率低,请提示另一种方式)一个存储答案键,另一个存储用户输入。所以我希望用户在文本框中键入他们的答案,然后程序会将文本框的文本与答案键进行比较......基本上是这样的:

  if (textBoxes [i] .Text == answerKey [i]){correct ++; } 
else {错误++; }



其中,正确和错误的变量跟踪用户有多少问题是正确和错误,answerKey数组存储所有答案。



所以我的问题是,我如何获取表单中的所有文本框并将它们放入文本框数组?

解决方案

< blockquote>尝试这个..



  int  countAnswers =  0 ; 
列表< TextBox> lstText = new 列表< TextBox> ();
var controls = this .Controls.OfType< TextBox>()。ToArray();
for int i = 0 ; i < controls.Length; i ++)
{
if (controls [i] .Name == textBox + i)
lstText.Add (对照[I]);
}
// 确保lstText只有10个项目

string [] answers = { a b c d e f g h i j};
for int i = 0 ; i < 10 ; i ++)
{
if (lstText [i] .Text == answers [i])
countAnswers ++;
}
int countWrong = lstText.Count - countAnswers;


这是一种可能性。

  //  创建列表以保存文本框控件;我们的表单包含四个文本框控件 
List< TextBox> textBoxes = new List< TextBox>();
textBoxes.Add(textBox1);
textBoxes.Add(textBox2);
textBoxes.Add(textBox3);
textBoxes.Add(textBox4);

// 迭代列表并根据答案键检查文本框文本
foreach (TextBox tb in textBoxes)
{
if (answerKey.Contains(tb.Text)){correct ++; }
else {错误++; }
}


Hello!

I am writing a quiz program in C# and I'm at a bit of a loss on how I can compare the user's answers to the answer key. Basically, I want there to be 2 arrays (if my method is inefficient please suggest another way) one which stores the answer key and one which stores the user's input. So I want the user to type in their answers into textboxes and then the program will compare the textbox's text to the answer key... Basically like this:

if (textBoxes[i].Text == answerKey[i]) { correct++; }
else { wrong++; }


Where, the correct and wrong variables keep track of how many questions the user got right and wrong and the answerKey array stores all the answers.

So my question is, how can I take all of the textboxes inside of my form and place them into the textbox array?

解决方案

Try this..

int countAnswers = 0;
            List<TextBox> lstText =new List<TextBox> ();
            var controls = this.Controls.OfType<TextBox>().ToArray();
            for (int i = 0; i < controls.Length; i++)
            {
                if (controls[i].Name == "textBox" + i)
                    lstText.Add(controls[i]);
            }
            // make sure that the lstText has only 10 items

            string[] answers = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
            for (int i = 0; i < 10; i++)
            {
                if (lstText[i].Text == answers[i])
                    countAnswers ++;
            }
            int countWrong = lstText.Count - countAnswers;


Here's one possibility.

// create list to hold textbox controls; our form contains four textbox controls
List<TextBox> textBoxes = new List<TextBox>();
textBoxes.Add(textBox1);
textBoxes.Add(textBox2);
textBoxes.Add(textBox3);
textBoxes.Add(textBox4);            

// iterate list and check textbox text against the answer keys
foreach (TextBox tb in textBoxes)
{
    if (answerKey.Contains(tb.Text)) { correct++; }
    else { wrong++; }                
}


这篇关于如何在C#中访问表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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