using System; using System.Collections.Generic; using System.IO; using System.Linq;…
- User
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Forms;
namespace ReadLargeTxtFile { public partial class MainForm : Form { private readonly List _lines = new List(); private readonly int _pageSize = 100; // 每页显示的行数 private int _totalLines; // 总行数 private int _currentPage; // 当前页数 private int _totalPages; // 总页数
public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { listView.VirtualListSize = 0; // 先设置为0,等数据加载后再更新 } private void btnLoad_Click(object sender, EventArgs e) { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = “Text files (.txt)|.txt”; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); openFileDialog.Multiselect = false; if (openFileDialog.ShowDialog() == DialogResult.OK) { var filePath = openFileDialog.FileName; LoadFile(filePath); UpdateListView(); } } private void LoadFile(string filePath) { _lines.Clear(); _totalLines = 0; _currentPage = 0; _totalPages = 0; using (var streamReader = new StreamReader(filePath)) { while (!streamReader.EndOfStream) { var line = streamReader.ReadLine(); _lines.Add(line); _totalLines++; } } // 计算总页数 _totalPages = (int)Math.Ceiling((double)_totalLines / _pageSize); } private void UpdateListView() { listView.VirtualListSize = _totalLines; // 更新总行数 listView.VirtualMode = true; // 开启虚拟模式 listView.Columns.Clear(); listView.Columns.Add(“序号”); listView.Columns.Add(“行内容”); } private void listView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { // 获取指定行的数据 var lineNumber = e.ItemIndex + 1; // 行号从1开始 var lineContent = _lines[e.ItemIndex]; // 构造ListViewItem对象,并设置其子项的文本 var listViewItem = new ListViewItem(lineNumber.ToString()); listViewItem.SubItems.Add(lineContent); // 将ListViewItem对象赋值给RetrieveVirtualItemEventArgs的Item属性 e.Item = listViewItem; } private void btnFirstPage_Click(object sender, EventArgs e) { if (_currentPage > 0) { _currentPage = 0; listView.Refresh(); } } private void btnPrevPage_Click(object sender, EventArgs e) { if (_currentPage > 0) { _currentPage–; listView.Refresh(); } } private void btnNextPage_Click(object sender, EventArgs e) { if (_currentPage < _totalPages - 1) { _currentPage++; listView.Refresh(); } } private void btnLastPage_Click(object sender, EventArgs e) { if (_currentPage < _totalPages - 1) { _currentPage = _totalPages - 1; listView.Refresh(); } } private void listView_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e) { // 预加载当前页及周围页的数据,避免滚动时重复加载数据 var startLine = Math.Max(0, _currentPage - 1) * _pageSize; var endLine = Math.Min(_totalLines - 1, (_currentPage + 1) * _pageSize - 1); for (var i = startLine; i <= endLine; i++) { if (_lines.Count <= i) { break; } if (i >= e.StartIndex && i <= e.EndIndex) { // 当前行已缓存,无需重新加载 continue; } listView.Items.Add(new ListViewItem(i.ToString())); } } private void listView_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e) { // 根据关键字查找指定行号的行,以便ListView自动定位到该行 var keyword = e.Text.ToLower(); var startLine = Math.Max(0, _currentPage - 1) * _pageSize; var endLine = Math.Min(_totalLines - 1, (_currentPage + 1) * _pageSize - 1); for (var i = startLine; i <= endLine; i++) { var lineContent = _lines[i].ToLower(); if (lineContent.Contains(keyword)) { e.Index = i; return; } } // 未找到匹配项,设置e.Index为-1 e.Index = -1; } private void listView_SearchForVirtualItemCompleted(object sender, SearchForVirtualItemEventArgs e) { // 没有发现匹配项 if (e.Index < 0) { MessageBox.Show($“未找到匹配项:”{e.Text}“”, “查找结果”, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { // 定位到匹配行,并选中该行 listView.TopItem = listView.Items[e.Index]; listView.TopItem.Focused = true; listView.TopItem.Selected = true; } } } - Assistant
本代码实现了一个WinForm程序,可以读取大型的文本文件,并支持分页、关键字搜索和虚拟模式优化。主要采用了ListView控件的虚拟模式功能,可以避免加载过多的数据,提高了程序的运行效率。主要逻辑如下:
-
点击“加载文件”按钮时,弹出文件选择对话框,获取选中的文件路径,然后调用LoadFile方法读取文件内容,计算总行数和总页数,并调用UpdateListView方法更新ListView控件。
-
在LoadFile方法中,使用StreamReader逐行读取文本文件,并将每行文本内容存储在List对象中。读取完所有行后,计算总行数和总页数。
-
在UpdateListView方法中,先将ListView控件的VirtualListSize属性设置为总行数,然后开启虚拟模式,最后添加两列列标题:“序号”和“行内容”。
-
当ListView控件需要显示指定行的数据时,触发RetrieveVirtualItem事件,根据行号从List对象中获取对应的行内容和行号,并构造ListViewItem对象,设置其子项的文本。
-
点击“首页”、“上一页”、“下一页”、“末页”按钮时,更新_currentPage变量的值,并调用ListView的Refresh方法刷新显示内容。Refresh方法会触发ListView控件的RetrieveVirtualItem事件,重新加载对应行的数据。
-
当滚动ListView控件时,ListView会触发CacheVirtualItems事件,预加载当前页及周围页的数据,避免滚动时重复加载数据。
-
当在ListView控件中搜索关键字时,ListView控件会触发SearchForVirtualItem事件,根据关键字查找指定行号的行,以便ListView自动定位到该行。
-
当搜索结束时,ListView控件会触发SearchForVirtualItemCompleted事件,将搜索结果返回给用户。如果找到匹配项,则定位到该行,并选中该行。如果未找到匹配项,则弹出提示框提示用户。
整个程序的架构比较清晰,利用ListView控件的虚拟模式和事件回调机制,实现了高效的大文件读取和搜索功能。
-
- User
以上代码中有部分错误,你知道是哪里有问题吗
- Assistant
在代码中,以下部分存在错误:
-
openFileDialog.Filter = “Text files (.txt)|.txt”; 应该使用双引号包裹的语法,而不是中文的引号包括。
-
Listview控件的CacheVirtualItems事件中,仅仅是添加了一行新的ListViewItem对象,而没有实际加载对应行的数据,因此会导致在滚动ListView时出现空白或重复的行。
-
在SearchForVirtualItemCompleted事件中,设置了ListView的TopItem属性,但是并没有设置其Focused和Selected属性,因此可能会导致定位到匹配行,但是无法选中该行。
正确的代码如下:
private void btnLoad_Click(object sender, EventArgs e) { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text files (.txt)|*.txt"; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK) { var filePath = openFileDialog.FileName; LoadFile(filePath); UpdateListView(); } -
- User
在ListView1_CacheVirtualItems方法中,会出现错误 当 ListView 处于虚拟模式时,无法向 ListView 项集合中添加项。请改用 VirtualListSize 属性更改 ListView 项集合的大小
- Assistant
ListView控件在虚拟模式下,需要使用VirtualListSize属性来设置ListView控件的总行数,以便ListView控件创建足够的行来显示所有的数据。因此,在CacheVirtualItems事件中,如果需要添加新的ListViewItem对象,也需要先判断当前行索引是否超出了ListView控件虚拟模式下的实际行数范围。如果当前行索引超出了虚拟模式下的实际行数范围,就需要先通过设置VirtualListSize属性扩展ListView控件的行数,再添加新的ListViewItem对象。
修改后的ListView1_CacheVirtualItems方法代码如下:
private void listView1_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e) { var startLine = Math.Max(0, _currentPage - 1) * _pageSize; var endLine = Math.Min(_totalLines - 1, (_currentPage + 1) * _pageSize - 1);
for (var i = e.StartIndex; i <= e.EndIndex; i++) { if (i >= startLine && i <= endLine) { // 当前行已缓存,无需重新加载 continue; } // 判断索引是否超出了虚拟模式下的实际行数范围 if (i >= listView1.VirtualListSize) { listView1.VirtualListSize = i + 1; // 扩展ListView的行数 } var lineContent = _lines[i]; var listViewItem = new ListViewItem((i + 1).ToString()); listViewItem.SubItems.Add(lineContent); listView1.Items.Add(listViewItem); }}
- User
在
- Assistant
抱歉,由于您并未标注出哪个代码部分存在问题,我无法给予具体建议。如果您能告诉我出现了哪个问题或错误,我会尽力解答并提供正确代码。
- User
var listViewItem = new ListViewItem((i + 1).ToString()); listViewItem.SubItems.Add(lineContent); listView1.Items.Add(listViewItem); 这部分代码会出现错误:当 ListView 处于虚拟模式时,无法向 ListView 项集合中添加项。请改用 VirtualListSize 属性更改 ListView 项集合的大小
- Assistant
ListView控件在虚拟模式下,需要使用VirtualListSize属性来设置ListView控件的总行数,以便ListView控件创建足够的行来显示所有的数据。因此,在CacheVirtualItems事件中,如果需要添加新的ListViewItem对象,也需要先判断当前行索引是否超出了ListView控件虚拟模式下的实际行数范围。如果当前行索引超出了虚拟模式下的实际行数范围,就需要先通过设置VirtualListSize属性扩展ListView控件的行数,再添加新的ListViewItem对象。
修改后的ListView1_CacheVirtualItems方法代码如下:
private void listView1_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e) { var startLine = Math.Max(0, _currentPage - 1) * _pageSize; var endLine = Math.Min(_totalLines - 1, (_currentPage + 1) * _pageSize - 1);
for (var i = e.StartIndex; i <= e.EndIndex; i++) { if (i >= startLine && i <= endLine) { // 当前行已缓存,无需重新加载 continue; } // 判断索引是否超出了虚拟模式下的实际行数范围 if (i >= listView1.VirtualListSize) { listView1.VirtualListSize = i + 1; // 扩展ListView的行数 } var lineContent = _lines[i]; var listViewItem = new ListViewItem((i + 1).ToString()); listViewItem.SubItems.Add(lineContent); listView1.Items.Add(listViewItem); }}
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.