如何在循环中找到Lable [英] How To Find Lable In For Loop

查看:44
本文介绍了如何在循环中找到Lable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了在线考试系统。在ExamQuestionsPaper一个问题及其选项将被显示。在这个问题下面,我已经拿了50个标签。这个标签说在这个考试中有50个问题。现在如果用户回答问题1,那么Label1(从50个标签)变成绿色。

为此,我必须使用For循环。

i have made onlineExam system.In ExamQuestionsPaper One Question and their options will be display. Below that Question,i have taken 50 Labels.This Labels Says That in This Exam there are 50 Questions.Now If Question 1 is answred by User,then Label1 (From 50 Labels)becomes Green color.
For that I have to Use For Loop.

for (i = 0; i < 50; i++)
                        {
                            //Here Id is QuestionNumber  
                            if (Id == Convert.ToInt16(Lable1.Text))
                            {
                                Lable1.ForeColor = Color.Green;
                            }
                        }





我的问题是如何在FOR循环中找到标签ID。在我的代码中我有写了Label1.但我必须从50个Lables找到Lable。



什么是这个问题的解决方案。



My Problem is How To Find Label ID in FOR Loop.In My code I have written Label1.But I have To find Lable From 50 Lables.

What is Solution of this Problem.

推荐答案

你可以从Page获得所有控件。因此,当你按照他们的类型获得控件时,你可以改变他们的颜色。

这是一个链接,解释你如何做到这一点:

获取网页中的所有控件 [ ^ ]

类似的代码将是:

Well you can get all the controls from the Page. so when you get the controls by their type you can change their color.
Here is a link which explains how you can do it:
Get all controls in web page[^]
A similar code will be:
public void GetUserControls(ControlCollection controls)
{
    foreach (Control ctl in controls)
    {
        if (ctl is UserControl)
        {
            // Do whatever.
        }

        if (ctl.Controls.Count > 0)
            GetUserControls(ctl.Controls);
    }
}



您可以将它用作:


And you can use it as:

GetUserControls(Page.Controls);





祝你好运,

OI



Good luck,
OI


for循环将耗费大量资源我建议



A for loop will be resource intensive i would suggest

//will return all Controls in  ctrlToSearch.Controls whose name is nameofControl
private Control[] FindControlsByName(Control ctrlToSearch, string nameofControl)
{
return ctrlToSearch.Controls.Find(nameofControl, true);
}


这篇关于如何在循环中找到Lable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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