-
Jan 15th, 2021, 10:32 AM
#1
Thread Starter
Addicted Member
Toggle between two images based on filename
Hi ... I am trying to toggle an image in a single image box. I am loading an image file using 'open file dialogue'. Once that image is loaded, I want to be able to toggle to the filename that matches the end charatcers criteria. For example: Load "Image1_TOP", now I want to have a toggle to allow loading "Image1_BTM". The filenames will all have a match with either _TOP or _BTM on the end. This is what I am using, but I am sending myself in circles. I can get the opposite image to load, but I can't get back to the original loaded to get that toggle action. I'm sure there must be a simpler way to do this, but I figured I have to be starting with the longest route possible. Thanks for your help and time!
Code:
Dim sInput As String = myShrtFileName
Dim CCASide As String
CCASide = sInput.Substring(sInput.Length - 3)
If CheckBox1.CheckState = CheckState.Checked And CCASide = "TOP" Then
Dim str As String = myShrtFileName
myShrtFileName = (str.Substring(0, str.Length - 3))
Me.PictureBox1.Image = Nothing
PictureBox1.Image = Image.FromFile(prgPath & "\CCA_Images\" & myShrtFileName & "BTM.bmp")
CCASide = "BTM"
ElseIf CheckBox1.CheckState = CheckState.Checked And CCASide = "BTM" Then
Dim str As String = myShrtFileName
myShrtFileName = (str.Substring(0, str.Length - 3))
Me.PictureBox1.Image = Nothing
PictureBox1.Image = Image.FromFile(prgPath & "\CCA_Images\" & myShrtFileName & "TOP.bmp")
CCASide = "TOP"
End If
If CheckBox1.CheckState = CheckState.Unchecked Then
Me.PictureBox1.Image = Nothing
PictureBox1.Image = Image.FromFile(prgPath & "\CCA_Images\" & myShrtFileName & "TOP.bmp")
ElseIf CheckBox1.CheckState = CheckState.Unchecked Then
Me.PictureBox1.Image = Nothing
PictureBox1.Image = Image.FromFile(prgPath & "\CCA_Images\" & myShrtFileName & "BTM.bmp")
End If
-
Jan 15th, 2021, 10:58 AM
#2
Re: Toggle between two images based on filename
Here is how I would approach it:
- Get the filename from the open file dialog
- Store the value in a global variable without the _TOP or _BTM suffix
- Load the image by appending the suffix based on the checked state of your Checkbox
Here is an example:
Code:
Private partialFilename As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using ofd = New OpenFileDialog()
With ofd
' setup the properties
.Filter = "Images (*.bmp)|*.bmp"
.RestoreDirectory = True
' if the user picks an image
If (.ShowDialog() = DialogResult.Ok) Then
Dim filename = IO.Path.GetFileNameWithoutExtension(.FileName)
Dim directory = IO.Path.GetDirectoryName(.FileName)
partialFilename = IO.Path.Combine(directory, filename)
End If
End With
End Using
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) CheckBox1.CheckedChanged
PictureBox1.Load(If(CheckBox1.Checked, $"{partialFilename}_TOP.bmp", $"{partialFilename}_BTM.bmp"))
End Sub
-
Jan 15th, 2021, 01:07 PM
#3
Thread Starter
Addicted Member
Re: Toggle between two images based on filename
thanks dday9... not sure what I am doing wrong, but when I select the image, it will not load into the picturebox. I don't get any error, just nothing happens. Once I try to toggle, it throws an error: Cannot find "image1_TOP_TOP" So if it adds the "_TOP", how would I rename my files to be different? Another thing I just noticed... if I load a _BTM first, the error pops up: Cannot find "Image1_BTM_TOP"
-
Jan 19th, 2021, 03:47 PM
#4
Thread Starter
Addicted Member
Re: Toggle between two images based on filename
Based on the code you provided... how can I detect that I am going to load -TOP or -BTM? I just discovered that this causes me an issue where I am writing to a text file based on the file (-top or -btm). So somehow I need to differentiate them. Thanks
-
Jan 19th, 2021, 03:59 PM
#5
Re: Toggle between two images based on filename
The file is loaded dynamically every time based on the ternary If in the CheckChanged event. If the CheckBox is checked then it loads {partialFilename}_TOP.bmp otherwise it loads {partialFilename}_BTM.bmp
-
Jan 20th, 2021, 04:46 AM
#6
Re: Toggle between two images based on filename
I would not be loading the file every time unless you're confident that you'll only toggle a small number of times. Otherwise, you'll be creating a large number of Image objects from the same. In that case, I'd create both Image objects up front and assign them to variables or put them in an array, then get them from there each time. If you did stick to the suggested method, I'd make one small change to the code, from this:
Code:
PictureBox1.Load(If(CheckBox1.Checked, $"{partialFilename}_TOP.bmp", $"{partialFilename}_BTM.bmp"))
to this:
Code:
PictureBox1.Load($"{partialFilename}_{If(CheckBox1.Checked, "TOP", "BTM")}.bmp")
-
Jan 20th, 2021, 09:52 AM
#7
Thread Starter
Addicted Member
Re: Toggle between two images based on filename
jmcilhinney... thank you for your time and help! I am certainly open to suggestions as I am totally open on how to approach this. For what you were explaining, do you have an example of how that would be done? For my project, the user will load either top or btm first based on what they have, they will then toggle back and forth a few times. Thanks again!
-
Jan 20th, 2021, 10:18 AM
#8
Re: Toggle between two images based on filename
The point of an example is to demonstrate a principle and you then implement the principle in your own scenario. An "example" of what I described would be the whole thing, so it wouldn't be an example; it would be a solution. What you need to do is break it down and determine what the principles are, look for examples of those, then use what you learned to build a solution. I said:
I'd create both Image objects up front and assign them to variables
Are you really saying that you can't find examples of creating Image objects or assigning objects to variables? I would hope not. Once you've got the Image objects, all you need is some logic to switch between two objects. You already have an example of that. You just need to understand the principle and then adapt it to do what you need to do. You have access to all the examples you need. You need to understand them and then apply some logic to adapt the principles to your own scenario. If you try something and it doesn't work, then you have a question to ask here.
-
Jan 20th, 2021, 10:30 AM
#9
Thread Starter
Addicted Member
Re: Toggle between two images based on filename
Thanks for the push on that, I will start looking into this option and report back with any issues.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|