如何在c #WindowsFormsApplication中使用标记 [英] How do I use tags in c# WindowsFormsApplication

查看:62
本文介绍了如何在c #WindowsFormsApplication中使用标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充一些具有相同标签(例如1)的panel1的空文本框,其中empty。

我使用此代码:

< pre lang =c#> foreach (控制t panel1.Controls)
{
if (t.Tag.ToString()== 1&& t.Text ==
{
t.Text = empty;
}
}



我收到此错误:对象引用未设置为对象的实例。

可以有人帮忙吗?

解决方案

你应该采取不同的方式:



  foreach (控制c  panel1.Controls中)
{
TextBox tbx = c as TextBox;

if (tbx!= null && tbx.Tag! = null && tbx.Tag.ToString()== 1&& t.Text ==
{
tbx.Text = empty;
}
}





你这样做的原因是你只关心文本框。如果你有你的代码,它也会尝试对你页面上的任何标签,组合框,图片框等执行相同的方法。 tbx = c作为TextBox只会将tbx转换为文本框,如果c是要使用的文本框。否则tbx将为null,这是你在if语句中检查的第一件事。



你得到错误的原因是你有一个文本框没有标记属性集。如果未设置该属性,则无法在其上调用 ToString ,因为它是一个空对象。为了解决这个问题,你必须在执行 ToString()之前检查Tag是否为null。



出于学习目的,注意&& 是一种快捷方法也很重要,如果你只是使用& 这在语法上是正确的,它仍然会给你一个错误,因为它会评估整个if语句,而不仅仅是停在第一个返回的语句false。


您可以使用Linq简化提取某个Type的所有元素,并使用非null Tag属性:

  foreach (TextBox tbx  in  panel1.Controls.OfType< TextBox>()。其中​​(tb = >  tb.Tag!=  null )。ToList())
{
Console.WriteLine(tbx。 Tag.ToString());
}

如果您在表格/面板/用户控制等中拥有固定数量的某种控制类型,并且您需要经常访问所有这些控制,则可能值得 - 虽然要构建一次这些控件的集合,然后重新使用它:

 List< TextBox> tbList = panel1.Controls.OfType< TextBox>()。其中​​(tb => tb.Tag!= null)。ToList(); 


I want to fill some of empty textboxes that have same tags (like "1") of panel1 with "empty".
I use this code :

foreach (Control t in panel1.Controls)
            {
                if (t.Tag.ToString() == "1" && t.Text == "")
                {
                    t.Text = "empty";
                }
             }


I receive this error: Object reference not set to an instance of an object.
Can anyone help ,please ?

解决方案

You should do this a different way:

foreach (Control c in panel1.Controls)
{
    TextBox tbx = c as TextBox;

    if (tbx != null && tbx.Tag != null && tbx.Tag.ToString() == "1" && t.Text == "")
    {
        tbx.Text = "empty";
    }
}



The reason that you do it this way, is that you only care about textboxes. If you have your code, it would also try to perform the same method for any label, combobox, picturebox, etc that you have on your page. The tbx = c as TextBox will only turn tbx into a textbox if c is a textbox to being with. Otherwise tbx will be null, and that is the first thing you check in the if statement.

The reason you are getting the error is that you have a textbox on there that does not have the Tag property set. If the property isn't set, you can't call ToString on it because its a null object. In order to get around that, you have to check that Tag is not null BEFORE you do the ToString().

Just for learning purposes, its also important to note that the && is a shortcut method, if you just use & which would be syntatically correct, it would still give you an error because it would evaluate the entire if statement, not just stop at the first one that returned false.


You can simplify extracting all the elements of a certain Type, and with a non-null Tag Property, using Linq:

foreach (TextBox tbx in panel1.Controls.OfType<TextBox>().Where(tb => tb.Tag != null).ToList())
{
    Console.WriteLine(tbx.Tag.ToString());
}

If you have a fixed number of a certain Type of Control in a Form/Panel/UserControl, etc., and you need to access all of them frequently, it may be worth-while to build a collection of those Controls once, and then re-use it:

List<TextBox> tbList = panel1.Controls.OfType<TextBox>().Where(tb => tb.Tag != null).ToList();


这篇关于如何在c #WindowsFormsApplication中使用标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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