控制标记在转换后变为null

时间:2014-10-20 08:56:12

标签: c# .net winforms user-controls

当我将控件转换为其类型时,结果项的Tag属性为null,而原始控件的Tag属性保持不变。

public partial class SomethingAllocator : UserControl
{
    GlobalOptions g = Program.Core.GlobalOptions;
    public SomethingAllocator()
    {
        InitializeComponent();

    }

    private void SomethingAllocator_Load(object sender, EventArgs e)
    {
        ucsAlloc1.lbl_Name.Text = "S 1 - Primary";
       ucsAlloc2.lbl_Name.Text = "S 2";
        ucsAlloc3.lbl_Name.Text = "S 3";
       ucsAlloc4.lbl_Name.Text = "S 4";
       ucsAlloc5.lbl_Name.Text = "S 5";
        ucsAlloc6.lbl_Name.Text = "S 6";


        ucsAlloc1.Tag = 0;
        ucsAlloc2.Tag = 1;
      ucsAlloc3.Tag = 2;
        ucsAlloc4.Tag = 3;
       ucsAlloc5.Tag = 4;
        ucsAlloc6.Tag = 5;

        ucsAlloc1.cbx_DL.SelectedIndex = 3;
        ucsAlloc1.cbx_UL.SelectedIndex = 3;
        ucsAlloc2.cbx_DL.SelectedIndex = 3;
        ucsAlloc2.cbx_UL.SelectedIndex = 3;
        ucsAlloc3.cbx_DL.SelectedIndex = 3;
        ucsAlloc3.cbx_UL.SelectedIndex = 3;
        ucsAlloc4.cbx_DL.SelectedIndex = 3;
        ucsAlloc4.cbx_UL.SelectedIndex = 3;
        ucsAlloc5.cbx_DL.SelectedIndex = 3;
        ucsAlloc5.cbx_UL.SelectedIndex = 3;
        ucsAlloc6.cbx_DL.SelectedIndex = 3;
        ucsAlloc6.cbx_UL.SelectedIndex = 3;
        InitSs();



    }

    void ucsAlloc1_onComboIndexChanged(object sender, EventArgs e)
    {
        var cont = ((sender as Control).Parent as UCSAlloc);
        var selected = cont.comboBox1.SelectedIndex;
        if (selected == 0 && (int)cont.Tag == 0)
        {
            cont.comboBox1.SelectedIndex = cont.comboBox1.Items.IndexOf(Program.Core.Mission.AllocatedStations[0].Name);
            MessageBox.Show("****");
            return;
        }
        if (selected == 0)
        {
            Program.Core.Mission.AllocatedStations[(int)cont.Tag] = null;
            return;
        }


        foreach (var item in this.Controls.OfType<UCSAlloc>())
        {
            if (item != cont)
            {
                if (item.comboBox1.SelectedIndex == selected)
                {
                    cont.comboBox1.SelectedIndex = 0;
                    MessageBox.Show("***");
                    return;
                }
            }
        }
        var station = g.Stations.FirstOrDefault(x => x.Name == cont.comboBox1.Text);
        if (station == null)
            MessageBox.Show("***");
        else
        {
            var index = (int)cont.Tag;
            Program.Core.Mission.AllocatedStations[index] = station;
            cont.cbx_DL.SelectedIndex = cont.cbx_DL.Items.IndexOf(Program.Core.Mission.AllocatedStations[index].DlChannel.ToString());
            cont.cbx_UL.SelectedIndex = cont.cbx_UL.Items.IndexOf(Program.Core.Mission.AllocatedStations[index].UlChannel.ToString());
        }
    }

    public void InitSs()
    {
        g = Program.Core.GlobalOptions;
        ucsAlloc1.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged;
        ucsAlloc2.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged;
        ucsAlloc3.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged;
        ucsAlloc4.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged;
        ucsAlloc5.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged;
        ucsAlloc6.onComboIndexChanged -= ucsAlloc1_onComboIndexChanged;

        ucsAlloc1.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged;
        ucsAlloc2.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged;
        ucsAlloc3.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged;
        ucsAlloc4.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged;
        ucsAlloc5.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged;
        ucsAlloc6.cbx_DL.SelectedIndexChanged -= cbx_DL_SelectedIndexChanged;


        ucsAlloc1.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged;
        ucsAlloc2.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged;
        ucsAlloc3.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged;
        ucsAlloc4.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged;
        ucsAlloc5.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged;
        ucsAlloc6.cbx_UL.SelectedIndexChanged -= cbx_UL_SelectedIndexChanged;


        ucgsAlloc1.comboBox1.Items.Clear();
        ucgsAlloc2.comboBox1.Items.Clear();
        ucgsAlloc3.comboBox1.Items.Clear();
        ucgsAlloc4.comboBox1.Items.Clear();
        ucgsAlloc5.comboBox1.Items.Clear();
        ucgsAlloc6.comboBox1.Items.Clear();

        ucsAlloc1.comboBox1.Items.Add("None");
        ucsAlloc2.comboBox1.Items.Add("None");
        ucsAlloc3.comboBox1.Items.Add("None");
        ucsAlloc4.comboBox1.Items.Add("None");
        ucsAlloc5.comboBox1.Items.Add("None");
        ucsAlloc6.comboBox1.Items.Add("None");

        for (int i = 0; i < g.Stations.Count; i++)
        {
            var name = g.Stations[i].Name;
            ucsAlloc1.comboBox1.Items.Add(name);
            ucsAlloc2.comboBox1.Items.Add(name);
            ucsAlloc3.comboBox1.Items.Add(name);
            ucsAlloc4.comboBox1.Items.Add(name);
            ucsAlloc5.comboBox1.Items.Add(name);
            ucsAlloc6.comboBox1.Items.Add(name);
        }

        ucsAlloc1.comboBox1.SelectedIndex = 0;
        ucsAlloc2.comboBox1.SelectedIndex = 0;
        ucsAlloc3.comboBox1.SelectedIndex = 0;
        ucsAlloc4.comboBox1.SelectedIndex = 0;
        ucsAlloc5.comboBox1.SelectedIndex = 0;
        ucsAlloc6.comboBox1.SelectedIndex = 0;

        if (g.Stations.Count > 0)
            ucsAlloc1.comboBox1.SelectedIndex = 1;
        if (Program.Core.Mission != null)
        {
            for (int i = 0; i < Program.Core.Mission.AllocatedStations.Length; i++)
            {
                if (Program.Core.Mission.AllocatedStations[i] == null) continue;
                var loc = this.Controls.OfType < UCSAlloc>().FirstOrDefault(t => (int)t.Tag == i);//--------------null exception over the tag property
                if (loc != null)
                {
                    var item = Program.Core.Mission.AllocatedStations[i];
                    var nameIndex = loc.comboBox1.Items.IndexOf(item.Name.ToString());
                    if (nameIndex > -1)
                        loc.comboBox1.SelectedIndex = nameIndex;
                    loc.cbx_DL.SelectedIndex = loc.cbx_DL.Items.IndexOf(item.DlChannel.ToString());
                    loc.cbx_UL.SelectedIndex = loc.cbx_UL.Items.IndexOf(item.UlChannel.ToString());
                }
            }
        }


        ucsAlloc1.onComboIndexChanged += ucsAlloc1_onComboIndexChanged;
        ucsAlloc2.onComboIndexChanged += ucsAlloc1_onComboIndexChanged;
        ucsAlloc3.onComboIndexChanged += ucsAlloc1_onComboIndexChanged;
        ucsAlloc4.onComboIndexChanged += ucsAlloc1_onComboIndexChanged;
        ucsAlloc5.onComboIndexChanged += ucsAlloc1_onComboIndexChanged;
        ucsAlloc6.onComboIndexChanged += ucsAlloc1_onComboIndexChanged;

        ucsAlloc1.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged;
        ucsAlloc2.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged;
        ucsAlloc3.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged;
        ucsAlloc4.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged;
        ucsAlloc5.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged;
        ucsAlloc6.cbx_DL.SelectedIndexChanged += cbx_DL_SelectedIndexChanged;


        ucsAlloc1.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged;
        ucsAlloc2.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged;
        ucsAlloc3.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged;
        ucsAlloc4.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged;
        ucsAlloc5.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged;
        ucsAlloc6.cbx_UL.SelectedIndexChanged += cbx_UL_SelectedIndexChanged;

    }

    void cbx_UL_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ctrl = (sender as Control).Parent as UCSAlloc;          
        var number = ctrl.cbx_UL.Text;
        int chnl;
        if (int.TryParse(number, out chnl))
        {
            if (Program.Core.Mission.AllocatedStations[(int)ctrl.Tag] != null)////-------- null exception over tag. ctrl isnt null
                Program.Core.Mission.AllocatedStations[(int)ctrl.Tag].UlChannel = chnl;
        }



    }

    void cbx_DL_SelectedIndexChanged(object sender, EventArgs e)
    {
       var ctrl = (sender as Control).Parent as UCSAlloc;          
        var number = ctrl.cbx_DL.Text;
        int chnl;
        if (int.TryParse(number, out chnl))
        {
            if (Program.Core.Mission.AllocatedStations[(int)ctrl.Tag] != null)////-------- null exception over tag. ctrl isnt null
                Program.Core.Mission.AllocatedStations[(int)ctrl.Tag].DlChannel = chnl;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

Where中的情况有误:

.Where(item=>item.Tag==123)

默认情况下,对于引用类型,==运算符测试引用相等性。由于item.Tag的类型为Objectitem.Tag == 123会测试item.Tag123是否为同一个对象实例;但由于int是值类型,因此123 将框装入新的对象实例,因此它永远不会与item.Tag相同。相反,您应该使用Equals方法:

.Where(item=>item.Tag.Equals(123))

==运算符不同,Equals方法是虚方法,因此将根据调用它的实例使用不同的实现;在这种情况下,它将调用Int32.Equals方法,如果标记为123,则会正确返回true。


编辑:跟随您的问题编辑

问题在于:

FirstOrDefault(t => (int)t.Tag == i)

如果t.Tag为null,则对值类型的强制转换会抛出NullReferenceException。检查您是否确实已为Tag

的所有实例初始化UCSAlloc

答案 1 :(得分:0)

“crtl”本身在转换失败时(无论出于何种原因)为null,因为如果转换失败,使用“as”进行转换将返回null。