Hello,
I am beginner in C# and I need your help.
I can read the file and I can see it in my richtextbox. But I do not want to see all of lines in richtextbox. I want to see lines which are begin with MR, MISS and CHD. I can not do that. Than, I will save them to the my database. My text file is like that:
1 MR HANN CO 03.04.10 10.04.10 7 1XDouble Room Balcony ( All Inclusive 278115
2 CHD. INGA CO 13J.
3 MISS JON, BIRGITTA 03.04.10 10.04.10 7 1XDeluxe Suite, Balcony All Inclusive 278115 GOT FHY836
4 MR JONSSON, PER
5 CHD JONSSON, OSKAR 12J.
6 CHD JONSSON, JAKOB 9J.
7 CHD JONSSON, ISAK 5J.
8 MISS NAIMER, HENNA 03.04.10 10.04.10 7 1XFamily Suite Balcony All Inclusive 411622 HEL XQ 6525
My codes are:
namespace ANALYZE
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Open File";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "Text files(*.txt)|*.txt";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fdlg.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty)
{
MessageBox.Show("Please browse text file!");
}
FileStream akis;
StreamReader Okuma;
string Yol = textBox1.Text;
akis = new FileStream(Yol, FileMode.Open, FileAccess.Read);
Okuma = new StreamReader(akis);
richTextBox1.Text = Okuma.ReadToEnd(); // I can just do this. but here, I want to see necessary informations.
MessageBox.Show("Operation is completed.");
Loading
Hirendra SisodiyaPosted Apr 12, 2010, 7:36 AM
Apply this code sample on button_2click event, i think it will go perfectly:
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty)
{
MessageBox.Show("Please browse text file!");
}
FileStream akis;
StreamReader Okuma;
string Yol = textBox1.Text;
akis = new FileStream(Yol, FileMode.Open, FileAccess.Read);
Okuma = new StreamReader(akis);
string OneLine = "";
string[] readLaststring = null;
OneLine = Okuma.ReadLine();
Application.DoEvents();
while (!(OneLine == null))
{
if ((OneLine.Contains("MR")) || (OneLine.Contains("MISS")) || (OneLine.Contains("CHD")))
{
richTextBox1.Text += Environment.NewLine + OneLine;
}
OneLine = Okuma.ReadLine();
}
// richTextBox1.Text = Okuma.ReadToEnd(); // I can just do this. but here, I want to see necessary informations.
MessageBox.Show("Operation is completed.");
}
thanks
Please mark as answere if it helps
kristen koPosted Apr 12, 2010, 8:50 AM
kristen koPosted Apr 12, 2010, 8:44 AM
Program start perfectly but then i can not see the down of richtextbox and new dialog is seen. It said that deadlock and -->
The CLR has been unable to transition from COM context 0x1b8f0080 to COM context 0x1b8f02d0 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
Hirendra SisodiyaPosted Apr 12, 2010, 8:36 AM
thanks
kristen koPosted Apr 12, 2010, 8:15 AM
Thank you. But ContextSwitchDeadlock occured. It started to write to rich text box than program said "not responding" and ContextSwitchDeadlock. How can we solve this?
kristen koPosted Apr 12, 2010, 7:06 AM
sheetalw wattamwarPosted Apr 12, 2010, 6:38 AM
HI
You need to put checking about which line u want to take. First read all lines then check for MR. Miss, etc, if found word take that line to ur database.