如何将修饰符用于动态创建的控件? [英] How can use modifiers for dynamically created controls?

查看:73
本文介绍了如何将修饰符用于动态创建的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有组合框,并且我使用selectedIndexchanged事件,请注意以下代码.如果我选择索引值一,它将创建一个成人标签,但如果我选择索引值二,则应显示一个孩子"标签,同时不显示成人"标签.我该如何解决这个问题?

Hi,
I have combo box, and i use selectedindexchanged event,note the following codes. if i choose, index value one it will create a a label adult, but if i choose index value as two it should show a label ''child'' and parallelly it shouldn''t show a label ''Adult''. How can i solve this problem?

private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAdults.SelectedIndex == 1)
            {
                Label lblobj = new Label();
                lblobj.Text = "Adult";
                lblobj.Font = new Font(lblobj.Font, lblobj.Font.Style | FontStyle.Bold);
                lblobj.BackColor = Color.Transparent;
                lblobj.ForeColor = Color.White;
                lblobj.AutoSize = true;
                lblobj.Location = new Point(270, 110);
                //this.Controls.Add(lblobj);
                groupBox1.Controls.Add(lblobj);
            }
            else if (cmbAdults.SelectedIndex == 2)
            {
                Label lblobj1 = new Label();
                lblobj1.Text = "child";
                lblobj1.Font = new Font(lblobj1.Font, lblobj1.Font.Style | FontStyle.Bold);
                lblobj1.BackColor = Color.Transparent;
                lblobj1.ForeColor = Color.White;
                lblobj1.AutoSize = true;
                lblobj1.Location = new Point(270,140);
                groupBox1.Controls.Add(lblobj1);
            }
}



我不能在儿童"标签中使用lblobj,在成人"标签中使用lblobj1..



i can''t use lblobj in ''Child'' label and lblobj1 in ''Adult'' label..

推荐答案

private void cmbAdults_SelectedIndexChanged(object sender,EventArgs e)
{
标签lblobj;

如果(cmbAdults.SelectedIndex == 1)
{
lblobj = new Label();

lblobj.Text =成人";
lblobj.Font =新字体(lblobj.Font,lblobj.Font.Style | FontStyle.Bold);
lblobj.BackColor = Color.Transparent;
lblobj.ForeColor = Color.White;
lblobj.AutoSize = true;
lblobj.Location =新Point(270,110);
//this.Controls.Add(lblobj);
groupBox1.Controls.Add(lblobj);
}
否则,如果(cmbAdults.SelectedIndex == 2)
{
标签lblobj1 =新Label();
lblobj1.Text ="child";
lblobj1.Font =新字体(lblobj1.Font,lblobj1.Font.Style | FontStyle.Bold);
lblobj1.BackColor = Color.Transparent;
lblobj1.ForeColor = Color.White;
lblobj1.AutoSize = true;
lblobj1.Location =新点(270,140);
groupBox1.Controls.Remove(lblobj)
//或
if(lblobj!= null)
lblObj.Visible = false;
groupBox1.Controls.Add(lblobj1);
}
}
private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
{
Label lblobj ;

if (cmbAdults.SelectedIndex == 1)
{
lblobj = new Label();

lblobj.Text = "Adult";
lblobj.Font = new Font(lblobj.Font, lblobj.Font.Style | FontStyle.Bold);
lblobj.BackColor = Color.Transparent;
lblobj.ForeColor = Color.White;
lblobj.AutoSize = true;
lblobj.Location = new Point(270, 110);
//this.Controls.Add(lblobj);
groupBox1.Controls.Add(lblobj);
}
else if (cmbAdults.SelectedIndex == 2)
{
Label lblobj1 = new Label();
lblobj1.Text = "child";
lblobj1.Font = new Font(lblobj1.Font, lblobj1.Font.Style | FontStyle.Bold);
lblobj1.BackColor = Color.Transparent;
lblobj1.ForeColor = Color.White;
lblobj1.AutoSize = true;
lblobj1.Location = new Point(270,140);
groupBox1.Controls.Remove(lblobj)
//Or
if(lblobj != null)
lblObj.Visible = false;
groupBox1.Controls.Add(lblobj1);
}
}


设置每个标签的名称属性.
然后,在添加另一个对象之前,先使用containsKey和removeByKey方法删除这些对象.

示例用法如下所示-
Set name property of each label.
Then use the containsKey and removeByKey methods to remove the objects prior to adding another.

sample usage is shown below -
lblobj.Name = "lblAdult"; // "lblChild"
// in your  if(cmbAdults.SelectedIndex == 2) block 
if(groupBox1.Controls.containsKey("lblAdult")) {
 groupBox1.Controls.removeByKey("lblAdult");
}
// similarly for  if(cmbAdults.SelectedIndex == 1) block


谢谢&问候,
Niral Soni


Thanks & Regards,
Niral Soni


您不能从"else if"块引用lblobj,因为它是在"if"块的范围内定义的,相反,您不能在其中引用lblobj1在"else if"块的范围内定义的"if"块

为了解决这个问题,您可以简单地将decleration(而不是实例化)移到if-elseif
上方
例如

You cant reference lblobj from the ''else if'' block as it is defined within the scope of the ''if'' block and, conversely, you cannot reference lblobj1 within the ''if'' block as it is defined within the scope of the ''else if'' block

to remedy this you could simply move the decleration (not the instantiation) above the if-elseif

e.g.

private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
{
    Label lblobj;
    Label lblobj1;

    if (cmbAdults.SelectedIndex == 1)
    {
        lblobj = new Label();
        lblobj.Text = "Adult";
        lblobj.Font = new Font(lblobj.Font, lblobj.Font.Style | FontStyle.Bold);
        lblobj.BackColor = Color.Transparent;
        lblobj.ForeColor = Color.White;
        lblobj.AutoSize = true;
        lblobj.Location = new Point(270, 110);
        groupBox1.Controls.Add(lblobj);
    }
    else if (cmbAdults.SelectedIndex == 2)
    {
        lblobj1 = new Label();
        lblobj1.Text = "child";
        lblobj1.Font = new Font(lblobj1.Font, lblobj1.Font.Style | FontStyle.Bold);
        lblobj1.BackColor = Color.Transparent;
        lblobj1.ForeColor = Color.White;
        lblobj1.AutoSize = true;
        lblobj1.Location = new Point(270,140);
        groupBox1.Controls.Add(lblobj1);
    }
}



但是,这仍然是解决此问题的糟糕方法.

为什么不简单地在表单上放置一个标签,并将其文本设置为",然后将其简化为



However, this is still a bad way to go about this.

Why not simply have a single label, already positioned on your form with text set to "", then the simplifies to

private void cmbAdults_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cmbAdults.SelectedIndex == 1)
    {
        lblobj.Text = "Adult";
    }
    else if (cmbAdults.SelectedIndex == 2)
    {
        lblobj.Text = "Child";                
    }
}


这篇关于如何将修饰符用于动态创建的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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