I have a browse button which allows users to browse folders, when the folder has been selected it is put into a textbox, the user then clicks a button to open the folder.
What sort of exception handling do I need for the open button? If you click it when there is no path in the textbox the program stops. I tried something like:
if textbox1.text = true;
but didn't work, please help if you know how!
Loading
Posted Jun 15, 2007, 3:31 PM
EDIT:
Im still going to see if I cant get it to work the other way. I know I can and it bothers me when I just havent done it, lol. Ill get back to you with the working code so you can implement it if you want.
Brandon
EDIT[2]:
Drew, I have a question for you. Are you building this program just for fun or are you building it for a client or something? Ive kind of run out of idea's for programs to build myself and I would like to help you out on this particular one your working on. After reviewing some of your coding I would say were really at about the same level of knowledge right now so itd be a learning experience for the both of us!
Drew TaylorPosted Jun 15, 2007, 4:44 AM
I got two errors:
Operator || cannot be applied to operands of type string and object
Possible uninitiated reference comparison
I have another idea, what if I have an error message if the user clicks the button and there is nothing selected? Would that be easier to do? I added an if else to the button but it breaks on the following line of code:
FileStream fsFileInfo = new FileStream(FilePath + selectedFile, FileMode.Open, FileAccess.Read);
Here is the code:
if (Convert.ToBoolean(filesListBox.SelectedItem = false))
{
MessageBox.Show("Please select a folder");
}
else
{
string selectedFile; string header = "File Name"; //string[] items = text.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries); //Grab the file name that is selectedselectedFile = Convert.ToString(filesListBox.SelectedItem);
string fileName = selectedFile; //Open the file and assign a StreamReader to read the contentsFileStream fsFileInfo =
new FileStream(FilePath + selectedFile, FileMode.Open, FileAccess.Read);StreamReader srFileInfo =
new StreamReader(fsFileInfo); //Finally, display the entire contents of the file with a header at start of filefileContentsText.Text = Convert.ToString(header + Environment.NewLine + "************" + Environment.NewLine + fileName + Environment.NewLine + Environment.NewLine + "Contents" + Environment.NewLine + "************" + Environment.NewLine + srFileInfo.ReadToEnd());
srFileInfo.Close();
fsFileInfo.Close();
this.textBox1.Text=(DateTime.Now.ToShortTimeString() + " File was opened succussfully");}
Posted Jun 14, 2007, 10:03 AM
Double click the list box and it will create a listBox1_SelectedIndexChanged() code stub for you to handle the event.
Put this code in that event:
if(listBox1.SelectedItem = String.Empty || listBox1.SelectedItem = null)
{
button.Enabled = false;
}
else if(listBox1.SelectedItem != String.Empty || listBox1.SelectedItem != null)
{
button.Enabled = true;
}
I think that will work, you might have to tweak it a bit but thats the general idea. It evaluates the listbox contents to see if an item is selected and if it is, it enabled the button. And because "_SelectedIndexChanged" is almost an autonomous event, it checks it whenever the contents change, the user selects something different, or anything else happens to that listBox.
I used one and it gave me a few problems in the future but I fixed them easily and it works very dynamically and clean. Just remember, if ANYTHING happens to that listbox it will almost definitely trigger the _SelectedIndexChanged event. Good luck!
Brandon
David IsaacPosted Jun 14, 2007, 9:56 AM
the SelectedIndexChanged event on the list box should allow you to play about with enabling and disabling buttons
Drew TaylorPosted Jun 14, 2007, 9:38 AM
Thanks for your replies, the code works great! Thanks! I tried something like that earlier but didn't know how.
I have a different button which opens highlighted files in a listbox, is there a way to have it enabled only when an item is selected?
David IsaacPosted Jun 14, 2007, 9:33 AM
Mahesh ChandPosted Jun 14, 2007, 9:29 AM
{
MessageBox.Show("Please select a folder");
Return;
}