[英]How to delete an item row in C# in a 2D array in forms
您好,我想知道如何使用按鈕從 C# 表單中的數組中刪除一行,就像您寫下要刪除的行#,然后按下按鈕將從列表中刪除該項目。 這個想法是使用“按鈕 2”刪除一行,該按鈕讀取寫在“文本框”中的數字。 以及如果該行不存在如何顯示警告消息。 我不能使用“list<>”或“grid thing”。 謝謝你們中的任何一個人可以借給我。 `公共部分類Form1:Form {
int pos = 0;
int counter = 0;
string[,] sales = new String[50, 5];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string product;
double price, subtotal, total;
int qty;
textBox1.BackColor = Color.White;
if (textBox1.Text == "")
{
textBox1.BackColor = Color.Red;
textBox1.Focus();
MessageBox.Show("MISSING PRODUCT!!!");
return;
}
textBox2.BackColor = Color.White;
if (textBox2.Text == "")
{
textBox2.BackColor = Color.Red;
textBox2.Focus();
MessageBox.Show("MISSING PRODUCT!!!");
return;
}
textBox3.BackColor = Color.White;
if (textBox3.Text == "")
{
textBox3.BackColor = Color.Red;
textBox3.Focus();
MessageBox.Show("MISSING PRODUCT!!!");
return;
}
product = textBox1.Text;
price = double.Parse(textBox2.Text);
qty = int.Parse(textBox3.Text);
subtotal = price * qty;
sales[pos, 0] = pos.ToString();
sales[pos, 1] = product;
sales[pos, 2] = price.ToString();
sales[pos, 3] = qty.ToString();
sales[pos, 4] = subtotal.ToString();
pos++;
textBox5.Text = "";
total = 0;
for (int i = 0; i < sales.GetLength(0); i++)
{
textBox5.Text += sales[i, 0] + " " + sales[i, 1] + " " + sales[i, 2] + " " + sales[i, 3] + " " + sales[i, 4] + " " + "\r\n";
//<>
if (sales[i, 4] != null)
{
total += int.Parse(sales[i, 4]);
}
}
textBox4.Text = total.ToString();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
// what to put here
}
}
`
我將在這里給你兩個答案,因為你不清楚是否需要使用數組而不是List<>
。 我也有點不確定你到底要什么,因為你沒有給出任何你希望輸出的例子,或者類似的東西,所以如果我完全錯過了標記,我深表歉意。 只要讓我知道,我會更改我的答案以更恰當地匹配您的要求:)
private void button2_Click(object sender, EventArgs e)
{
//Checks to see if the desired row to delete exists
if(sales[pos][0] is null)
{
//Lets the user know the index is already empty
MessageBox.Show("Index has no product listed.");
return;
}
//Loops through each item at the pos index
for(int i = 0; i < sales.GetLength(1); i++)
{
//remove
sales[pos][i] = null;
}
/*******************************************************
* I'm unsure if you would want to drop the index of all
* of the arrays with a larger index number down one, so
* this next bit of code will do this, but if you don't
* want this functionality, just ignore this.
********************************************************/
//Loops through every index from pos to the second to last index
for(int i = pos; i < sales.GetLength(0) - 1; i++)
{
//Loops through every index of a product
for(int j = 0; j < sales.GetLength(1); j++)
{
//Sets current row to the value in the next
sales[i][j] = sales[i+1][j];
}
}
}
List<>
/*****************************************************
* I've changed the name of the variable to pascal case
* as this is Microsoft's documentation standard.
* This also shows how you would need to change the
* declaration of the Sales object.
*****************************************************/
List<List<string>> Sales = new List<List<string>>();
private void button2_Click(object sender, EventArgs e)
{
//Checks if the index is out of bounds of the list.
if(pos > Sales.Count() - 1)
{
//Lets the user know the index out of bounds
MessageBox.Show("Index is out of bounds.");
return;
}
//Remove the product at the selected index
Sales.RemoveAt(pos);
}
我注意到您似乎正在嘗試列出要出售的產品。 我建議您創建一個Product
類,其中包含產品所具有的所有信息。 一個快速的模擬可能看起來像這樣:
public class Product
{
/**********************************************************
* If you are unfamiliar with how properties work,
* I recommend looking into them more, but quick, simplified
* explanation is they are just variables you access from
* other classes that have access to this one by
* saying, in this case, Product.AttributeOne, and you
* can even set it by writing Product.AttributeOne = "foo";
**********************************************************/
public string AttributeOne { get; set; }
public string AttributeTwo { get; set; }
public string AttributeThree { get; set; }
public string AttributeFour { get; set; }
public string AttributeFive { get; set; }
}
使用此類可以簡化您擁有的其他代碼,並使您的數組/列表成為一維的。 我強烈建議將產品作為對象,就像它可以用更少的代碼為您提供更多功能一樣。 我將向您展示button1_Click
和button2_Click()
的代碼在此更改后的樣子
public partial class Form1 : Form
{
int pos = 0;
int counter = 0;
List<Product> Sales = new List<Product>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string product;
double price, subtotal, total;
int qty;
textBox1.BackColor = Color.White;
if (textBox1.Text == "")
{
textBox1.BackColor = Color.Red;
textBox1.Focus();
MessageBox.Show("MISSING PRODUCT!!!");
return;
}
textBox2.BackColor = Color.White;
if (textBox2.Text == "")
{
textBox2.BackColor = Color.Red;
textBox2.Focus();
MessageBox.Show("MISSING PRODUCT!!!");
return;
}
textBox3.BackColor = Color.White;
if (textBox3.Text == "")
{
textBox3.BackColor = Color.Red;
textBox3.Focus();
MessageBox.Show("MISSING PRODUCT!!!");
return;
}
product = textBox1.Text;
price = double.Parse(textBox2.Text);
qty = int.Parse(textBox3.Text);
subtotal = price * qty;
Sales.ElementAt(pos).AttributeOne = pos.ToString();
Sales.ElementAt(pos).AttributeTwo = product;
Sales.ElementAt(pos).AttributeThree = price.ToString();
Sales.ElementAt(pos).AttributeFour = qty.ToString();
Sales.ElementAt(pos).AttributeFive = subtotal.ToString();
pos++;
textBox5.Text = "";
total = 0;
for (int i = 0; i < Sales.Count; i++)
{
textBox5.Text += Sales.ElementAt(i).AttributeOne + " " + Sales.ElementAt(i).AttributeTwo + " " + Sales.ElementAt(i).AttributeThree + " " + Sales.ElementAt(i).AttributeFour + " " + Sales.ElementAt(i).AttributeFive + " " + "\r\n";
//<>
if (Sales.ElementAt(i).AttributeFive != null)
{
total += int.Parse(sales[i, 4]);
}
}
textBox4.Text = total.ToString();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
//Checks if the index is out of bounds of the list.
if(pos > Sales.Count() - 1)
{
//Lets the user know the index out of bounds
MessageBox.Show("Index is out of bounds.");
return;
}
//Remove the product at the selected index
Sales.RemoveAt(pos);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.