Dynamicly adding text to a menu item picture.

NJDubois

Well-known member
Joined
May 15, 2008
Messages
84
Programming Experience
Beginner
Sorry for the constant questions, I am very appreciative of the entire VB.Net Forum community's patience. As of a week ago, I had not even looked at vb.net and if it were vb6 I'd be able to do all of this on my own no problem!

As stated in other posts, I am making a Menu program. Currently I am working on skinning the menu. What I would like to do is have an array of menu items, each member in the array would have a label, up image, down image and over image. In vb6 I could use type.

Type Menu_Type
Dim iText As String
Dim iUp As Image
Dim iDown As Image
Dim iOver As Image
End Type

Dim Menu_Items(10) As Menu_Type


But I cannot do type, so I use

Public bmp_but_up As New Bitmap("c:\somebmp.bmp")

Public Class Menu_Type
Public iText as String
Public iUp as Bitmap
...​
End Class

Public Menu_Items(10) As Menu_Type


But when I try to assign a bitmap to menu_items(0).iUp

Menu_Items(0).iUp = bmp_but_up

I get the error "Object reference not set to an instance of an object."
If I understand correctly, I am trying to set an object as something that is not an object.

What should I research to help me understand how to make this work? I have loaded and scaled the image for each state of a button, no problem. But because I do not know what the text for each button will be until run time, I'd like to have each member of the menu list defined in an array, and the 3 states of the button stored with the text in menu_item(x).iText added to the image and have it assigned to that menu item.

So
Menu_Item(1).txt = "Menu Item 1"
and iup, idown, iover will be a correctly scaled image with the text "Menu Item 1" on each. This way, when it comes time to draw menu item 1, I do not have to go through the code of setting the image, adding the text and then draw the image, and when the state of the button changes have to do it all over again. I can just say

Private Sub picbox1.Mousemove(...)
picbox1.Image = Menu_Item(cur_item).iOver​
End Sub


Again, I thank everyone so much for taking time to help me. I hope that someday I can return the favor!
 
I would suggest looking into Systems.Collections.Generic and setting it up so you have a List(Of Menu_Type)

VB.NET:
Public Class Form1
    Public bmp_but_up As New Bitmap("c:\somebmp.bmp")
    Public Menu_Items(10) As Menu_Type

    Private Sub MenuSetup()
        Menu_Items(0).iUp = bmp_but_up
    End Sub
End Class

Public Class Menu_Type
    Public iText As String
    Public iUp As Image
    Public iDown As Image
    Public iOver As Image
End Class
 
Classes are reference types, you have to create instances with New keyword. "New Menu_Type" creates a new object of this type. Structures are value types, so you can declare an array of this type and assign values just like for example an Integer.
What should I research to help me understand how to make this work?
Using arrays creates lots of unnecessary work for yourself, learn some about collections. Apart from that, grabbing a VB.Net book should get you fast up to pace. Just do it.
 
Thank you!

Systems.Collections.Generic led me to Dictionarys.
I found that Dictionarys can only have 2 values, the key and the data. But while doing research I found a nice code sample where the poster made a class of his own, and used a Structure to put 2 value types into the one value. I worked with it, and made it so I can have my 3 button images and a key.

MattP: I have been working with a few books from my local library. I always start learning something new with a basic run through of the language. My problem is I can read over something time and time again and it goes right over my head, but if I can find sample code I can learn what I need to learn in minutes, and understand it better. I guess I learn more from doing than study.

As usual, thank you so much! With in hours I was pointed in a direction and found exactly what I needed. You guys are the greatest!
 
What I got after hours...

I have attached my test program. Its all source.

Its a little crazy, And I plan on cleaning it up as much as I can.

My main reason for posting it here is to get what I would like to call a grade on it. I'd like to know if there was an easier way for what I did to be done! My goal is to learn as much as I can and if I did something really backwards please feel free to throw as many stones as you can!

Also, if someone else is trying to do the same kind of thing they might find this post and be able to follow my foot steps.

And please, feel free to change the up, down, and over bitmaps! If I like it I might use um in my final menu program ; )

If it were not for the hours and hours I have been programming already, I would make a theme editor so you can change font, text color and alignment. We will just leave that in a coming soon list!

source code posts that saved my life:
Adding text to an image
Measuring a string in pixels
Scaling images
Dictionary's

There might be one or two more.

Thanks for all the help. I can't say it enough!
 

Attachments

  • working_with_images.zip
    17.6 KB · Views: 31
Last edited by a moderator:
Back
Top