目前我想要的一切都很好。但我正在尝试清理它,并在使用泛型类时寻找一些建议。我想取消void update,void add和void delete并将其移动到另一个类,然后在需要时在我的表单中调用它。我的猜测是尝试使用像_delete然后getter和setter这样的东西,但每当我尝试我会得到错误,因为我不能识别listView1。有人能指出我正确的方向吗?我的代码如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace cartest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
listView1.View = View.Details;
listView1.FullRowSelect = true;
listView1.Columns.Add("Make", 60);
listView1.Columns.Add("Model", 60);
listView1.Columns.Add("Color", 50);
listView1.Columns.Add("Miles", 50);
listView1.Columns.Add("Condition", 90);
listView1.Columns.Add("Interior", 60);
listView1.Columns.Add("Highway MPG", 90);
listView1.Columns.Add("City MPG", 90);
listView1.Columns.Add("Price", 50);
}
private void add(string make, string model, string color, string miles, string condition, string interior, string highway, string city, string price)
{
string[] row = { make, model, color, miles, condition, interior, highway, city, price }; //adding rows to list
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);
}
private void update()
{
listView1.SelectedItems[0].SubItems[0].Text = textBoxMake.Text;
listView1.SelectedItems[0].SubItems[0].Text = textBoxModel.Text;
listView1.SelectedItems[0].SubItems[0].Text = textBoxColor.Text;
listView1.SelectedItems[0].SubItems[0].Text = textBoxMiles.Text;
listView1.SelectedItems[0].SubItems[0].Text = textBoxCondition.Text;
listView1.SelectedItems[0].SubItems[0].Text = textBoxInterior.Text;
listView1.SelectedItems[0].SubItems[0].Text = textBoxHighway.Text;
listView1.SelectedItems[0].SubItems[0].Text = textBoxCity.Text;
listView1.SelectedItems[0].SubItems[0].Text = textBoxPrice.Text;
//After you update an item it clears the text
textBoxMake.Text = ""; textBoxModel.Text = ""; textBoxColor.Text = ""; textBoxMiles.Text = ""; textBoxCondition.Text = ""; textBoxInterior.Text = ""; textBoxHighway.Text = ""; textBoxCity.Text = ""; textBoxPrice.Text = "";
}
//delete from list
private void delete()
{
if (MessageBox.Show("Are you sure you want to delete this item?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
//After you delete it clears text
textBoxMake.Text = ""; textBoxModel.Text = ""; textBoxColor.Text = ""; textBoxMiles.Text = ""; textBoxCondition.Text = ""; textBoxInterior.Text = ""; textBoxHighway.Text = ""; textBoxCity.Text = ""; textBoxPrice.Text = "";
}
}
// add to list
private void buttonAdd_Click(object sender, EventArgs e)
{
add(textBoxMake.Text, textBoxModel.Text, textBoxColor.Text, textBoxMiles.Text, textBoxCondition.Text, textBoxInterior.Text, textBoxHighway.Text, textBoxCity.Text, textBoxPrice.Text);
//After you add an item to list it clears text
textBoxMake.Text = ""; textBoxModel.Text = ""; textBoxColor.Text = ""; textBoxMiles.Text = ""; textBoxCondition.Text = ""; textBoxInterior.Text = ""; textBoxHighway.Text = ""; textBoxCity.Text = ""; textBoxPrice.Text = "";
}
//update item in list
private void buttonUpdate_Click(object sender, EventArgs e)
{
update();
}
//delete a item in list
private void buttonDelete_Click(object sender, EventArgs e)
{
delete();
}
//Click on an item, you get all the values in that row into the textbox
//Used for update purposes
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
textBoxMake.Text = listView1.SelectedItems[0].SubItems[0].Text;
textBoxModel.Text = listView1.SelectedItems[0].SubItems[1].Text;
textBoxColor.Text = listView1.SelectedItems[0].SubItems[2].Text;
textBoxMiles.Text = listView1.SelectedItems[0].SubItems[3].Text;
textBoxCondition.Text = listView1.SelectedItems[0].SubItems[4].Text;
textBoxInterior.Text = listView1.SelectedItems[0].SubItems[5].Text;
textBoxHighway.Text = listView1.SelectedItems[0].SubItems[6].Text;
textBoxCity.Text = listView1.SelectedItems[0].SubItems[7].Text;
textBoxPrice.Text = listView1.SelectedItems[0].SubItems[8].Text;
}
private void buttonSearch_Click(object sender, EventArgs e)
{
}
private void buttonSave_Click(object sender, EventArgs e)
{
string make = textBoxMake.Text;
string model = textBoxModel.Text;
string color = textBoxModel.Text;
string miles = textBoxModel.Text;
string condition = textBoxModel.Text;
string interior = textBoxModel.Text;
string highway = textBoxModel.Text;
string city = textBoxCity.Text;
string price = textBoxPrice.Text;
openFileDialog1.FileName = "cars";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (StreamWriter ap = File.AppendText("cars.txt"))
{
ap.WriteLine(make);
ap.WriteLine(model);
ap.WriteLine(color);
ap.WriteLine(miles);
ap.WriteLine(condition);
ap.WriteLine(interior);
ap.WriteLine(highway);
ap.WriteLine(city);
ap.WriteLine(price);
}
}
}
private void buttonLoad_Click(object sender, EventArgs e)
{
var fileLines = File.ReadAllLines(@"C:\Users\Tyler\Documents\Visual Studio 2015\Projects\Prac\FinalProject\FinalProject\bin\Debug\cars.txt");
for (int i = 0; i + 7 < fileLines.Length; i += 8)
{
listView1.Items.Add(
new ListViewItem(new[]
{
fileLines[i],
fileLines[i + 1],
fileLines[i + 2],
fileLines[i + 3],
fileLines[i + 4],
fileLines[i + 5],
fileLines[i + 6],
fileLines[i + 7],
fileLines[i + 8]
}));
}
}
}
}
答案 0 :(得分:2)
小心使用“generic”这个词。 Generic class表示C#中非常具体的内容。看起来你实际上要做的只是将一些功能从表单中移到一个单独的类中。实现这一目标的方法可能是将listView1
传递给新类的构造函数,该构造函数将它存储在类成员中,以便在调用它们时添加,删除和移动引用。
这样的事情:
public class MyClass
{
private ListView listView;
public MyClass(ListView lv)
{
listView = lv;
}
public void add(string make, string model, string color, string miles, string condition, string interior, string highway, string city, string price)
{
string[] row = { make, model, color, miles, condition, interior, highway, city, price }; //adding rows to list
ListViewItem item = new ListViewItem(row);
listView.Items.Add(item);
}
// Delete method goes here
// Move method goes here
}
然后使用它,你可以做类似的事情:
private void buttonAdd_Click(object sender, EventArgs e)
{
MyClass foo = new MyClass(listView1);
foo.add(textBoxMake.Text, textBoxModel.Text, textBoxColor.Text, textBoxMiles.Text, textBoxCondition.Text, textBoxInterior.Text, textBoxHighway.Text, textBoxCity.Text, textBoxPrice.Text);
... // More code
}