foreach (Control ctrl in panel1.Controls)
{
if (ctrl is Label)
MessageBox.Show(((Label)ctrl).Text);
}
我刚才想了一下还是没有解决的方法,不知道如何下手.以前也没有用过这个foreach问题是这个样子的:在panel里边有几个label.我想取出第一个,和另一个panel里边的label第一个数据进行比较.依次类推..第二 个...因为在panel1和panel2里边有很多个label,不知道如何才能取出某一个label里边的值...(现在是都能够读出来,就差取某一个值了....)
{
if (ctrl is Label)
MessageBox.Show(((Label)ctrl).Text);
}
我刚才想了一下还是没有解决的方法,不知道如何下手.以前也没有用过这个foreach问题是这个样子的:在panel里边有几个label.我想取出第一个,和另一个panel里边的label第一个数据进行比较.依次类推..第二 个...因为在panel1和panel2里边有很多个label,不知道如何才能取出某一个label里边的值...(现在是都能够读出来,就差取某一个值了....)
解决方案 »
- 帮忙看下这句SQL语句
- C#如何生成二维码的问题
- 关于过时的方法GetDefaultProxy()
- 求一个正则表达式
- 难道CSDN真的没高手?很少提问的,但一提个自定义控件的问题怎么就没人回答呢?
- DataSet.ReadXml读取Stream流时缺少根元素异常
- C#.NET2005中如何使控件透明??
- ASP.NET中如何将listbox控件里面的值传入下一个页面,并用Datalist控件显示出来
- sql2005转到SQL2000程序访问出错
- VS2012出问题了 一创建新项目就弹出 ”未找到与约束“
- 简单问题 : ASP 中如何获得 GridView 的行号 ?????
- 求一SQL语句写法?
{
int count1 = GetLabelCount(panel1);
int count2 = GetLabelCount(panel2); for (int i = 0; i < count1; ++i)
{
for (int j = 0; j < count2; ++j)
{
Label lable1 = GetLabel(panel1, i);
Label lable2 = GetLabel(panel2, i);
if (lable1.Text == lable2.Text)
{
}
}
}
} private int GetLabelCount(Panel panel)
{
int cout = 0;
foreach (Control ctrl in panel.Controls)
{
if (ctrl is Label)
{
++cout;
}
}
} private Label GetLabel(Panel panel,int labelIndex)
{
int index = 0;
foreach (Control ctrl in panel.Controls)
{
if (ctrl is Label)
{
if (labelIndex == index)
{
return (Label)ctrl;
}
}
}
}
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is Label)
//MessageBox.Show(((Label)ctrl).Text);
if(满足你要得那个label的条件)
MessageBox.Show((Label)ctrl).Text);
}
{
if (ctrl is Label)
{
Label lable = (Label)ctrl;
if(lable.Name == "libelname")
MessageBox.Show((Label)ctrl).Text);
}
是这样吗?
{
if (ctrl1 is Label)
{
foreach (Control ctrl2 in panel2.Controls)
{
if (ctrl1.Text == ctrl2.Text)
{
MessageBox.Show("相等");
}
}
}
// MessageBox.Show(((Label)ctrl).Text);
}
{
if (var.Text[0]==e.KeyChar)
{
}
}这个例子是 将控件中的控件panel1中的所有label控件都遍历一遍与键盘输入的字母做对比
你要取值就可以这样 去判断label上的text去取出特定的lable
在这个小程序里边是有三个panel.我是想把其中的没一个panel里边对应的数取出来,然后三个进行比较,
我刚才提问的是简化的一个问法.只要两个搞好了,三个也就没有问题了.
比如 1 2 3
其中1放在panel1里边的第一个label里边.2放在panel里的第一个label里边,然后3放在panel的第一个textbox里边.我要把这三个数都取出来,然后进行响应的1+2与3的比较.最后出一个结果.因为这里边的label比较多,所以麻烦些,但都是对应的第一个对应第一个,第二个对应第二个....
ArrayList panel1Text = new ArrayList();
ArrayList panel2Text = new ArrayList();
ArrayList panel3Text = new ArrayList();
foreach (Control ctrl1 in panel1.Controls)
{
if (ctrl1 is Label)
{
panel1Text.Add(Convert.ToInt32( ctrl1.Text));
}
}
foreach (Control ctrl2 in panel2.Controls)
{
if (ctrl2 is Label)
{
panel2Text.Add(Convert.ToInt32(ctrl2.Text));
}
}
foreach (Control ctrl3 in panel3.Controls)
{
if (ctrl3 is TextBox)
{
panel3Text.Add(Convert.ToInt32(ctrl3.Text));
}
}
for (int i = 0; i < panel1Text.Count; i++)
{
for (int j = 0; j < panel2Text.Count; j++)
{
for (int z = 0; z < panel3Text.Count; z++)
{
int a = (int)panel1Text[i];
int b = (int)panel2Text[j];
int k = a+b;
if (k == (int)panel3Text[z])
{
MessageBox.Show(panel1Text[i]+" + "+panel2Text[j]+"="+panel3Text[z]);
}
}
}
}
贴一下源程序.看看那的问题???我就是想做一个考试软件..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication10
{
public partial class Form1 : Form
{
private int second=0;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
int num;
num = Convert.ToInt32(textBox1.Text);
panel2.Controls.Clear();
panel2.Refresh();
panel3.Controls.Clear();
panel3.Refresh();
panel4.Controls.Clear();
panel4.Refresh();
for (int i = 0; i < num; i++)
{
//int x;
//int y;
Label lb1 =new Label ();
lb1.Location = new Point(0,16+i*25 );
lb1.Text = "第"+(i+1).ToString ()+"题:";
this.panel1.Controls.Add(lb1 );
Label lb2 = new Label();
lb2.Location = new Point(0,16+i*25);
//取随机数
Random rm = new Random();
StringBuilder sb = new StringBuilder();
sb.Append(rm.Next (100,1000));
lb2.Text =sb.ToString ();
this.panel2.Controls.Add(lb2); Label lb3 = new Label();
lb3.Text = comboBox1.SelectedItem.ToString ();
lb3.Location = new Point(0,16+i*25);
this.panel3.Controls.Add(lb3); Label lb4 = new Label();
lb4.Location = new Point(0, 16 + i * 25);
//取随机数
Random rmd = new Random();
StringBuilder sbl = new StringBuilder();
sbl.Append(rmd.Next(100,1000));
lb4.Text = sbl.ToString();
this.panel4.Controls.Add(lb4); Label lb5= new Label();
lb5.Text = "=";
lb5.Location = new Point(0, 16 + i * 25);
this.panel5.Controls.Add(lb5);
TextBox tb = new TextBox();
tb.Location =new Point (0,16+i*25);
this.panel6 .Controls .Add (tb);
/*lbl.Location = new Point(200, 16 + i * 25);
lbl.Text = "+";
this.panel1.Controls.Add(lbl);
lbl.Location = new Point(260, 16 + i * 25);
lbl.Text = i.ToString();
this.panel1.Controls.Add(lbl);*/ }
timer1.Enabled = true;
timer1.Interval =1000;
second= Convert .ToInt32 (textBox2 .Text)*60;
} private void Form1_Load(object sender, EventArgs e)
{
label7.Text = "";
}
private void timer1_Tick(object sender, EventArgs e)
{
if(second>=0)
{
label7.Text = second.ToString();
second--;
if (second == 0)
{
MessageBox.Show("时间到.请交试卷");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
int zhengQue=0;
int num;
string fuhao;
fuhao = comboBox1.SelectedItem.ToString();
num = Convert.ToInt32(textBox1 .Text );
for (int i = 0; i < num;i++ )
{
Label label1=GetLabel(panel2,i );
Label label2 = GetLabel(panel4,i);
TextBox tb = GetTextBox(panel6,i);
if ((Convert.ToInt32(label1.Text) + Convert.ToInt32(label2.Text))==Convert .ToInt32 (tb.Text ))
zhengQue++;
}
MessageBox.Show("共"+num.ToString ()+"题;"+"正确的"+zhengQue .ToString ()+"道");
}
private Label GetLabel(Panel panel, int labelIndex)
{
int index = 0;
foreach (Control ctrl in panel.Controls)
{
if (ctrl is Label)
{
if (labelIndex == index)
{
return (Label)ctrl;
}
}
}
}
private TextBox GetTextBox(Panel panel, int TextBoxIndex)
{
int index = 0;
foreach (Control ctrl in panel.Controls)
{
if (ctrl is TextBox )
{
if (TextBoxIndex == index)
{
return (TextBox )ctrl;
}
}
}
} }
}
{
int index = 0;
foreach (Control ctrl in panel.Controls)
{
if (ctrl is Label)
{
if (labelIndex == index)
{
return (Label)ctrl;
}
}
}
return null;
}