リストボックスにデータベース値を追加しようとしていて、doubleclickイベントを使用してリストアアイテムを削除しようとしています。リストボックス内のアイテムを削除したいのですが、そのアイテムの価格はtxtTotalに差し引かれます。テキスト。例えば。私はリストボックス名 "Package1"に299の価格で商品を追加し、300の価格で別の商品名 "Package2"を追加します。 "Package1"を削除すると、txtTotalは299の数を減らし、300になります。私は私があなたの供給コードに興味のあるいくつかのポイントを説明することを願って、私は道でMySQLを使用していリストボックスのアイテムを削除する方法C#
void fillCombo()
{
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbladvance;";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDB = new MySqlCommand(Query, con);
MySqlDataReader myReader;
try
{
con.Open();
myReader = cmdDB.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString("Names");
comboBox1.Items.Add(sName);
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbladvance where Names='" + comboBox1.Text + "' ;";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDB = new MySqlCommand(Query, con);
MySqlDataReader myReader;
try
{
con.Open();
myReader = cmdDB.ExecuteReader();
while (myReader.Read())
{
double sPrice = myReader.GetDouble("Price");
string sDesc = myReader.GetString("Description");
txtPrice.Text = sPrice.ToString();
txtDesc.Text = sDesc;
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
int total;
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(comboBox1.Text);
int num1 = int.Parse(txtPrice.Text);
int num2 = int.Parse(txtTotal.Text);
total = num1 + num2;
txtTotal.Text = total.ToString();
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbladvance;";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDB = new MySqlCommand(Query, con);
MySqlDataReader myReader;
con.Open();
myReader = cmdDB.ExecuteReader();
while (myReader.Read())
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string sName = myReader.GetString("Names");
string removelistitem = sName;
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
listBox1.Items.RemoveAt(n);
}
}
}
con.Close();
}
、事前に感謝:)