在列表框中单击一下即可显示图像文件和文本文件 [英] Display image file and textfile on one click in listbox
问题描述
使用folderdialog浏览的图像和文本文件夹
但是我无法通过点击图像名称打开相应的文本文件
例如:如果图像名称为DataFile1.jpg,则从列表框中单击DataFile1.jpg时,相应的DataFile1.txt不会自动打开
由于下面我可以在两个不同的列表框中浏览图像文件夹和文本文件夹并打开它
我尝试过:
i试图通过单独点击不同的列表框单独打开
我的代码:
Image and textfile folder browsed using folderdialog
But i am not able to open the respective textfile by clicking on image name
e.g: if image name is "DataFile1.jpg" then respective "DataFile1.txt" is not getting opened automatically when clicked on "DataFile1.jpg" from listbox
Due to below i am able to browse both images folder and textfile folder in two different listboxes and open it
What I have tried:
i tried to open both seperately by clicking separately from different listbox
My code:
Private Sub Agregar_Archivos2(ByVal Nombre_Largo As String)
Dim nombre_Archivo As String = Path.GetFileName(Nombre_Largo)
If Not Mi_imagen.ContainsKey(nombre_Archivo) Then
Mi_imagen.Add(nombre_Archivo, Nombre_Largo)
Lis.Items.Add(nombre_Archivo)
End If
End Sub
Private Sub Agregar_Archivostext(ByVal Nombre_Largo As String)
Dim nombre_Archivo As String = Path.GetFileName(Nombre_Largo)
If Not Mi_textn.ContainsKey(nombre_Archivo) Then
Mi_textn.Add(nombre_Archivo, Nombre_Largo)
ListBox1.Items.Add(nombre_Archivo)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
opp.Filter = "Image Documents (*.jpg) | *.jpg" 'etc, etc..
If opp.ShowDialog() = DialogResult.OK Then
Dim archivos() As String = opp.FileNames
Dim i As Integer
For i = 0 To archivos.Length - 1
Agregar_Archivos2(archivos(i))
Next
End If
Catch ex As Exception
Exit Sub
End Try
End Sub
Private Sub Agregar_Archivos(v As String)
Throw New NotImplementedException()
End Sub
Sub imagen()
Try
Pic.Image = BM1
BM1 = Image.FromFile(Obtener_nombre_Largo(Lis.SelectedItem.ToString()))
Pic.SizeMode = PictureBoxSizeMode.AutoSize
FlowLayoutPanel1.AutoScroll = True
FlowLayoutPanel1.Controls.Add(Pic)
nom.Text = "Image File Name is: " & Lis.Text
Catch ex As Exception
Exit Sub
End Try
End Sub
Sub textn() Try TextBox1.Text = File.ReadAllText(Obtener_nombre_Largotext(ListBox1.SelectedItem.ToString()))
'Pic.SizeMode = PictureBoxSizeMode.AutoSize
txtfilename.Text = "File Name: " & ListBox1.Text
Catch ex As Exception
Exit Sub
End Try
End Sub
推荐答案
除 Richard MacCutchan [ ^ ]的评论......
比方说,你的*。jpg
文件是位于目录中:
In addition to Richard MacCutchan[^]'s comments...
Let say, your*.jpg
file is located in a directory:
string jpgFileName = @"C:\mydir\myfile.jpg";
现在,你想要设置av可以引用位于同一目录中的文本文件名:
Now, you want to set a variable to refer a text file name which is located in the same directory:
string txtFileName = Path.Combine(Path.GetDirectoryName(jpgFileName),
Path.GetFileNameWithoutExtension(jpgFileName) + ".txt");
//above variable stores: "C:\mydir\myfile.txt"
现在你可以打开一个 *。jpg
和一个 * .txt
file;)
知道了吗?
Now you can open a *.jpg
and a *.txt
file ;)
Got it?
这篇关于在列表框中单击一下即可显示图像文件和文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!