Read data from a file line by line [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am currently open a file and show it on a message box.
private void button2_Click_1(object sender, EventArgs e)
{
//OpenFileDialog1.ShowDialog();
OpenFileDialog file = new OpenFileDialog();
file.FileName = "";
file.Title = "Open A Text document.";
file.Filter = "(*.gc)|*.gc|(*.etf)|*.etf|(*.txt)|*.txt|(*.GC)|*.GC|(*.tap)|*.tap";
DialogResult result = file.ShowDialog();
if (result == DialogResult.OK)
{
System.IO.StreamReader OpenFile = new System.IO.StreamReader(file.FileName);
textBox1.Text = OpenFile.ReadToEnd();
OpenFile.Close();
}
if (file.FileName.Length > 0)
{
OpenFile(file.FileName);
}
But I need to read the file line by line and show it on a message box. Is there a way to change this code.

There is a File.ReadLines method that returns an IEnumerable so you can do:
foreach(var line in File.ReadLines("myfile.txt")){
//Do stuff with line
}

Consider using StreamReader.ReadLine() method.

That's exactly what the ReadLine method is for
while (OpenFile.Peek() >= 0)
{
MessageBox.Show(OpenFile.ReadLine());
}

have you tried the OpenFile.ReadLine() method?
or, for a more radical approach
string[] lines = OpenFile.ReadToEnd().Split('\n');

Simplest way to read all lines from file is File.ReadAllLines method (internally it uses StreamReader):
foreach(string line File.ReadAllLines(fileName))
MessageBox.Show(line);

Related

Using ProgressBar for specific time [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want my ProgressBar starting in some point in my code and run total of seconds until my file finish, and of course I know how long the run of my file will take.
I try to read on MSDN but I did not understood how to use it.
My application run files (wireshark file, send the packet using bittwist) and each file will run few seconds and I want the option to see the progress ongoing.
For example I want to set my ProgressBar running for 30 seconds.
How can I do it?
Maybe you want something like this:
public void AnimateProgBar (int milliSeconds)
{
if (!timer1.Enabled) {
progressBar1.Value = 0;
timer1.Interval = milliSeconds / 100;
timer1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < 100) {
progressBar1.Value += 1;
progressBar1.Refresh();
} else {
timer1.Enabled = false;
}
}
Then you just have to call AnimateProgBar(2000) to have your ProgressBar animated during 2 seconds.
EDIT: Sorry, I posted code in VB.NET. Modified to C#.
EDIT: You can add the handler and call the function in this way (for example):
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
AnimateProgBar(2000);
}

C# integer variable in foreach loop [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In C# I have a foreach loop where I want to ++ a integer.
The code is like this:
private void btnClick(object sender, EventArgs e)
{
int Counter = 0;
foreach (SettingsProperty currrentProperty in Properties.Settings.Default.Properties)
{
Counter++;
}
lblText.Text = Counter.ToString();
}
Simple, but of course because I have to assing the integer the variable sticks to 0, otherwise the compiler errors. So the lblText.Text prints 0 to me.
I just can't get it working properly..
Sure this is an easy one, but I couldn't find a awnser.
I think that Properties.Settings.Default.Properties is empty . So to get sure that it is empty try:
private void btnClick(object sender, EventArgs e)
{
if(Properties.Settings.Default.Properties.Count != 0)
{
int Counter = 0;
foreach (SettingsProperty currrentProperty in Properties.Settings.Default.Properties)
{
Counter++;
//Some stuff here else just use .Count without use a foreach
}
lblText.Text = Counter.ToString();
}
else
throw new Exception("Properties.Settings.Default.Properties is empty");
}
Else try to set some breakpoints before compile the code.

Need an equivalent for a C# code in VB.Net [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm doing C# And now I want to learn about VB.Net, and I want the equivalent for this:
Hide();
using (login loginForm = new login())
{
var result = loginForm.ShowDialog();
if (result == DialogResult.OK)
{
Show();
}
else
{
Close();
}
}
Hide()
Using loginForm As New login()
Dim result = loginForm.ShowDialog()
If result = DialogResult.OK Then
Show()
Else
Close()
End If
End Using
What did you try yourself? Just look up each thing on MSDN (Using statement, If statement, ...) and look at the code examples in VB.
Anyway, here is the converted code:
Hide()
Using loginForm As New login()
Dim result = loginForm.ShowDialog()
If result = DialogResult.OK Then
Show()
Else
Close()
End If
End Using

How can I prevent a ListView option from changing? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How can I prevent a ListView option from changing?
I have tried the following, however it still changes:
bool q=false;
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (q)
{
// Let change happen
}
else
{
// Stop change from happening
return;
}
}
Thank you for any help.
Save the prior value and then reset it to the prior.
int lastIndex = -1;
if (q)
{
lastIndex = (ListView)Sender.SelectedIndex;
}
else
{
// Stop change from happening
(ListView)Sender.SelectedIndex = lastIndex;
}

how to open usercontrol and close 2 seconds [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Hello People how can i open userControl and close in the 2 seconds and show another form?
c# WinForms
public void MyFunction()
{
firstForm.ShowDialog();
secondForm.Show();
}
public void firstForm_Load(object sender, EventArgs e)
{
Timer timer = new System.Windows.Forms.Timer() { Interval = 2000 };
timer.Tick += delegate { timer.Stop(); Close(); };
timer.Start();
}

Categories