Поиск в listview
25.05.2015, 07:16. Показов 1280. Ответов 0
Есть 2 формы: на первой listview, по кнопке добавить открывается вторая форма, вносятся данные из textbox'ов в listview
помогите сделать поиск, по всем данным из listview, чтобы при вводе текста в текстбокс на первой форме сразу выдавало результаты в listview, а по кнопке x сброс к изначальному списку
form1:
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
| namespace Kursach
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Data.ind = -1;
Data.kolichestvo = 1;
//snula();
load();
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
listView1.Columns.Add("№", 30);
listView1.Columns.Add("Название", 500);
listView1.Columns.Add("Город", 110);
listView1.Columns.Add("Область", 100);
listView1.Columns.Add("Адрес", 170);
listView1.Columns.Add("Число экспонатов", 130);
listView1.Columns.Add("Дата создания", 120);
}
private void load()
{
FileStream f = new FileStream("BD.txt", FileMode.Open);
BinaryReader fIn = new BinaryReader(f);
Data.kolichestvo = fIn.ReadInt32();
Data.stran = new string[Data.kolichestvo, 6];
for (int i = 0; i < Data.kolichestvo; i++)
for (int j = 0; j < 6; j++)
Data.stran[i, j] = fIn.ReadString();
fIn.Close();
f.Close();
//Add items in the listview
string[] arr = new string[7];
ListViewItem itm;
listView1.Items.Clear();
for (int i = 0; i < Data.kolichestvo; i++)
{
arr[0] = (i + 1).ToString();
arr[1] = Data.stran[i, 0];
arr[2] = Data.stran[i, 1];
arr[3] = Data.stran[i, 2];
arr[4] = Data.stran[i, 3];
arr[5] = Data.stran[i, 4];
arr[6] = Data.stran[i, 5];
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
//------------------------------------------------сохранение-----------------------------------------------------------
private void save()
{
FileStream f = new FileStream("BD.txt", FileMode.Create);
BinaryWriter fOut= new BinaryWriter(f);
fOut.Write(Data.kolichestvo);
for (int i = 0; i < Data.kolichestvo; i++)
for (int j = 0; j < 6; j++)
{
fOut.Write(Data.stran[i, j]);
}
fOut.Close();
f.Close();
}
//------------------------------------------перезапись для удаления----------------------------------------------------
private void save(int a)
{
FileStream f = new FileStream("BD.txt", FileMode.Create);
BinaryWriter fOut = new BinaryWriter(f);
fOut.Write(Data.kolichestvo - 1);
for (int i = 0; i < Data.kolichestvo; i++)
{
if (i == a) continue;
for (int j = 0; j < 6; j++)
{
fOut.Write(Data.stran[i, j]);
}
}
fOut.Close();
f.Close();
}
//-------------------------------------------------Удаление------------------------------------------------------------
private void button4_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0) return;
int q = 0;
q = int.Parse(listView1.SelectedItems[0].Text) - 1;
save(q);
load();
}
//---------------Клик по колонке--------------------
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (e.Column != itemComparer.col1)
{
itemComparer.col1 = e.Column;
itemComparer.so1 = SortOrder.Ascending;
}
else
{
if (itemComparer.so1 == SortOrder.Ascending)
itemComparer.so1 = SortOrder.Descending;
else itemComparer.so1 = SortOrder.Ascending;
}
((ListView)sender).Sort();
listView1.ListViewItemSorter = itemComparer;
}
//--------------------------------Этот класс реализует интерфейс IComparer---------------------------------------------
ItemComparer itemComparer = new ItemComparer();
class ItemComparer : IComparer
{
public int col1 = -1; //---------КЛИКНУТАЯ КОЛОНКА
public SortOrder so1;//--------ПОРЯДОК СОРТИРОВКИ
// ----------------------------------------------работает-------------------------------------------
public int Compare(object x, object y)
{
int returnval = -1;
int num1, num2;
switch (col1)
{
case 0:
{
num1 = int.Parse(((ListViewItem)x).SubItems[col1].Text);
num2 = int.Parse(((ListViewItem)y).SubItems[col1].Text);
if (num1 < num2)
returnval = -1;
else if (num1 > num2)
returnval = 1;
else returnval = 0;
} break;
case 3:
{
string num31 = (((ListViewItem)x).SubItems[col1].Text);
string num32 = (((ListViewItem)y).SubItems[col1].Text);
returnval = String.Compare(num31, num32);
} break;
case 5:
{
num1 = int.Parse(((ListViewItem)x).SubItems[col1].Text);
num2 = int.Parse(((ListViewItem)y).SubItems[col1].Text);
if (num1 < num2)
returnval = -1;
else if (num1 > num2)
returnval = 1;
else returnval = 0;
} break;
default:
{
string value1 = ((ListViewItem)x).SubItems[col1].Text;
string value2 = ((ListViewItem)y).SubItems[col1].Text;
returnval = String.Compare(value1, value2);
} break;
}
if (so1 == SortOrder.Descending)
returnval *= -1;
return returnval;
}
}
//---------------------СОБСНА ПОИСК--------------------
private void button8_Click(object sender, EventArgs e)
{
bool flag = true;
try { double a = double.Parse(textBox1.Text); }
catch { flag = false; }
if (flag)
{
StringBuilder strb = new StringBuilder(textBox1.Text);
{
string[] arr = new string[8];
ListViewItem itm;
listView1.Items.Clear();
for (int i = 0; i < Data.kolichestvo; i++)
for (int j = 0; j < 6; j++)
if ((Data.stran[i, j].ToLower()).IndexOf(textBox1.Text.ToLower()) != -1)
{
arr[0] = (i + 1).ToString();
arr[1] = Data.stran[i, 0];
arr[2] = Data.stran[i, 1];
arr[3] = Data.stran[i, 2];
arr[4] = Data.stran[i, 3];
arr[5] = Data.stran[i, 4];
arr[6] = Data.stran[i, 5];
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
j = 6;
}
}
}
else MessageBox.Show("Введите число в строку поиска");
}
//private void button1_Click(object sender, EventArgs e)
//{
// textBox1.Clear();
// load();
//}
//----------------------------------Удаление всего---------------------------------
private void button5_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Вы действительно хотите удалить все данные?", "Подтверждение удаления", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
FileStream f = new FileStream("BD.txt", FileMode.Create);
BinaryWriter fOut = new BinaryWriter(f);
fOut.Write(0);
fOut.Close();
f.Close();
load();
}
else if (dialogResult == DialogResult.No)
{ }
}
//ЗДЕСЬ БУДЕТ ПОИСК
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
load();
}
private void button3_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0) return;
Data.ind = int.Parse(listView1.SelectedItems[0].Text) - 1;
Form2 f2 = new Form2();
f2.ShowDialog();
Data.ind = -1;
load();
} |
|
form2:
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
| namespace Kursach
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool flag=false;
if (textBox1.Text != "" && textBox2.Text != "" && comboBox1.Text != "" && textBox5.Text != "")
try { flag = true; }
catch { flag = false; }
if (flag)
{
FileStream f = new FileStream("BD.txt", FileMode.Create);
BinaryWriter fOut = new BinaryWriter(f);
//--------------------------------------Добавление------------------------------
if (Data.ind == -1)
{
fOut.Write(Data.kolichestvo + 1);
for (int i = 0; i < Data.kolichestvo; i++)
for (int j = 0; j < 6; j++)
fOut.Write(Data.stran[i, j]);
fOut.Write(textBox1.Text);
fOut.Write(textBox2.Text);
fOut.Write(comboBox1.Text);
fOut.Write(textBox4.Text);
fOut.Write(textBox5.Text);
fOut.Write(textBox6.Text);
}
//-----------------------------------------------Редактирование----------------------------------------
else
{
fOut.Write(Data.kolichestvo);
for (int i = 0; i < Data.kolichestvo; i++)
{
if (i == Data.ind)
{
fOut.Write(textBox1.Text);
fOut.Write(textBox2.Text);
fOut.Write(comboBox1.Text);
fOut.Write(textBox4.Text);
fOut.Write(textBox5.Text);
fOut.Write(textBox6.Text);
}
else
{
for (int j = 0; j < 6; j++)
fOut.Write(Data.stran[i, j]);
}
}
}
fOut.Close();
f.Close();
Close();
}
else { MessageBox.Show("Введите корректные данные"); }
}
private void Form2_Load(object sender, EventArgs e)
{
if (Data.ind != -1)
{
button1.Text = "Сохранить";
textBox1.Text = Data.stran[Data.ind, 0];
textBox2.Text = Data.stran[Data.ind, 1];
comboBox1.Text = Data.stran[Data.ind, 2];
textBox4.Text = Data.stran[Data.ind, 3];
textBox5.Text = Data.stran[Data.ind, 4];
textBox6.Text = Data.stran[Data.ind, 5];
}
else button1.Text = "Добавить";
}
private void textBox3_Validated_1(object sender, EventArgs e)
{
//try
//{
// int x = int.Parse(textBox3.Text);
// errorProvider1.SetError(textBox3, "");
//}
//catch
//{
// errorProvider1.SetError(textBox3, "Ошибка,введите целое число");
// textBox3.Focus();
//}
}
private void textBox5_Validated(object sender, EventArgs e)
{
try
{
double x = double.Parse(textBox5.Text);
errorProvider1.SetError(textBox5, "");
}
catch
{
errorProvider1.SetError(textBox5, "Ошибка, тут должно быть число");
textBox5.Focus();
}
}
private void textBox6_Validated(object sender, EventArgs e)
{
try
{
double x = double.Parse(textBox6.Text);
errorProvider1.SetError(textBox6, "");
}
catch
{
errorProvider1.SetError(textBox6, "Ошибка, тут должна быть дата");
textBox6.Focus();
}
}
}
} |
|
0
|