使用 C# Windows Forms Application 在 listBox 中指定正确的行

Specify proper line in listBox using C# Windows Forms Application

我正在使用 C# Windows Forms Application 创建一个简单的应用程序。我使用了一个列表框来读取和显示一个文本文件(10 行)。如何通过按适当的按钮来指定列表框中的第一行、最后一行、上一行和下一行?任何帮助将不胜感激。谢谢你。 [在此处输入图片描述][1]

我~想~你问的是如何通过按下按钮来更改列表框中的 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);
    }
}

请注意,如果当前 select 没有项目,则按上一个或下一个将 select 列表框中的第一个项目。

How to specify the First, Last, Previous and Next line in the listBox by pressing a proper button?

注意这个答案假定您一次只显示一行,它可以是第一行、下一行、上一行和/或最后一行文本文件。

让我们先回顾一下您当前的方法。例程 button6_Click 仅尝试加载文件,读取行,如果 line/string 不是 null,则将此字符串添加到 listBox1;这是对该文件中的每个字符串执行此操作。 OpenFileDialogStreamReader 的用法实现了 IDisposable,您应该将它们包装在 using 块中以确保对象也被正确处理。

您当前的方法很可能不是您所需要的,因为您不想一次加载所有内容,只是需要的时候。考虑到这一点,您需要从文件中保留这些行中的某种 collection;方法有很多,但我只关注一种方法。

首先,创建一个新的 class,如下所示:

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();
    }

}

接下来,您可以简单地通过调用一些函数来使用它 return 您需要的东西。在您的特定按钮中放置以下逻辑来处理您需要的内容。

 // 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 class 中的 CurrentFileLines 构建一个 Linq 查询并提取您需要的内容,它如果某处需要,可能会有所帮助。

注:

每次我做某事时,我都在清除 listBox1.Items,你不必这样做,但我做到了。还要记住,在调用这些函数中的任何一个和/或编辑它们以确保您有数据之前,请确保您已加载文件。如果有不明白的地方,请告诉我,我可以提供帮助。