Results 1 to 12 of 12

Thread: Is there a better way to do this?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    7

    Is there a better way to do this?

    Hello, I'm creating a program which will show you the statistics of champion's in a game called "League of Legends."

    I have every champion's icon and background image as an embedded resource in my program.

    Whenever someone selects a champion from the combo box in my program, I want it to show that champion's background image and icon in the form.

    This is the code for ONE champion called "Aatrox."

    Code:
    Imports System.Reflection
    Imports System.IO
    
    Public Class Form1
    
        Dim championBackground, championIcon As Stream
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
            If ComboBox1.Text.Equals("Aatrox", StringComparison.CurrentCultureIgnoreCase) Then
    
                championBackground = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoLBuddy.Aatrox_Background.jpg")
                championIcon = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoLBuddy.Aatrox_Icon.png")
                BackgroundImage = New Bitmap(championBackground)
                PictureBox1.Image = New Bitmap(championIcon)
    
            End If
        End Sub
    End Class
    Name:  OhkJLPE.jpg
Views: 269
Size:  30.2 KB

    This is the code if I add a champion called "Ahri."

    Code:
    Imports System.Reflection
    Imports System.IO
    
    Public Class Form1
    
        Dim championBackground, championIcon As Stream
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
            If ComboBox1.Text.Equals("Aatrox", StringComparison.CurrentCultureIgnoreCase) Then
    
                championBackground = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoLBuddy.Aatrox_Background.jpg")
                championIcon = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoLBuddy.Aatrox_Icon.png")
                BackgroundImage = New Bitmap(championBackground)
                PictureBox1.Image = New Bitmap(championIcon)
    
            ElseIf ComboBox1.Text.Equals("Ahri", StringComparison.CurrentCultureIgnoreCase) Then
    
                championBackground = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoLBuddy.Ahri_Background.jpg")
                championIcon = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoLBuddy.Ahri_Icon.png")
                BackgroundImage = New Bitmap(championBackground)
                PictureBox1.Image = New Bitmap(championIcon)
    
            End If
        End Sub
    End Class
    Name:  CXaKxph.jpg
Views: 262
Size:  32.3 KB

    It seems like a lot of code every time I want to support a new champion. There are 123 champions in the game, so I would have a LOT of lines just to support all the champions.

    Is there a better way to do this?

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Is there a better way to do this?

    Sounds like you could use a database (an access one would be fine) where you store the name in a column, the background as blob in another column, and the pic in another blob.. then you can use a query to select the names into the combobox, then when the user selects one you can simply set the background and that picturebox to the images from the same row in the database. Would be a few lines of code to do, but you wont have to code the names in for each person.
    You'll need to make a utility app to insert/edit/delete characters from the database though, a minor thing to make considering.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Is there a better way to do this?

    Quote Originally Posted by JuggaloBrotha View Post
    Sounds like you could use a database (an access one would be fine) where you store the name in a column, the background as blob in another column, and the pic in another blob.. then you can use a query to select the names into the combobox, then when the user selects one you can simply set the background and that picturebox to the images from the same row in the database. Would be a few lines of code to do, but you wont have to code the names in for each person.
    You'll need to make a utility app to insert/edit/delete characters from the database though, a minor thing to make considering.
    Using a blob/picture data structure to store images on a database can be sluggish when you're dealing with large amounts of requests. It's usually better to store the image location as a string and read that, you get the idea. But I agree with you though, a database is well suited for this.

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    41

    Re: Is there a better way to do this?

    If all the champions are hardcoded, and all the image file names are the same format as you have shown:

    LoLBuddy.Aatrox_Background.jpg
    LoLBuddy.Ahri_Background.jpg

    You could:

    Code:
    Dim FileName as String = "LoLBuddy." & ComboBox1.Text & "_Background.jpg"
    Where ComboBox1.Text is "Aatrox" or "Ahri" etc...

    I would of course, check the file actually exists before using, to avoid a crash

    Paul
    Last edited by PaulLaverick; Feb 4th, 2015 at 01:07 PM.

  5. #5
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Is there a better way to do this?

    Quote Originally Posted by PaulLaverick View Post
    If all the champions are hardcoded, and all the image file names are the same format as you have shown:

    LoLBuddy.Aatrox_Background.jpg
    LoLBuddy.Ahri_Background.jpg

    You could:

    Code:
    Dim FileName as String = "LoLBuddy." & ComboBox1.Text & "_Background.jpg"
    Where ComboBox1.Text is "Aatrox" or "Ahri" etc...

    I would of course, check the file actually exists before using, to avoid a crash

    Paul
    Have fun doing that with 123 champions. 2 images per person. A Database is the way this should be done, no doubt about it.

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: Is there a better way to do this?

    For only 123 entries, I would look at a a file rather than a database proper. If there were 1,000's then yea a database for sure, but with only a couple hundred, you could easily create a datatable and send it to xml pretty fast using the datatable.WriteXML method. Either way would work, but using a an xml table relieves the need for the database.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Is there a better way to do this?

    Are XML that good? Loads of people seem to recommend them. Might look into it soon.

  8. #8
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: Is there a better way to do this?

    Quote Originally Posted by Toph View Post
    Are XML that good? Loads of people seem to recommend them. Might look into it soon.
    It's not that xml is good, it's just a file format. What makes it work for the op's app is the data table's read/write XML methods. Because there are only a couple of hundred items the op is dealing with, it would be best to just load the entire table into memory rather than carry a database connection around. Using the data table method would also make portability for the app much easier.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    41

    Re: Is there a better way to do this?

    Quote Originally Posted by Toph View Post
    Have fun doing that with 123 champions. 2 images per person. A Database is the way this should be done, no doubt about it.
    I must not of been clear in my post. This would work:

    Code:
    Public Class Form1
    
        Dim championBackground, championIcon As Stream
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
            
                championBackground = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoLBuddy." & ComboBox1.Text & "_Background.jpg")
                championIcon = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoLBuddy." & ComboBox1.Text & "_Icon.png")
                BackgroundImage = New Bitmap(championBackground)
                PictureBox1.Image = New Bitmap(championIcon)
    
          End Sub
    End Class
    No need for databases, they already have a combobox, so quite straightforward...

    Unless of course, the combobox needs populating, then a basic text file with all the names in loaded on startup, add to the combobox, and the photo/icons file names can be automatically created like above? As long as none of the champion names have special chars in that can't be used in filenames... Then it gets more complicated!

  10. #10
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Is there a better way to do this?

    Quote Originally Posted by kebo View Post
    For only 123 entries, I would look at a a file rather than a database proper. If there were 1,000's then yea a database for sure, but with only a couple hundred, you could easily create a datatable and send it to xml pretty fast using the datatable.WriteXML method. Either way would work, but using a an xml table relieves the need for the database.
    The only issue with this is the xml file would be unbarebly huge in size due to xml being a text file and images as text is very bloated. The database would store the images in a slightly compressed fashion which has speed gains that xml simply wont have.
    Plus the complexity of using a DB for this (even an Access database) is the same as coding for xml use, so might as well just use a DB and have the ability to query the database using sql instead of xml parsing.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  11. #11
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Is there a better way to do this?

    Quote Originally Posted by JuggaloBrotha View Post
    The only issue with this is the xml file would be unbarebly huge in size due to xml being a text file and images as text is very bloated. The database would store the images in a slightly compressed fashion which has speed gains that xml simply wont have.
    Plus the complexity of using a DB for this (even an Access database) is the same as coding for xml use, so might as well just use a DB and have the ability to query the database using sql instead of xml parsing.
    I agree, the complexity of XML seems a bit much for this, but honestly I think so does a database. Is there an issue with storing the images in the project's resource files? Otherwise OP could keep them as files in the App Data folder.

  12. #12
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: Is there a better way to do this?

    Quote Originally Posted by JuggaloBrotha View Post
    The only issue with this is the xml file would be unbarebly huge in size due to xml being a text file and images as text is very bloated. The database would store the images in a slightly compressed fashion which has speed gains that xml simply wont have.
    Plus the complexity of using a DB for this (even an Access database) is the same as coding for xml use, so might as well just use a DB and have the ability to query the database using sql instead of xml parsing.
    Yea, I don't think I would be putting the images in the xml, nor would I be using the xml namespace. I was thinking something more like this...

    Code:
      Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
            Call makeTable()
            Call getTable()
    
        End Sub
    
        'assume the table is in the same directory as the images
        Dim xmlTablePath As String = "m:\test\table.xml"
        Dim imagePath As String = IO.Path.GetDirectoryName(xmlTablePath)
    
        Private Sub makeTable()
    
            Dim dT As New DataTable("thisTable")
            dT.Columns.Add(New DataColumn("name", GetType(String)))
            dT.Columns.Add(New DataColumn("imagePath", GetType(String)))
            dT.Columns.Add(New DataColumn("iconPath", GetType(String)))
    
            dT.PrimaryKey = {dT.Columns(0)}
    
            'add the data so the image path as icon path are relative filenames
            dT.Rows.Add({"first", "images\first.jpg", "icons\first.jpg"})
            dT.Rows.Add({"second", "images\second.jpg", "icons\second.jpg"})
            dT.Rows.Add({"third", "images\third.jpg", "icons\third.jpg"})
            dT.Rows.Add({"fourth", "images\fourth.jpg", "icons\fourth.jpg"})
    
            'save the table to xml
    
            Dim fpath As String = xmlTablePath
            dT.WriteXml(xmlTablePath, XmlWriteMode.WriteSchema)
    
        End Sub
    
    
        Dim dT As DataTable = Nothing
    
        Private Sub getTable()
    
            dT = New DataTable
            dT.ReadXml(xmlTablePath)
    
            'bind to the combo box
            ComboBox1.DataSource = dT
    
            ComboBox1.DisplayMember = "name"
            ComboBox1.ValueMember = "name"
    
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
            If ComboBox1.SelectedItem IsNot Nothing Then
    
                Dim name As String = ComboBox1.SelectedValue.ToString
                Dim row As DataRow = dT.Rows.Find(name)
    
                If row IsNot Nothing Then
    
                    Dim thisImagePath As String = IO.Path.Combine(imagePath, row("imagePath").ToString)
                    Dim thisIconPath As String = IO.Path.Combine(imagePath, row("iconPath").ToString)
    
                    Dim sb As New System.Text.StringBuilder
                    sb.AppendLine(name)
                    sb.AppendLine(thisImagePath)
                    sb.AppendLine(thisIconPath)
                    MessageBox.Show(sb.ToString)
    
    
                End If
    
    
    
            End If
    
    
    
        End Sub
    Because the op is only dealing with a couple hundred items, loading and saving the table is too bad.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width