Results 1 to 19 of 19

Thread: How do i make a set of buttons appear when a button is presed???

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    How do i make a set of buttons appear when a button is presed???

    Hello guys, I am new to Visual Basic and all, as i have recently taken up the courage to start programming, thanks to my High School course. Keep in mind, that I am a 16 year old taking a course meant for 18 year olds.
    As because of being new, i started out with just designing a simple crafting giude. I already have a whole layout of buttons and tabs, and the buttons are programmed to display text in a text box. My questions is, that along with the text being displayed, I also want a set of pictures to appear in picture boxes (3 of them) when a button is pressed. i will post pictures of my form later, but any help on this would be much appreciated.
    ~Mister_Meh
    P.S.
    I didn't know where to post my thread, so i started it here.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How do i make a set of buttons appear when a button is presed???

    What programming language are you using? Visual Basic 3/4/5/6 or Visual Basic.Net?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    I'm not sure, is there any way i can figure out? or can i tell you something to help you figure it out?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How do i make a set of buttons appear when a button is presed???

    Is the program installed Visual Studios or does it say Visual Basic 2005(or higher)?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    The software that I installed on my laptop is "Visual Studio 2013" and I am designing a regular "Windows Form."

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    Now, as I promised earlier, here is my Form, and the blue circles are where i want the Picture boxes to appear.Name:  Unturned CG snip.jpg
Views: 498
Size:  32.1 KB
    and also, here is an example of the code I use.Name:  Unturned CG Code Snip.jpg
Views: 500
Size:  63.2 KB

    Now i was also wondering of replacing the buttons with a drop down menu, because from what i know, the buttons will be taking up quite some ram, correct?

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    guys?
    ~Mister_Meh

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    Soo.... I did some searching on the Interwebs(tm) and i found this string of code for VB.net,///// http://stackoverflow.com/questions/1...x-visual-basic /////I'm guessing that this is what I am looking for?
    ~Mister_Meh

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How do i make a set of buttons appear when a button is presed???

    Moderator Actions: Moved thread from Games and Graphics to VB.Net
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: How do i make a set of buttons appear when a button is presed???

    I mostly just read the VB.Net forum, so that's why at least for me it took so long for an answer. Yes, the code from that stackoverflow question will work for displaying your pictures. As for using a drop-down (or as MS calls them, a ComboBox) instead of buttons sounds like a great idea. For one thing, if you add additional items, you don't need to re-create the form just to add a new button. Also as you mentioned, since every object on the form takes up some amount of memory, the ComboBox will use less than a bunch of individual buttons. In addition to the RAM savings, you probably won't use as much code either as you can just have one method for the ComboBox when the user makes a selection instead of all of the different button CliFinally the form gets too cluttered if you just use buttons, making it more difficult for your users to figure out what they need to do.

    One thing that may be beneficial for you is that with ComboBoxes, you can set a DataTable to the ComboBox's DataSource specifying which column to use as the DisplayMember (ie what the ComboBox displays) as well as a ValueMember (ie a column that may be hidden which the program can then more easily use). The DataTable can also include other columns / fields to hold additional data that you can then use later, like in the SelectedIndexChanged event handler for your ComboBox. As an example:
    Code:
    Option Strict On
    Option Explicit On
    
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ' Although this is a little "junky"... Probably better to put this into a sub like CreateCraftingTable()
      Dim dtCrafting As New DataTable
      With dtCrafting.Columns
        .Add("IndexKey", Type.GetType(Int32))
        .Add("Name", Type.GetType(String))
        .Add("Pic1", Type.GetType(String))
        .Add("Pic2", Type.GetType(String))
        .Add("Pic3", Type.GetType(String))
        .Add("ToolTip", Type.GetType(String))
      End With
    
      With dtCrafting
        Dim drNewRow As DataRow = .NewRow()
        drNewRow("IndexKey") = 0
        drNewRow("Name") = "Arrow"
        drNewRow("Pic1") = "C:\CraftingPics\Stick.jpg"
        drNewRow("Pic2") = "C:\CraftingPics\Nail.jpg"
        drNewRow("Pic3") = "C:\CraftingPics\Arrow.jpg"
        drNewRow("ToolTip") = "1 Stick + 1 Nail = 3 Arrows"
        .Rows.Add(drNewRow)
    
        drNewRow = .NewRow()
        drNewRow("IndexKey") = 1
        drNewRow("Name") = "Buckshot"
        drNewRow("Pic1") = "C:\CraftingPics\Shell.jpg"
        drNewRow("Pic2") = "C:\CraftingPics\Nail.jpg"
        drNewRow("Pic3") = "C:\CraftingPics\Buckshot.jpg"
        drNewRow("ToolTip") = "1 Shell + 2 Nails = 3 Buckshot"
        .Rows.Add(drNewRow)
    
        drNewRow = .NewRow()
        drNewRow("IndexKey") = 2
        drNewRow("Name") = "Slug"
        drNewRow("Pic1") = "C:\CraftingPics\Shell.jpg"
        drNewRow("Pic2") = "C:\CraftingPics\Bolt.jpg"
        drNewRow("Pic3") = "C:\CraftingPics\Slug.jpg"
        drNewRow("ToolTip") = "1 Shell + 2 Bolts = 6 Slugs"
        .Rows.Add(drNewRow)
    
        [etc...]
      End With
    
      With cboCrafting 'Name of the ComboBox that you use on your form
        .DisplayMember = "Name"
        .ValueMember = "IndexKey"
        .DataSource = dtCrafting
      End With
    End Sub
    
    Private Sub cboCrafting_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboTest1.SelectedIndexChanged
      Me.txtCraftingTitle.Text = DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("Name").ToString
      Me.txtToolTip.Text = DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("ToolTip").ToString
      Me.picPic1.Image = Image.FromFile(DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("Pic1").ToString)
      Me.picPic2.Image = Image.FromFile(DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("Pic2").ToString)
      Me.picPic3.Image = Image.FromFile(DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("Pic3").ToString)
    End Sub

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    Quote Originally Posted by Pyth007 View Post
    I mostly just read the VB.Net forum, so that's why at least for me it took so long for an answer. Yes, the code from that stackoverflow question will work for displaying your pictures. As for using a drop-down (or as MS calls them, a ComboBox) instead of buttons sounds like a great idea. For one thing, if you add additional items, you don't need to re-create the form just to add a new button. Also as you mentioned, since every object on the form takes up some amount of memory, the ComboBox will use less than a bunch of individual buttons. In addition to the RAM savings, you probably won't use as much code either as you can just have one method for the ComboBox when the user makes a selection instead of all of the different button CliFinally the form gets too cluttered if you just use buttons, making it more difficult for your users to figure out what they need to do.

    One thing that may be beneficial for you is that with ComboBoxes, you can set a DataTable to the ComboBox's DataSource specifying which column to use as the DisplayMember (ie what the ComboBox displays) as well as a ValueMember (ie a column that may be hidden which the program can then more easily use). The DataTable can also include other columns / fields to hold additional data that you can then use later, like in the SelectedIndexChanged event handler for your ComboBox. As an example:
    Code:
    Option Strict On
    Option Explicit On
    
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ' Although this is a little "junky"... Probably better to put this into a sub like CreateCraftingTable()
      Dim dtCrafting As New DataTable
      With dtCrafting.Columns
        .Add("IndexKey", Type.GetType(Int32))
        .Add("Name", Type.GetType(String))
        .Add("Pic1", Type.GetType(String))
        .Add("Pic2", Type.GetType(String))
        .Add("Pic3", Type.GetType(String))
        .Add("ToolTip", Type.GetType(String))
      End With
    
      With dtCrafting
        Dim drNewRow As DataRow = .NewRow()
        drNewRow("IndexKey") = 0
        drNewRow("Name") = "Arrow"
        drNewRow("Pic1") = "C:\CraftingPics\Stick.jpg"
        drNewRow("Pic2") = "C:\CraftingPics\Nail.jpg"
        drNewRow("Pic3") = "C:\CraftingPics\Arrow.jpg"
        drNewRow("ToolTip") = "1 Stick + 1 Nail = 3 Arrows"
        .Rows.Add(drNewRow)
    
        drNewRow = .NewRow()
        drNewRow("IndexKey") = 1
        drNewRow("Name") = "Buckshot"
        drNewRow("Pic1") = "C:\CraftingPics\Shell.jpg"
        drNewRow("Pic2") = "C:\CraftingPics\Nail.jpg"
        drNewRow("Pic3") = "C:\CraftingPics\Buckshot.jpg"
        drNewRow("ToolTip") = "1 Shell + 2 Nails = 3 Buckshot"
        .Rows.Add(drNewRow)
    
        drNewRow = .NewRow()
        drNewRow("IndexKey") = 2
        drNewRow("Name") = "Slug"
        drNewRow("Pic1") = "C:\CraftingPics\Shell.jpg"
        drNewRow("Pic2") = "C:\CraftingPics\Bolt.jpg"
        drNewRow("Pic3") = "C:\CraftingPics\Slug.jpg"
        drNewRow("ToolTip") = "1 Shell + 2 Bolts = 6 Slugs"
        .Rows.Add(drNewRow)
    
        [etc...]
      End With
    
      With cboCrafting 'Name of the ComboBox that you use on your form
        .DisplayMember = "Name"
        .ValueMember = "IndexKey"
        .DataSource = dtCrafting
      End With
    End Sub
    
    Private Sub cboCrafting_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboTest1.SelectedIndexChanged
      Me.txtCraftingTitle.Text = DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("Name").ToString
      Me.txtToolTip.Text = DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("ToolTip").ToString
      Me.picPic1.Image = Image.FromFile(DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("Pic1").ToString)
      Me.picPic2.Image = Image.FromFile(DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("Pic2").ToString)
      Me.picPic3.Image = Image.FromFile(DirectCast(Me.cboCrafting.SelectedItem, DataRowView).Item("Pic3").ToString)
    End Sub
    Allrighty, thanks for the help, i really appreciate it, and I will use this information to my advantage in my form. I have also advanced in coding it, and will post pictures later once again, thanks
    ~Mister_Meh

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    Ah, hey, Pyth007, could you give me an explanation of what you mean exactly with the last part of your post regarding DataTables and DataSources? thanks.
    ~Mister_Meh

  13. #13
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: How do i make a set of buttons appear when a button is presed???

    In the example I gave, I first created a DataTable called dtCrafting that has 6 columns / fields: IndexKey, Name, Pic1, Pic2, Pic3, and ToolTip. After creating the DataTable structure, I actually fill it in with data (the "With dtCrafting" block). I start with creating an empty row using the NewRow function, and then address each column in the row using drNewRow("Column Name"), and assign that field a value. I've just done the first 3 crafting items that were in your screen-shot above as and example. I then set up the ComboBox to use the DataTable as its source (the "With cboCrafting" block). I have the ComboBox use the "Name" column as what the ComboBox will display using the DisplayMember property. Then I set the "IndexKey" as the ValueMember; this is what gets returned if you called for cboCrafting.SelectedValue for the particular item that was selected. Since it's easier and faster to search for a number vs. a string, people tend to have some number as the key value behind the scenes of the ComboBox instead of just the Text value. The last line of this block is what assigns the DataTable dtCrafting to the DataSource of the ComboBox. This tells the ComboBox to use that table to provide the data for the DisplayMember as well as the ValueMember. But as this DataTable has more than just 2 columns, you can also use the data from any of the columns when something is selected in the ComboBox. This is what I tried to demonstrate with the code in the cboCrafting_SelectedIndexChanged sub. Since when the user selects something in the ComboBox it retrieves that entire row from the DataTable, I first cast the SelectedItem as a DataRowView, and then I select the column that contains the desired data by using the Item property, essentially the same process in reverse as was used to fill the table with data.

  14. #14

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    allrighty, thanks. It will take some time for me to be able to comprehend all of this, but I will try :P.
    Thanks again
    ~Mister_Meh

  15. #15

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    Allrighty, one more question. In my crafting guide, i have tips that involve more than one method, like so: Name:  Timer Question.jpg
Views: 416
Size:  27.5 KB
    Here's the code for it too: Name:  Timer Question 2.jpg
Views: 406
Size:  15.8 KB
    Now, i need to know how to make the images switch from one to another in the same picture box using the timer, and using the set up i currently have. All i know is that i need a timer, and an interval of maybe 2000 for it.
    Thanks

    Oh, and Pyth007, with my current setup, is there a way to change the buttons into ComboBoxes without having to spend loads of time editing code AND/OR starting the entire damned thing over again?
    Last edited by Mister_Meh; Oct 26th, 2014 at 06:54 PM. Reason: adding on to the question
    ~Mister_Meh

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How do i make a set of buttons appear when a button is presed???

    There probably is not a simple way to change buttons to comboboxes, as they are pretty different. With buttons, each one is on its own. Using a ComboBox, all the items are part of one control. The two don't seem very similar, to me.

    As to the timer, based on the picture, I'm not quite clear what result you are trying to get. After all, you show wood + saw = thinner wood. That seems reasonable, so what should be changing in the pictureboxes?
    My usual boring signature: Nothing

  17. #17

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    well, as stated in the text box, there is wood + Saw = 4 Sticks OR 1 Branch = 3 Sticks
    So now i would like the timer to switch between pictures of the two sets
    ~Mister_Meh

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How do i make a set of buttons appear when a button is presed???

    Oh, so the 'result' picturebox should cycle through the images for 4 sticks or 3 sticks, right?

    What I would do would be this:

    1) I wouldn't want to be loading images all the time, so I'd make a List(of Image) at form scope and put all the images into that list just one time.

    2) I'd also have an integer variable at form scope that would start at 0.

    3) In the timer tick event, I'd increment that integer variable (the counter), and if it is larger than the List.Count - 1, I'd set it back to 0. I'd then show the image at List(counter).


    Basically, I want the images to be in a collection, such that all the timer tick has to do is determine the next item in the sequence, and put it in the Picturebox. Loading the images each time would be pretty wasteful, especially since they never need to go away until after the user stops the timer from cycling.
    My usual boring signature: Nothing

  19. #19

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Canada, British Columbia
    Posts
    54

    Re: How do i make a set of buttons appear when a button is presed???

    Okie, Never mind the Timer, i'll focus on that later, but now, since i AM new, i need to know, how do i incorporate a textbox into an array?
    here's some pictures of my form and code:Name:  1'st half of code.jpg
Views: 394
Size:  41.5 KBName:  2nd half of code.jpg
Views: 385
Size:  6.5 KB
    and here's the form, the White line on the bottom is the Text box, and in the code where you see the orange/red string of code, that's where i want to add the textBox results. if you need clarification, just ask.Name:  Form.jpg
Views: 389
Size:  22.8 KB
    ~Mister_Meh

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