0
votes

I have a form that has 2 comboboxes. The first combobox (cb_CharacterName) which contains the character/user name and is loaded when the form opens, this works fine.

When a name is choosen in the cb_CharacterName the other combobox (Talent_Name) checks the (cb_CharacterName) combobox for the name and loads the talents the character/user has, this is also works fine.

However when a new character/user has been choosen in the cb_CharacterName the Talent_Name combobox does not remove the talents from the previous character, and just adds more to the list. At the same time if i select the same character twice i get duplicates.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;

namespace Dark_Heresy
{
    /// <summary>
    /// Interaction logic for Character.xaml
    /// </summary>
    public partial class Character : Window
    {
        public Character()
        {
            InitializeComponent();
        }

        private void character_name_loader(object sender, RoutedEventArgs e)
        {
            string constring = "datasource= localhost; port=3306; username=root; password=MyPass;";
            string Query = "SELECT Name_ FROM dark_heresy.character_";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDatabase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDatabase.ExecuteReader();

                while (myReader.Read())
                {
                    string charactername = myReader.GetString("Name_");
                    cb_CharacterName.Items.Add(charactername);
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: \r\n" + ex);
            }
        }

        private void cb_CharacterName_DropDownClosed(object sender, EventArgs e)
        {
            string constring = "datasource = localhost; port = 3306; username = root; password = MyPass;";
            string Query = "SELECT * FROM dark_heresy.character_ WHERE Name_='" + cb_CharacterName.Text + "' ;";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                while (myReader.Read())
                {
                    string career = myReader.GetString("Class");
                    string world = myReader.GetString("World_Type");
                    string strength = myReader.GetInt32("Str").ToString();
                    string weaponskill = myReader.GetInt32("WS").ToString();
                    string ballisticskill = myReader.GetInt32("BS").ToString();
                    string fellowship = myReader.GetInt32("Fel").ToString();
                    string perception = myReader.GetInt32("Per").ToString();
                    string intelligence = myReader.GetInt32("Int_").ToString();
                    string agility = myReader.GetInt32("Agi").ToString();
                    string willpower = myReader.GetInt32("WP").ToString();
                    string toughness = myReader.GetInt32("Tough").ToString();


                    TextCareer.Text = career;
                    TextWorld.Text = world;
                    TextStrength.Text = strength;
                    TextWeaponskill.Text = weaponskill;
                    TextBallisticskill.Text = ballisticskill;
                    TextFellowship.Text = fellowship;
                    TextPerception.Text = perception;
                    TextIntelligence.Text = intelligence;
                    TextAgility.Text = agility;
                    TextWillpower.Text = willpower;
                    TextToughness.Text = toughness;

                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: \r\n" + ex);
            }
        }

        private void cb_Talent_NameDropDownOpen(object sender, EventArgs e)
        {
            string constring = "datasource= localhost; port=3306; username=root; password=MyPass;";
            string Query = "SELECT Talent_Name FROM dark_heresy.learned_talents WHERE Character_Name='" + cb_CharacterName.Text + "' ;";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDatabase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDatabase.ExecuteReader();

                while (myReader.Read())
                {
                    string talent_name = myReader.GetString("Talent_Name");
                    Talent_Name.Items.Add(talent_name);
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: \r\n" + ex);
            }
        }

        private void cb_Talent_Name_DropDownClosed(object sender, EventArgs e)
        {
            string constring = "datasource = localhost; port = 3306; username = root; password = MyPass;";
            string Query = "SElECT learned_talents.Talent_Name , talents.Description FROM dark_heresy.learned_talents, dark_heresy.talents WHERE learned_talents.Talent_Name = talents.TalentName AND learned_talents.Character_Name = '" + cb_CharacterName.Text + "';";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                while (myReader.Read())
                {
                    string talents_description = myReader.GetString("Description");

                    Talents_Description.Text = talents_description;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: \r\n" + ex);
            }
        }

    }
}

How can i make the Talent_Name combobox each time a new character/user or the samme has been choosen to flush the old informations and add the new ones in? i presume this would also remove the duplications problem.

Also, does WPF have a SelectedIndex? right now i am using DropdownClosed which is not the perfect choice, since when i use the arrows in the keyboard the values the character/user contains does not gets updated.

1

1 Answers

1
votes

You want to call the Clear() method:

        try
        {
            conDataBase.Open();
            myReader = cmdDatabase.ExecuteReader();

            // You're missing this line!
            Talent_Name.Items.Clear();

            while (myReader.Read())
            {
                string talent_name = myReader.GetString("Talent_Name");
                Talent_Name.Items.Add(talent_name);
            }
        }

        catch (Exception ex)
        {
            MessageBox.Show("Error: \r\n" + ex);
        }

There should be a SelectedItem property you can use too grab the current item.