search for a word in textfile in c#

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace WindowsApplication1
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(696, 496);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(736, 40);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(736, 72);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(712, 112);
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox2.Size = new System.Drawing.Size(120, 20);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(720, 144);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 4;
this.textBox3.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(840, 605);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox3,
this.textBox2,
this.button2,
this.button1,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{


}

private void button1_Click(object sender, System.EventArgs e)
{ textBox1.Clear();
openFileDialog1.ShowDialog();
StreamReader sr = new StreamReader(openFileDialog1.FileName);

String woord;
woord = sr.ReadLine();
while(woord != null)
{
textBox1.Text = textBox1.Text + woord ;
woord = sr.ReadLine();
}
sr.Close();
}

private void button2_Click(object sender, System.EventArgs e)
{
textBox1.Clear();
openFileDialog1.ShowDialog();
StreamReader sr = new StreamReader(openFileDialog1.FileName);

String woord;
int gevonden = 0;
woord = sr.ReadLine();
while(woord != null)
{

textBox1.Text = textBox1.Text + woord ;

for(int i=0;i<woord.Length-1;i++)
{
if(woord.Substring(0,i).Equals(textBox2.Text))
{
gevonden++;
}
}

woord = sr.ReadLine();
}
sr.Close();
textBox3.Text = "" + gevonden;
}
}
}

Comments

  • string is immutable. You cant change the content of a string once you have assigned to it.
    C# has provided a work around for this, but it is very memory intensive.
    You are also using String instead of string. Take a look in the framework documentation to see the difference between String and string.
    Use StringBuilder instead.


  • thank you for your reply, but i have found a way to look for a word in a textfile on my one. If you want i can give you the code.
    greets joekske
  • Hi joekske,

    can you share with me your source-code for search for a word in textfile in c#. thanks in advance. My email fairplay2284@gmail.com.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories