locked
Greenhand asks for help regarding VB.net search function RRS feed

  • Question

  • Hi all,

    I would like to build a picture displayer by VB.net. Is there any statement other than 'if...elseif...then' can search from a large number of pictures in the 'myresources'?

    For example, currently, I'm using:

    Dim RM As Resources.ResourceManager
    RM = New Resources.ResourceManager("test.Resources", System.Reflection.Assembly.GetExecutingAssembly)

    If ComboBox1.Text = "MV" And ComboBox2.Text = "70" And ComboBox3.Text = "23" And ComboBox4.Text = "1150" And ComboBox5.Text = "165)" Then
    ResultsPictureBox.Image = RM.GetObject("MV_70_165_2_1150")
    End If

    But it fails to work when there's a lot of pictures.

    Is there any other statements I can use?

    Best regards.


    Tuesday, September 8, 2020 12:05 AM

Answers

  • Hi Yiran5953,

    I made a mistake and I have updated my reply.

    Since you have installed visual studio 2019, you should be able to use Interpolated Strings.

        PictureBox1.Image = CType(RM.GetObject($"_{ComboBox1.Text}_{ComboBox2.Text}"), Bitmap)

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, September 8, 2020 8:29 AM
  • Another approach, populate the ComboBox using a list that has a property for what to display and a property for the actual image name.

    Public Class ImageItem
        Public Property Text() As String
        Public Property ImageName() As String
    
        Public Overrides Function ToString() As String
            Return Text
        End Function
    End Class
    

    Here I'm simply doing a mock up where you would need to do this manually, could be you have a json or xml file that holds this information. I would use json and work with json.net which has methods to read and write to a json string that in this case would have text for display and text for image name. 

    Although I don't have time to write this up you could get most information use code from the Gist I posted in my first reply.

    Dim ImageItemList = New List(Of ImageItem)
    ImageItemList.Add(New ImageItem() With {.Text = "First", .ImageName = "FirstImage"})
    ImageItemList.Add(New ImageItem() With {.Text = "Second", .ImageName = "SecondImage"})
    
    ComboBox1.DataSource = ImageItemList


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Yiran5953 Wednesday, September 9, 2020 1:06 AM
    Tuesday, September 8, 2020 1:53 PM

All replies

  • Seem something like this (of course unsure if these combinations will work).

    RM.GetObject($"{ComboBox1.Text}_{ComboBox2.Text}_{ComboBox3.Text}_{ComboBox4.Text}_{ComboBox5.Text}")

    In the past I've not needed to search this way and used the following

    https://gist.github.com/karenpayneoregon/9e041c7fc2a6afed6843ab3a42a3b581


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, September 8, 2020 1:47 AM
  • Dear Karen,

    Thank you very much for your reply! I tried both methods. But sorry I have few more questions because I have very few knowledge regarding this.

    (1) The first one: RM.GetObject($"{ComboBox1.Text}_{ComboBox2.Text}_{ComboBox3.Text}_{ComboBox4.Text}_{ComboBox5.Text}")

    I guess when there is "", the 'ComboBox1.Text' will be read as a plain text, while cannot be read as the number within the ComboBox1. Also, I cannot use '$' within this sentence. It turns out an error always.

    (2) The second one, I guess I should use it!!! But could you please enlighten me why I cannot use 'ResourceImage.GetInstance()'? There is a blue wavy line under it.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListBox1.DisplayMember = "Name"
            ListBox1.DataSource = ResourceImages.GetInstance().BitMaps()
        End Sub
        Private Sub GetImageButton_Click(sender As Object, e As EventArgs) Handles GetImageButton.Click
            PictureBox1.Image = ResourceImages.GetInstance().GetSingleBitMap(ListBox1.Text)
        End Sub
    End Class

    Thank you again. Looking forward to hearing from you.

    Tuesday, September 8, 2020 4:32 AM
  • Hi Yiran5953,

    Thanks for your feedback.

    Interpolated Strings is available in Visual Basic 14 and later versions, so you need to install Visual Studio 2015 or later.

    >>why I cannot use 'ResourceImage.GetInstance()'? There is a blue wavy line under it.

    Could you provide more details about the error message you received ?  

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, September 8, 2020 6:41 AM
  • Hi Xingyu Zhao,

    Thank you very much for your reply. I think I'm almost there.

    I download visual studio 2019, and have a basic try as follows:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim RM As Resources.ResourceManager
            RM = New Resources.ResourceManager("WindowsApp1.Resources", System.Reflection.Assembly.GetExecutingAssembly)
            PictureBox1.Image = RM.GetObject("_{1}_{2}", ComboBox1.Text, ComboBox2.Text)
        End Sub

    However, an error happened to 'GetObject', that is, 

    Error BC30516 Overload resolution failed because no accessible 'GetObject' accepts this number of arguments.

    Could you please help me with it? Should I change to any other possible?

    Looking forward to hearing from you.

    Best regards,

    Yiran


    Tuesday, September 8, 2020 8:19 AM
  • Hi Yiran5953,

    I made a mistake and I have updated my reply.

    Since you have installed visual studio 2019, you should be able to use Interpolated Strings.

        PictureBox1.Image = CType(RM.GetObject($"_{ComboBox1.Text}_{ComboBox2.Text}"), Bitmap)

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, September 8, 2020 8:29 AM
  • All fixed. Thank you so much!!!!
    Tuesday, September 8, 2020 8:51 AM
  • Dear Xingyu,

    I have one more question. May I ask if I can do a small transfer in the name?

    For example, if ComboBox1.Text = 70mm, but it is only the number '70' in the picture name. How can I modify the code? Maybe I should define another variable?

    Looking forward to hearing from you.

    Best regards,

    Yiran

    Tuesday, September 8, 2020 12:18 PM
  • Dear Xingyu,

    I have one more question. May I ask if I can do a small transfer in the name?

    For example, if ComboBox1.Text = 70mm, but it is only the number '70' in the picture name. How can I modify the code? Maybe I should define another variable?

    Looking forward to hearing from you.

    Best regards,

    Yiran

    Use this

    Dim resultString As String = System.Text.RegularExpressions.
            Regex.Match(ComboBox1.Text, "\d+").Value
    MessageBox.Show(resultString)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, September 8, 2020 12:41 PM
  • Hi Karen,

    That works perfect where the value only I want, thanks a lot. I learnt a lot here.

    However, I still would like to know if I can replace the text in the comboBox? For example, if ComboBox1.Text = "medium rank material", while in the name of the picture, it is "MV". if ComboBox2.Text = "temperature result", while in the name of the picture, it is "temp". Sorry for bothering you so many times.

    Looking forward to hearing from you.

    Best regards,

    Yiran

    Tuesday, September 8, 2020 1:15 PM
  • Another approach, populate the ComboBox using a list that has a property for what to display and a property for the actual image name.

    Public Class ImageItem
        Public Property Text() As String
        Public Property ImageName() As String
    
        Public Overrides Function ToString() As String
            Return Text
        End Function
    End Class
    

    Here I'm simply doing a mock up where you would need to do this manually, could be you have a json or xml file that holds this information. I would use json and work with json.net which has methods to read and write to a json string that in this case would have text for display and text for image name. 

    Although I don't have time to write this up you could get most information use code from the Gist I posted in my first reply.

    Dim ImageItemList = New List(Of ImageItem)
    ImageItemList.Add(New ImageItem() With {.Text = "First", .ImageName = "FirstImage"})
    ImageItemList.Add(New ImageItem() With {.Text = "Second", .ImageName = "SecondImage"})
    
    ComboBox1.DataSource = ImageItemList


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Yiran5953 Wednesday, September 9, 2020 1:06 AM
    Tuesday, September 8, 2020 1:53 PM