gpt4 book ai didi

C# WPF Mysql 组合框重复值

转载 作者:行者123 更新时间:2023-11-29 00:06:40 25 4
gpt4 key购买 nike

我有一个包含 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 这不是完美的选择,因为当我使用键盘中的箭头时,字符/用户包含的值不会更新。

最佳答案

您想调用 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 属性,您可以使用它来抓取当前项目。

关于C# WPF Mysql 组合框重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27187569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com