-4

Basically I was attempting to use an open file dialog and a list box to list recently opened files in a list box also yes I do use fastcoloredtextbox why do 90% of people I talk to hate it also sorry if the code looks like it was designed by some depressed 11 year old because I am very new.

    private void button5_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        string fm = openFileDialog1.FileName;

        string rf = File.ReadAllText(fm);

        fastColoredTextBox.Text = rf;
        string files = Path.GetFileName(fm);

        foreach (string file in files)
        {
            listBox1.Items.Add(file);
        }
    }

Also it looks unfinished because it is unfinished - I've been trying to fix this and it just kept making more errors so like yea uhh help random people on the internet who I don't know uhh I'm going to stop randomly typing things now

wow edit #3 if it helps anyone I'm having the error on

foreach (string file in files)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
xb207076
  • 5
  • 2
  • 1
    `string files = Path.GetFileName(fm); foreach (string file in files)` Your variable names suggest your thinking is a bit faulty here. Your `files` is not **multiple** files - it is the name of a **single** file. And your `file` is not a single file - it is an individual **character** from the single filename you are looping over. – mjwills Jun 10 '20 at 05:21

1 Answers1

0

First you have to add a new Instance of var openFileDialog = new OpenFileDialog(); if you don't have. Then you could do something like this to get the text of the file and add it to you listbox:

private void button5_Click(object sender, EventArgs e)
{
    var openFileDialog = new OpenFileDialog();
    if (openFileDialog.ShowDialog() == true)
    {
        var file = openFileDialog.FileName;
        var text = File.ReadAllText(file);
        fastColoredTextBox.Text = text;
        var filename = Path.GetFileName(file);
        listBox1.Items.Add(filename);
    }
}

Also you do not have to do this:

foreach (string file in files)

because Path.GetFile() returns only a string with your FileName. So you can add it directly to your ListBox.

Presi
  • 806
  • 10
  • 26
  • i dont know what im doing wrong but i did everything to make it work and POOF CS0019 C# Operator '==' cannot be applied to operands of type 'DialogResult' and 'bool' – xb207076 Jun 10 '20 at 05:07
  • also i was an idiot and decided to copy past the code you put and edit the names of all my stuff copied the var openFileDialog = new OpenFileDialog(); and it didnt help (also this would be an edit to my other comment but my caveman brain cant find the edit button somehow) – xb207076 Jun 10 '20 at 05:16
  • Do you have the correct references for this? The Namespace of the OpenFileDialog is `Microsoft.Win32.OpenFileDialog`. Then the ShowDialog Method is a nullable bool – Presi Jun 10 '20 at 05:37
  • this is the closest anyone has got me and yes i do have the correct reference i changed it from == true to == DialougeResult.OK And It Worked From There But Im Trying To Get Rid Of The File Paths – xb207076 Jun 10 '20 at 12:19
  • Great that it helped you @xb207076 – Presi Jun 10 '20 at 12:24
  • uuuuuuuuuugh im stupid besides that i changed the items.add(file); to items.add(filename); im pretty sure you left that in there so i could do that if i wanted or something if you didnt act like i never existed alright – xb207076 Jun 10 '20 at 12:25
  • hey also unrelevant to the question so if you dont want to read you dont have to but a minute ago i was adding an always on top function to my program and i got 2 random errors without codes or locations i cant remember what they said exactly but they were both something along the lines of "something exec function needs something to do" and im saying that how i would phrase it i tried to rebuild my project and it failed, built it normally and it worked fine but the errors were still there then i added a comment to test if it was just the fact there wasnt a diffrence and the errors disapeared – xb207076 Jun 10 '20 at 13:20
  • It sounds like a special problem. I had something like that few months ago. This helped: https://stackoverflow.com/questions/17703004/visual-studio-displaying-errors-even-if-projects-build Maybe it helps you too. The solution for me was the second answer. – Presi Jun 10 '20 at 13:44
  • when the whole things is basically about resharper but u have classicprojectui and thats it XD – xb207076 Jun 10 '20 at 13:52