A
A
Artem2016-07-12 16:52:08
Programming
Artem, 2016-07-12 16:52:08

How to view files in a category and open them in the C# program itself?

The point is this. Going to a specific tab in the tabControl, the user immediately sees two windows. On the left, files from a specific folder of the .pdf extension are displayed (it would be great if there were other extensions). By choosing any name, a file opens on the left, where the user can simply watch it without changes. Tell me, please, how can this be done?
fb9b6af4d3ff426e86f81e381c2cec4e.png155087f089af488280d93e49c4b4521a.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Johnny Gat, 2016-07-12
@Deni74

So far only for pdf-files. For Word files, I came up with an option only with converting the file to pdf and displaying the pdf file on the screen.

Screen
93a830ac35894bb085135c3f80f5c5b7.png

On the form:
- TabControl element (tabControl1)
- On the tabPage1 tab, the Button element (button1)
- On the tabPage2 tab, the ListView (listView1) elements ( set the View property to List in the designer ) and WebBrowser (webBrowser1)
The button1 button click handler:
private void button1_Click(object sender, EventArgs e)
{
    // перейти во вторую вкладку
    tabControl1.SelectTab(1);

    // очистить отображаемый список файлов
    listView1.Items.Clear();

    // отобразить список pdf-файлов из директории
    foreach (string file in Directory.GetFiles(@"H:\Test\333", "*.pdf"))
    {
        listView1.Items.Add(new ListViewItem() { Text = Path.GetFileName(file), Tag = file });
    }
}

The file list consists of instances of the "ListViewItem" class.
The "Text" property is what is displayed on the screen, in this case it is the file name and extension (without the full path to the file) (obtained using the Path.GetFileName function).
The "Tag" property is intended for user data, in this case we will store the full path to the file there.
File selection event handler:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    // потому что при смене выбранного значения этот обработчик ловит ситуацию когда число выделенных элементов равно нулю, данный "костыль" нужен как раз для таких случаев
    if (listView1.SelectedIndices.Count <= 0)
        return;

    // вывести содержимое файла на экран
    webBrowser1.Navigate(listView1.SelectedItems[0].Tag.ToString());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question