如何从文件中读取文本并在列表框中显示 [英] How to read text from file and display in a listbox

查看:160
本文介绍了如何从文件中读取文本并在列表框中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我保存在程序中的图像以及从图像中标记的坐标。坐标保存在带有图像名称的文本文件中。我希望能够在列表框中选择图像并检查是否存在与所选图像同名的文本文件,然后将文本文件中的坐标显示到另一个列表框中。

这是我写的代码:



I have some images that i saved in my program along with coordinates that have been marked from the images. The coordinates are saved in a text file with the name of the image. I want to be able to select the image in a listbox and check if there is a text file with the same name as the selected image and then display the coordinate from the text file into another listbox.
Here is the code i have written:

string path = Application.StartupPath;
                   string pathString = System.IO.Path.Combine(path, "output Directory");
                   string myFile = lstImages.SelectedItem.ToString() + ".artery";
                   pathString = System.IO.Path.Combine(pathString, myFile);

                   //Read and dispalay the data from file it it exist
                   if (System.IO.File.Exists(lstImages.SelectedItem.ToString()))
                   {
                       using(StreamReader sr = new StreamReader(myFile))
                       {
                           string line;
                           while((line = sr.ReadLine()) !=null)
                           {
                               lstArtery.Items.Add(line);
                           }
                       }
                   }





这是获取图像的代码并将其显示在列表框中。这段代码工作正常。但是在选择图像时我无法显示文本文件的内容





This is the code that fetches the image and displays it in the listbox. This code is working fine. But i have not been able to display the content of the text file when the image is selected

//fetch and display the images selected in the list box
                   string filename = System.IO.Path.Combine(parentDir, lstImages.SelectedItem.ToString());
                   //picImageDisplay.Image = System.Drawing.Image.FromFile(filename);
                   //picImageDisplay.Image = (Bitmap).FromFile(filename);
                   Bitmap myImage = new Bitmap(System.Drawing.Image.FromFile(filename));
                   picImageDisplay.Image = myImage;





我需要帮助才能完成我的项目



please i need help to be able to complete my project

推荐答案

谢谢大家的贡献。我自己解决了这个问题。我非常感谢您的时间和精力。

以下是解决方案:



Thank you all for your contribution. I have solved the problem myself. I really appreciate your time and effort.
Here is the solution:

/display the coordinates of the selected file
                    string path = Application.StartupPath;
                    string pathString = System.IO.Path.Combine(path, "output Directory");
                    // string myFile = lstImages.SelectedItem.ToString() + ".artery";
                    string myFile = System.IO.Path.Combine(pathString, lstImages.SelectedItem.ToString() + ".artery" );
                    pathString = System.IO.Path.Combine(pathString, myFile);

                    //Read and dispalay the data from file it it exist
                    if (System.IO.File.Exists(pathString))
                    {
                        using (StreamReader sr = new StreamReader(pathString))
                        {
                            lstArtery.Items.Add("Artery");
                            string line;
                            while ((line = sr.ReadLine()) != null)
                            {
                                lstArtery.Items.Add(line);
                            }
                        }
                    }


这篇关于如何从文件中读取文本并在列表框中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆