【问题标题】:C# WPF Mysql combobox duplicates valuesC# WPF Mysql 组合框重复值
【发布时间】:2014-11-28 11:20:11
【问题描述】:

我有一个包含 2 个组合框的表单。第一个组合框 (cb_CharacterName) 包含角色/用户名并在表单打开时加载,这很好用。

当在 cb_CharacterName 中选择名称时,另一个组合框 (Talent_Name) 会检查 (cb_CharacterName) 组合框的名称并加载角色/用户拥有的才能,这也可以正常工作。

但是,当在 cb_CharacterName 中选择了新角色/用户时,Talent_Name 组合框不会从前一个角色中删除天赋,而只会将更多天赋添加到列表中。同时,如果我两次选择相同的字符,我会得到重复。

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);
            }
        }

    }
}

如何在每次选择新角色/用户或同名时创建 Talent_Name 组合框来刷新旧信息并添加新信息?我想这也会消除重复问题。

另外,WPF 有 SelectedIndex 吗?现在我正在使用 DropdownClosed 这不是完美的选择,因为当我使用键盘中的箭头时,字符/用户包含的值不会更新。

【问题讨论】:

    标签: c# mysql wpf combobox duplicates


    【解决方案1】:

    你想调用 Clear() 方法:

            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);
            }
    

    应该有一个 SelectedItem 属性,您也可以使用它来抓取当前项目。

    【讨论】:

    • Jep 它会清除它,但它也会在单击它后立即删除选定的 Talent_name,这意味着它只会显示描述。我可以以某种方式保持选定的 Talent_name 显示在组合框中,然后按下它就会清除它吗?我一直在搜索 SelectedItem,但似乎找不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2018-01-10
    • 2011-07-09
    相关资源
    最近更新 更多