when i cast a control to its type the Tag
property of the resulted item is null while the Tag
property of the original control is unchanged.
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;
}
}
}