Open file using combo box selection

Abdul Hayee

Member
Joined
Mar 31, 2020
Messages
24
Programming Experience
Beginner
Hi Everyone.
I want to make an application in which different files will be open based on selection from combo box. Below is the image what i want to do
1587738494218.png

if i select File1.pdf and click Open button then the file open in richtext box. same as for other files (File2.pdf ....so on).
Also if i add the files in folder which contains files, the combo box will be updated.
Any Suggestions or hint how to start this. What to read? How to start? because i am a newbie

Thanks and best regards
 
Firstly, you can't open a PDF file in a RichTextBox control. That control can display plain text or RTF text, which is markup akin to HTML that contains both the text itself and the formatting information. If you want to display the contents of a PDF file then you will need a dedicated PDF component or else a web browser control that understands PDFs.

As for the ComboBox and the file names/paths, I would suggest that you do the following:
C#:
var folder = new DirectoryInfo(folderPath);
var files = folder.GetFiles("*.pdf");

myComboBox.DisplayMember = "Name";
myComboBox.ValueMember = "FullName";
myComboBox.DataSource = files;
myComboBox.SelectedItem = null;
That will display the names of all the PDF files in the specified folder. You can handle the SelectionChangeCommitted event of the ComboBox and get the path of the selected file from the SelectedValue property and then do with it what you will.

If you want to be able to update the file list if and when the folder contents changes then you can use a FileSystemWatcher component to monitor the folder and raise an event when a change occurs. I won't go into detail on that now. You should do some research and make your best attempt to implement it and then ask more if you encounter an issue.

You should start a new thread for that though. Please keep each thread to a single topic and each topic to a single thread. We should not be talking about populating a ComboBox, displaying PDF file contents and monitoring folders in the same thread. This is why you need to do more work to break your problems down into smaller parts and then address each part separately. Ask a specific question about a specific problem, not every part of a multi-step process all at once.
 
if i select File1.pdf and click Open button then the file open in richtext box. same as for other files (File2.pdf ....so on).
You would need a PDF library to convert the contents : The Leading PDF Platform for Developers | iText - I believe it does pdf to text too. Ideally search Nuget for pdf text : NuGet Gallery | Packages matching pdf text and take your pick.
Also if i add the files in folder which contains files, the combo box will be updated.
I advise running a while loop in a new thread to check for new files in the directory. If the files are new and don't exist in the list, they are then added to it. If any new files exist, they will be added to the combobox from the lest if the value does not already exist there. Note; this example uses a multi-threaded cross-threading example with delegation and will work as posted. However, this is pseudo code...a working mock of what you need, and you will need to implement code from #2 or similar to it for getting each file in your source directory. I've highlighted the lines where you should put said code.
C#:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool Run_While { get; set; }

        public void ExecuteThis()
        {
            /* Start a new thread and run it */
            Thread new_Thread = new Thread(Execute_Non_UI);
            new_Thread.Start();
        }

        public delegate void Call_Back_To(string new_Value);

        private void Update_UI(string this_NewString)
        {
            /* I am talking with your UI, and I can update your UI with the new value. */
            comboBox1.Items.Add(this_NewString);
        }
        private void Execute_Non_UI()
        {
            List<string> List_Of_Files = new List<string>();
            while (Run_While)
            {
                /* I am on a new thread, (the non UI thread) */
                /* Invoke the control, by calling on the delegation of the Update_UI method followed by the parameter value from the Update_UI method. */

                /* ****************** */
                if (!List_Of_Files.Contains("Some file"))
                List_Of_Files.Add("Your file paths here");
                /* Include the logic here for checking your folder for new files from your directory, to add to the combobox. */
                using (List<string>.Enumerator File = List_Of_Files.GetEnumerator())
                {
                    while (File.MoveNext())
                    {
                        if (!comboBox1.Items.Contains(File.Current))
                            comboBox1.Invoke(new Call_Back_To(Update_UI), File.Current); /* Here you apply the logic in JM's post to this code to add your PDF's to combobox. */
                    }
                }
                /* ****************** */

            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (!Run_While)
            {
                Run_While = true;
                ExecuteThis();
            }
            else
                Run_While = false;
        }
    }

By right, you really should be using WPF and not Winforms. Winforms is practically end of life. Actually, Microsoft don't want to support it any more, and they are actually doing so for the corporate customers who still use winforms apps. Microsoft are however encouraging users to move to WPF.

Anyway, I hope this helps.

Edit - fixed typo on line 41
 
Last edited:
Back
Top Bottom