Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Visual Basic code:

Openfiledialog > I want to keep using this.
* CheckedListBox
* Textbox
* Picturebox

My code below is working but I want the CheckedListBox to show only the filename(safefilename) for each image.jpg item from folder and show the fullpath with filename for each CheckedListBox.item selected to show in my textbox, I don't know and can't find the right code for this. (now I am using the fullpath aka openfiledialog1.filenames) somehow I need to combine the openfiledialog.safefilenames with openfiledialog.filenames..

What I have tried:

VB.NET
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1_Click
    openFiledialog1.Filter = "Pictures(*.jpg*)|*.jpg"

    If openFiledialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        For Each f As String In openFiledialog1.FileNames
            SuspendLayout()
            CheckedListBox1.Items.Add(f.ToString)
            ResumeLayout()
            TextBox1.Text = f
            For i As Integer = 0 To CheckedListBox1.Items.Count - 1
                CheckedListBox1.SetItemChecked(i,True)
            Next
    end if


Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
    
    TextBox1.Text = CheckedListBox1.SelectedItem

        If Not CheckedListBox1.SelectedIndex < 0 Then
            Try
                Dim img As Image = Image.FromFile(CheckedListBox1.SelectedItem.ToString())
            
                PictureBox1.BackgroundImage = img
            Catch ex As Exception
            End Try
        End If
Posted
Updated 20-Feb-22 9:19am
v2

1 solution

To get file name only, use: Path.GetFileName Method (System.IO) | Microsoft Docs[^]
To get directory name only, use: Path.GetDirectoryName Method (System.IO) | Microsoft Docs[^]

I suggest to create List[^](of FileInfo[^]) which will hold all your files. Then bind it with CheckedListBox1 via CheckedListBox.DataSource Property (System.Windows.Forms) | Microsoft Docs[^]

Note: FileInfo class has several properties/fields you can use, for example:
FileInfo.DirectoryName Property (System.IO) | Microsoft Docs[^]
FileInfo.Name Property (System.IO) | Microsoft Docs[^]

[EDIT]
benngia wrote:
Thank you for your feedback, My knowledge is still too basic for implementing this, I'm trying for days now, I'm stuck. :(


Well... seems you want to get a fish, instead of a fishing rod.
So, i've prepared LinqPad 5[^] script. As you can see, there's few differences (for example: DataGridView instead of CheckedListBox), but the idea is the same.

I hope it would be helpful to you.

VB.NET
Sub Main
	Dim mf As New MyForm
	mf.Show()
	
End Sub

' Define other methods and classes here
Public Class MyForm
	Inherits Form

	Private DataGridView1 As New DataGridView()
	Private PictureBox1 AS New PictureBox()
	Private TextBox1 AS New TextBox()
	Private Pictures As List(Of FileInfo) 
	Private InitialPath As String

	Private Sub InitializeMyControls()
		Me.Size = New Size(850, 720)
		Me.WindowState = FormWindowState.Maximized
		With DataGridView1
			.Size = New Size(300,650)
			.Location = New Point(10,10)
			.Anchor = (AnchorStyles.Top OR AnchorStyles.Left OR AnchorStyles.Bottom)
			.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill 
			.SelectionMode = DataGridViewSelectionMode.FullRowSelect
			.RowHeadersVisible = False
			AddHandler .SelectionChanged, AddressOf DataGridView1_SelectionChanged
		End With
		With TextBox1
			.Size = New Size(500,24)
			.Location = New Point(330,10)
			.Anchor = (AnchorStyles.Top OR AnchorStyles.Left OR AnchorStyles.Right)
		End With
		With PictureBox1
			.Size = New Size(500,610)
			.Location = New Point(330,40)
			.Anchor = (AnchorStyles.Top OR AnchorStyles.Left OR AnchorStyles.Right  OR AnchorStyles.Bottom)
		End With
		Me.Controls.Add(DataGridView1)
		Me.Controls.Add(TextBox1)
		Me.Controls.Add(PictureBox1)
	End Sub

	Public Sub New()
		InitializeMyControls()
		InitialPath = "C:\Users\YourWindowsLoginHere\Pictures\"
		Pictures = Directory.GetFiles(InitialPath,"*.jpg").Select(Function(x) New FileInfo(x)).ToList()
		With DataGridView1
			.DataSource = Pictures
			For i As Integer = .Columns.Count() - 1 To 1 Step -1
				.Columns(i).Visible = False
			Next 
		End With
	End Sub

	Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) 
		Dim dgr As DataGridViewRow = DataGridView1.CurrentRow
		If dgr Is Nothing Then Exit Sub
        Try
            Dim img As Image = Image.FromFile(dgr.Cells("FullName").Value)
			TextBox1.Text = dgr.Cells("DirectoryName").Value
            PictureBox1.Image = img
			PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        Catch ex As Exception
			MessageBox.Show(ex.Message)
        End Try
	End Sub

End Class
 
Share this answer
 
v4
Comments
benngia 20-Feb-22 15:47pm    
Thank you for your feedback, My knowledge is still too basic for implementing this, I'm trying for days now, I'm stuck. :(
Maciej Los 21-Feb-22 10:28am    
See updated answer ;)
benngia 22-Feb-22 5:42am    
Hi, I don't want a fish (never really liked fish anyway :))
Thank you for the datagrid class, Looks real nice and will use it for sure.

But as you can see in the picturelink below, My code is already working, My main problem is that I want the Checked/Listbox to only show the safefilename.jpg aka picture3.jpg from the chosen picturefolder with Openfiledialog and my textbox will display the fullpath + picturefilename.jpg (C:\users\xxx\xx\Picture3.jpg) for each item selected in Checked/Listbox, Note that my folder is not fixed but variable by opening with Openfiledialog. Hope you can help me with this. Thank you.

If possible I want it to look like this:
* CheckedListbox displays only Picture3.jpg
* Textbox displays C:\users\xxx\xx\Picture3.jpg
and the image shows in Picturebox

ps: Your Datagridview Shows what I want, Except that the folderpath in your textbox doesn't show the picturefilename.jpg also when clicked on the filename.

Imgur: The magic of the Internet[^</
Maciej Los 22-Feb-22 12:03pm    
Sorry, but this is your job. You've got an example. Now, you can try to implement in your code. Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900