【发布时间】:2019-12-01 07:39:50
【问题描述】:
我正在使用 C# Windows 窗体应用程序来创建一个简单的应用程序。我使用 listBox 来读取和显示一个文本文件(10 行)。如何通过按正确的按钮指定列表框中的第一行、最后一行、上一行和下一行?任何帮助将不胜感激。谢谢你。 [在此处输入图片说明][1]
【问题讨论】:
我正在使用 C# Windows 窗体应用程序来创建一个简单的应用程序。我使用 listBox 来读取和显示一个文本文件(10 行)。如何通过按正确的按钮指定列表框中的第一行、最后一行、上一行和下一行?任何帮助将不胜感激。谢谢你。 [在此处输入图片说明][1]
【问题讨论】:
如何通过适当的按钮指定listBox中的第一行、最后一行、上一行和下一行?
注意:此答案假设您一次只显示一行,可以是文本文件中的第一行、下一行、上一行和或最后一行。
让我们先回顾一下您当前的方法。例程button6_Click 只是尝试加载文件,读取行,如果行/字符串不是null,则将此字符串添加到listBox1;这是为该文件中的每个字符串执行此操作。 OpenFileDialog 和 StreamReader 的使用实现了 IDisposable,您应该将它们包装在 using 块中以确保对象也被正确处理。
您当前的方法很可能不是您需要的,因为您不想一次加载所有内容,仅在需要时加载。考虑到这一点,您需要从文件中保留这些行中的某种collection;有很多方法,但我将只关注一种方法。
首先,创建一个新类,如下所示:
public class NavigateFile
{
// Stores the file lines collection
public IEnumerable<string> CurrentFileLines { get; private set; }
// Tracks the current line index
public int CurrentFileLineIndex { get; private set; } = 0;
// Tracks the next line index
public int NextFileLineIndex { get; private set; } = 1;
// Tracks the previous line index
public int PreviousFileLineIndex { get; private set; } = -1;
// Tracks the last line index
public int LastFileLineIndex { get; private set; } = 0;
// Routine to load the file
public void LoadFile()
{
using (OpenFileDialog ofd = new OpenFileDialog())
if (ofd.ShowDialog() == DialogResult.OK)
CurrentFileLines = File.ReadLines(ofd.FileName).Where(line => !string.IsNullOrEmpty(line));
CurrentFileLineIndex = 0;
NextFileLineIndex = 1;
PreviousFileLineIndex = -1;
LastFileLineIndex = CurrentFileLines.Count();
}
// Routine return the first line in the file
public string GoToFirstLine()
{
CurrentFileLineIndex = 0;
return CurrentFileLines.ToList()[CurrentFileLineIndex];
}
// Routine return the next line in the file if we can
public string GoToNextLine()
{
if((CurrentFileLineIndex + 1) <= CurrentFileLines.Count() - 1)
{
CurrentFileLineIndex++;
return CurrentFileLines.ToList()[CurrentFileLineIndex];
}
return CurrentFileLines.ToList()[CurrentFileLineIndex];
}
// Routine return the previous line in the file if we can
public string GoToPreviousLine()
{
if ((CurrentFileLineIndex - 1) >= 0 && (CurrentFileLineIndex - 1) <= CurrentFileLines.Count())
{
CurrentFileLineIndex--;
return CurrentFileLines.ToList()[CurrentFileLineIndex];
}
return CurrentFileLines.ToList()[CurrentFileLineIndex];
}
// Routine return the last line in the file
public string GoToLastLine()
{
CurrentFileLineIndex = CurrentFileLines.Count() - 1;
return CurrentFileLines.Last();
}
}
接下来,您可以简单地通过调用一些函数来返回您需要的内容。在您的特定按钮中添加以下逻辑以处理您需要的操作。
// put this in your class where you will be using the new class
private NavigateFile NavigateFile { get; set; } = new NavigateFile();
// Load a file
listBox1.Items.Clear();
NavigateFile.LoadFile();
listBox1.Items.Add(NavigateFile.GoToFirstLine());
// Go to first line
listBox1.Items.Clear();
listBox1.Items.Add(NavigateFile.GoToFirstLine());
// Go to next line
listBox1.Items.Clear();
listBox1.Items.Add(NavigateFile.GoToNextLine());
// Go to previous line
listBox1.Items.Clear();
listBox1.Items.Add(NavigateFile.GoToPreviousLine());
// Go to last line in the file
listBox1.Items.Clear();
listBox1.Items.Add(NavigateFile.GoToLastLine());
如果您需要查找某个项目,您可以在 NavigateFile 类中针对 CurrentFileLines 构建一个 Linq 查询并提取您需要的内容,如果在某个地方需要,它可能会有所帮助。
注意:
每次我做某事时,我都会清除listBox1.Items,您不必这样做,但我确实这样做了。还要记住,在调用任何这些函数之前确保你已经加载了一个文件,或者编辑它们以确保你有数据。如果有不明白的地方,请告诉我,我可以提供帮助。
【讨论】:
我~想~你在问如何通过按下按钮来改变列表框中的selected项?
如果是这样,类似:
OpenFileDialog openFile = new OpenFileDialog();
private void btnLoadFile_Click(object sender, EventArgs e)
{
if (openFile.ShowDialog() == DialogResult.OK )
{
try
{
List<String> lines = new List<String>(System.IO.File.ReadAllLines(openFile.FileName));
lines.RemoveAll(l => l.Trim().Length == 0);
if (lines.Count > 0)
{
listBox1.Items.Clear();
listBox1.Items.AddRange(lines.ToArray());
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "Error Loading File");
}
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.SelectedIndex = 0;
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.SelectedIndex = (listBox1.Items.Count - 1);
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.SelectedIndex = (listBox1.SelectedIndex > 0) ? (listBox1.SelectedIndex - 1) : 0;
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.SelectedIndex = (listBox1.SelectedIndex < (listBox1.Items.Count - 1)) ? (listBox1.SelectedIndex + 1) : (listBox1.Items.Count - 1);
}
}
请注意,如果当前未选择任何项目,则按上一个或下一个将选择列表框中的第一个项目。
【讨论】: