Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ProgressBar action to be done when performing a task in windows application

i have Browse Button ,RichtextBox & Progressbar [openfiledialog,backgroundworker] in the form.
Task
First Thread reads a directory text files and put in multiline text box
another thread to show progress bar works based on the number of files in the directory.


private void btnBrowse_Click(object sender, EventArgs e)
{
openFileandRead();
}
public void openFileandRead()
{
Stream myStream = null;
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Title = "Open Text File";
Dialog.Filter = "TXT files|*.txt";
Dialog.Multiselect = true; //allows to select multiple files in openfiledialog
 
if (Dialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = Dialog.OpenFile()) != null)
{
using (myStream)
{
 
string[] filePaths = Directory.GetFiles(@"D:\TestTxtFiles", "*.txt", SearchOption.AllDirectories);
foreach (string file in filePaths)
{
rtxtFile.AppendText(System.IO.File.ReadAllText(file));
Thread.Sleep(2000);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
 
}


Thanks in Advance

[Agent_Spock]
- Added code brackets
Posted
Updated 5-Sep-14 22:17pm
v4
Comments
Sanchayeeta 5-Sep-14 9:19am    
whats your problem?
kavitha3 5-Sep-14 9:34am    
c# code
Description of code - used openfiledialog to open multiple files then read each file in the Richtextbox
on doing it progressbar should run

code required : to run progressbar
kavitha3 5-Sep-14 9:38am    
i have Browse Button ,RichtextBox & Progressbar [openfiledialog,backgroundworker] in the form.
Task
First Thread reads a directory text files and put in multiline text box
another thread to show progress bar works based on the number of files in the directory.


private void btnBrowse_Click(object sender, EventArgs e)
{
openFileandRead();
}
public void openFileandRead()
{
Stream myStream = null;
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Title = "Open Text File";
Dialog.Filter = "TXT files|*.txt";
Dialog.Multiselect = true; //allows to select multiple files in openfiledialog

if (Dialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = Dialog.OpenFile()) != null)
{
using (myStream)
{

string[] filePaths = Directory.GetFiles(@"D:\TestTxtFiles", "*.txt", SearchOption.AllDirectories);
foreach (string file in filePaths)
{
rtxtFile.AppendText(System.IO.File.ReadAllText(file));
Thread.Sleep(2000);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}

}
agent_kruger 5-Sep-14 9:22am    
sir, with that little piece of information we cannot understand anything please reframe your question again.
kavitha3 5-Sep-14 9:42am    
i am Kavitha female...
i have updated the Question .Please check sir..
i tried to be clear.

i have Browse Button ,RichtextBox & Progressbar [openfiledialog,backgroundworker] in the form.
Task
First Thread reads a directory text files and put in multiline text box
another thread to show progress bar works based on the number of files in the directory.


private void btnBrowse_Click(object sender, EventArgs e)
{
openFileandRead();
}
public void openFileandRead()
{
Stream myStream = null;
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Title = "Open Text File";
Dialog.Filter = "TXT files|*.txt";
Dialog.Multiselect = true; //allows to select multiple files in openfiledialog

if (Dialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = Dialog.OpenFile()) != null)
{
using (myStream)
{

string[] filePaths = Directory.GetFiles(@"D:\TestTxtFiles", "*.txt", SearchOption.AllDirectories);
foreach (string file in filePaths)
{
rtxtFile.AppendText(System.IO.File.ReadAllText(file));
Thread.Sleep(2000);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}

}
 
Share this answer
 
Comments
agent_kruger 6-Sep-14 4:20am    
it is a kind request that please do not write answer to those question which are asked by yourself

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900