Member Avatar for Erwin_Rosyid

So I code a program that could store data inside a list of object. I linked it with each combobox so I could input the values inside the list correctly. But there are several bugs that disturbs me. Could I get hint of what to fix and what to modify?

Bug 1. Personal Data(tab0) Date, Address, and Job description turned null if I clicked the Save button on 3 different tabs
Bug 2. If I enable the date, It sometimes works - sometimes i got a nullArgument error

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUITabTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Activate the employee data class
        DataTemplate employee = new DataTemplate();

        //Create a dynamic list to hold the data
        List<DataTemplate> EmployeeData = new List<DataTemplate>();

        //Create a safety measure against errors
        int safety = 0;

    #region "UI"
        private void profileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (SelfName.Visible)
            {
                SelfName.Visible = false;
            }
            else
            {
                SelfName.Visible = true;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                ChildJob.Text = "Student";
            }
            else
            {
                ChildJob.Text = "Working";
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
            toolStripStatusLabel1.Text = "Access Personal Data";
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;
            toolStripStatusLabel1.Text = "Access Parent Data";
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 2;
            toolStripStatusLabel1.Text = "Access Spouse Data";
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 3;
            toolStripStatusLabel1.Text = "Access Child Data";
        }

        private void selfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
            toolStripStatusLabel1.Text = "Access Personal Data";
        }

        private void parrentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;
            toolStripStatusLabel1.Text = "Access Parent Data";
        }

        private void istriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 3;
            toolStripStatusLabel1.Text = "Access Child Data";
        }

        private void anakToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 2;
            toolStripStatusLabel1.Text = "Access Spouse Data";
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                toolStripStatusLabel1.Text = "Access Personal Data";
            }

            else if (tabControl1.SelectedIndex == 1)
            {
                toolStripStatusLabel1.Text = "Access Parent Data";
            }

            else if (tabControl1.SelectedIndex == 2)
            {
                toolStripStatusLabel1.Text = "Access Spouse Data";
            }

            else if (tabControl1.SelectedIndex == 3)
            {
                toolStripStatusLabel1.Text = "Access Child Data";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
#endregion
    #region "Self Methods"
        private int CalcSelfAge()
        {
            DateTime bday = SelfDate.Value;

            DateTime now = DateTime.Today;
            int age = now.Year - bday.Year;
            if (bday > now.AddYears(-age)) age--;
            return age;
        }

        private void SelfDataEntry()
        {
            DataTemplate employ = new DataTemplate();
            employ.SelfName = SelfName.Text;
            employ.SelfDate = SelfDate.Text.ToString();
            employ.SelfAge = CalcSelfAge();
            employ.SelfAddress = SelfAddress.Text;
            employ.SelfJob = SelfJob.Text;

            EmployeeData.Add(employ);
        }

        private void comboSelfName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelfName.Text = EmployeeData[comboSelfName.SelectedIndex].SelfName;
            //SelfDate.Value = DateTime.ParseExact(EmployeeData[comboSelfName.SelectedIndex].SelfDate, "dd MMMM yyyy", null);
            SelfAge.Text = EmployeeData[comboSelfName.SelectedIndex].SelfAge.ToString();
            SelfAddress.Text = EmployeeData[comboSelfName.SelectedIndex].SelfAddress;
            SelfJob.Text = EmployeeData[comboSelfName.SelectedIndex].SelfJob;
        }

        private void comboBoxManip(string value)
        {
            comboSelfName.Items.Add(value);
            comboBoxParent.Items.Add(value);
            comboBoxSpouse.Items.Add(value);
            comboBoxChild.Items.Add(value);
        }

        private void button1_Click(object sender, EventArgs e)
        {
                SelfDataEntry();
                comboBoxManip(SelfName.Text);
                safety = 0;
                SelfName.Clear();
                SelfDate.Value = DateTime.Today;
                SelfAge.Clear();
                SelfAddress.Clear();
                SelfJob.Clear();
        }

        private void SelfDate_ValueChanged(object sender, EventArgs e)
        {
            CalcSelfAge();
        }
#endregion
    #region "Parent Methods"
        private int CalcFatherAge()
        {
            DateTime bday = FatherDate.Value;

            DateTime now = DateTime.Today;
            int age = now.Year - bday.Year;
            if (bday > now.AddYears(-age)) age--;
            return age;
        }

        private int CalcMotherAge()
        {
            DateTime bday = MotherDate.Value;

            DateTime now = DateTime.Today;
            int age = now.Year - bday.Year;
            if (bday > now.AddYears(-age)) age--;
            return age;
        }

        private void ParentDataEntry()
        {
            DataTemplate employ = new DataTemplate();
            employ.FatherName = FatherName.Text;
            employ.FatherDate = FatherDate.Text.ToString();
            employ.FatherAge = CalcFatherAge();
            employ.FatherAddress = FatherAddress.Text;

            employ.MotherName = MotherName.Text;
            employ.MotherDate = MotherDate.Text.ToString();
            employ.MotherAge = CalcMotherAge();
            employ.MotherAddress = MotherAddress.Text;

            EmployeeData.Add(employ);
        }

        private void comboBoxParent_SelectedIndexChanged(object sender, EventArgs e)
        {
            FatherName.Text = EmployeeData[comboBoxParent.SelectedIndex].FatherName;
            FatherAge.Text = EmployeeData[comboBoxParent.SelectedIndex].FatherAge.ToString();
            FatherDate.Value = DateTime.ParseExact(EmployeeData[comboBoxParent.SelectedIndex].FatherDate, "dd MMMM yyyy", null);
            FatherAddress.Text = EmployeeData[comboBoxParent.SelectedIndex].FatherAddress;

            MotherName.Text = EmployeeData[comboBoxParent.SelectedIndex].MotherName;
            MotherDate.Value = DateTime.ParseExact(EmployeeData[comboBoxParent.SelectedIndex].MotherDate, "dd MMMM yyyy", null);
            MotherAge.Text = EmployeeData[comboBoxParent.SelectedIndex].MotherAge.ToString();
            MotherAddress.Text = EmployeeData[comboBoxParent.SelectedIndex].MotherAddress;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ParentDataEntry();
            FatherName.Clear();
            FatherAge.Clear();
            FatherDate.Value = DateTime.Today;
            FatherAddress.Clear();

            MotherName.Clear();
            MotherDate.Value = DateTime.Today;
            MotherAge.Clear();
            MotherAddress.Clear();
            safety++;
        }

        private void FatherDate_ValueChanged(object sender, EventArgs e)
        {
            CalcFatherAge();
        }

        private void MotherDate_ValueChanged(object sender, EventArgs e)
        {
            CalcMotherAge();
        }
#endregion
    #region "Spouse Methods"
        private int CalcSpouseAge()
        {
            DateTime bday = SpouseDate.Value;

            DateTime now = DateTime.Today;
            int age = now.Year - bday.Year;
            if (bday > now.AddYears(-age)) age--;
            return age;
        }

        private void SpouseDataEntry()
        {
            DataTemplate employ = new DataTemplate();
            employ.SpouseName = SpouseName.Text;
            employ.SpouseDate = SpouseDate.Text.ToString();
            employ.SpouseAge = CalcSpouseAge();
            employ.SpouseAddress = SpouseAddress.Text;
            employ.SpouseJob = SpouseJob.Text;

            EmployeeData.Add(employ);
        }

       private void comboBoxSpouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            SpouseName.Text = EmployeeData[comboBoxSpouse.SelectedIndex].SpouseName;
            //SpouseDate.Value = DateTime.ParseExact(SpouseData[comboBoxSpouse.SelectedIndex].SpouseDate, "dd MMMM yyyy", null);
            SpouseAge.Text = EmployeeData[comboBoxSpouse.SelectedIndex].SpouseAge.ToString();
            SpouseAddress.Text = EmployeeData[comboBoxSpouse.SelectedIndex].SpouseAddress;
            SpouseJob.Text = EmployeeData[comboBoxSpouse.SelectedIndex].SpouseJob;
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            SpouseDataEntry();
            SpouseName.Clear();
            SpouseDate.Value = DateTime.Today;
            SpouseAge.Clear();
            SpouseAddress.Clear();
            SpouseJob.Clear();
            safety++;
        }

        private void SpouseDate_ValueChanged(object sender, EventArgs e)
        {
            CalcSpouseAge();
        }
    #endregion
    #region "Child Methods"
        private int CalcChildAge()
        {
            DateTime bday = ChildDate.Value;

            DateTime now = DateTime.Today;
            int age = now.Year - bday.Year;
            if (bday > now.AddYears(-age)) age--;
            return age;
        }

        private void ChildDataEntry()
        {
            DataTemplate employ = new DataTemplate();
            employ.ChildName = ChildName.Text;
            employ.ChildDate = ChildDate.Text.ToString();
            employ.ChildAge = CalcChildAge();
            employ.ChildJob = ChildJob.Text;

            EmployeeData.Add(employ);
        }

        private void comboBoxChild_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChildName.Text = EmployeeData[comboBoxChild.SelectedIndex].ChildName;
            ChildDate.Value = DateTime.ParseExact(EmployeeData[comboBoxChild.SelectedIndex].ChildDate, "dd MMMM yyyy", null);
            ChildAge.Text = EmployeeData[comboBoxChild.SelectedIndex].ChildAge.ToString();
            ChildJob.Text = EmployeeData[comboBoxChild.SelectedIndex].ChildJob;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ChildDataEntry();
            ChildName.Clear();
            ChildDate.Value = DateTime.Today;
            ChildAge.Clear();
            ChildJob.Clear();
            safety++;
        }

        private void ChildDate_ValueChanged(object sender, EventArgs e)
        {
            CalcChildAge();
        }
        #endregion
    }
}

Since we don't have the actual forms it's hard to tell what the problem is.

Member Avatar for Erwin_Rosyid

Ah, sorry about that. Here is the Link to the project with forms Since compressed archive is forbidden in DaniWeb attachment system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.