繁体   English   中英

如何在C#中的标签控件中动态添加标签?

[英]How to add label dynamically in tab control in C#?

我试图将已发现的结果输出到选项卡控件中的标签。 但是它根本不显示它们。 有人可以看看我的代码并告诉我我做错了什么吗? 谢谢。

private void btnSearch_Click(object sender, EventArgs e)
{
    int indexResult = 0;
    string title, id;
    double price;
    string a = txtSearch.Text;

    if (Xbox360.HaveGame(a, ref indexResult))
    {
        title = Xbox360.GetTitle()[indexResult];
        Results.SetTitle(title);
        id = Xbox360.GetId()[indexResult];
        Results.SetId(id);
        price = Xbox360.GetPrice()[indexResult];
        Results.SetPrice(price);
    }

    if (Ps3.HaveGame(a, ref indexResult))
    {
        title = Ps3.GetTitle()[indexResult];
        Results.SetTitle(title);
        id = Ps3.GetId()[indexResult];
        Results.SetId(id);
        price = Ps3.GetPrice()[indexResult];
        Results.SetPrice(price);
    }

    if (Wii.HaveGame(a, ref indexResult))
    {
        title = Wii.GetTitle()[indexResult];
        Results.SetTitle(title);
        id = Wii.GetId()[indexResult];
        Results.SetId(id);
        price = Wii.GetPrice()[indexResult];
        Results.SetPrice(price);
    }

    // Basically I am going to output the result from the array Results, however, 
    // here I just want to output a sample string 
    for (int i = 0; i < 3; i++)
    {
        Label resultLabel = new Label();
        resultLabel.Location = new Point(10, 7);
        resultLabel.Text = "output is here";
        this.Controls.Add(resultLabel);
    }
}

尝试将它们添加到要在其上显示它们的TabPage:

this.TabControl1.TabPages[0].Controls.Add(resultLabel);

您还应该考虑将遗产用于Wii,Ps3和Xbox类,那么您可以这样做:

foreach (GenericType console in consoleList)
{

    if (console.HaveGame(a, ref indexResult))
    {
        title = console.GetTitle()[indexResult];
        Results.SetTitle(title);
        id = console.GetId()[indexResult];
        Results.SetId(id);
        price = console.GetPrice()[indexResult];
        Results.SetPrice(price);
    }
}
 foreach (TabPage tab in myTabControl.TabPages)
 {
     Label lbl = new Label();
     lbl.Text = tab.Text;
     tab.Controls.Add(lbl);
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM