Guten Tag, ich will ein Programm in Win. Forms erstellen, in welchen man eine Text Datei auswählen kann und nach Wörtern und Zeilen durchsuchen kann. Die Ausgabe soll den Zeileninhalt angeben in welchen das besagte Wort oder die Zeilenangabe drine steckt.
Bis jetzt habe ich es geschafft eine Text Datei auszuwählen und den Text nach Wörtern durchzusuchen, die der Text beinhaltet. Wo bei ich jetzt noch Hilfe bräuchte ist die Eingabe der Zeileneingabe.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> FileLines = new List<string>();
List<int> iLineNumber = new List<int>();
private void button1_Click(object sender, EventArgs e)
{
// Open a file dialog
using (OpenFileDialog openDialog = new OpenFileDialog())
{
// Set the file dialog to show only *.txt file or all files
openDialog.Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*";
// Allow only single file selection
openDialog.Multiselect = false;
// Make sure the user didn't clicked the 'Cancel' button
if (openDialog.ShowDialog(this) == DialogResult.OK)
{
// Update the current file label with the filename only (not the full path)
textBox1.Text = $"Current file: {Path.GetFileName(openDialog.FileName)}";
// Add each line of the txt file into the list
foreach (string line in File.ReadAllLines(openDialog.FileName, Encoding.UTF8))
FileLines.Add(line);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
// Clear the list
listView1.Items.Clear();
// Count the number of line so you will be able to present it on the results list later on
int iLineNumber = 1;
// For each item in the 'FileLines' list
foreach (var item in FileLines)
{
// Check whether the current line contains the term the user typed in the searchbox
// I'm using 'ToLower()' to ignore case
if (item.ToLower().Contains(textBox2.Text.ToLower()))
{
// Create new ListViewItem to be added later on to the results list
// Add the first column the complete line that contains the term in the searchbox
ListViewItem lvi = new ListViewItem(item);
// Add the line number to the second column
lvi.SubItems.Add(iLineNumber.ToString());
// Add the ListviewItem to the results list
listView1.Items.Add(lvi);
}
// Increment the line number variable
iLineNumber++;
}
}
Verstehe ich das richtig, dass "nach Worten suchen" und "Zeile wählen" zwei getrennte Funktionalitäten sind?
Das wäre doch recht einfach machbar in dem du via Index auf deine Liste "FileLines" zugreifst. Oder verstehe ich an der Anforderung irgendwas falsch?
Wissen ist nicht alles. Man muss es auch anwenden können.
PS Fritz!Box API - TR-064 Schnittstelle | PS EventLogManager |
Ein paar Anpassungen als Tipp.
T-Virus
Developer, Developer, Developer, Developer....
99 little bugs in the code, 99 little bugs. Take one down, patch it around, 117 little bugs in the code.