Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. image button

image button

Scheduled Pinned Locked Moved Visual Basic
helpcsharpquestion
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lisana
    wrote on last edited by
    #1

    I creat a button with image menually, like below Dim aImageButton As New Button tpgContact.Controls.Add(aImageButton) aImageButton.Name = entity aImageButton.Text = "" aImageButton.Image = Image.FromFile("C:\vb.net\client\images\checkbox_checked.gif") aimagebutton.ImageAlign = ContentAlignment.MiddleCenter aimagebutton.FlatStyle = FlatStyle.Flat aImageButton.Location = New Point(10, y) aImageButton.Width = 20 aimagebutton.Height = 20 AddHandler aImageButton.Click, AddressOf ImageButtonClickHandler the problem I have in here is "aImageButton.Image = Image.FromFile("C:\vb.net\client\images\checkbox_checked.gif")" this line of code. If the app install in other computer, then it won't be able to find the same location. What should I do? Is there any way to change the location to fix the need? Lisa

    D R 2 Replies Last reply
    0
    • L Lisana

      I creat a button with image menually, like below Dim aImageButton As New Button tpgContact.Controls.Add(aImageButton) aImageButton.Name = entity aImageButton.Text = "" aImageButton.Image = Image.FromFile("C:\vb.net\client\images\checkbox_checked.gif") aimagebutton.ImageAlign = ContentAlignment.MiddleCenter aimagebutton.FlatStyle = FlatStyle.Flat aImageButton.Location = New Point(10, y) aImageButton.Width = 20 aimagebutton.Height = 20 AddHandler aImageButton.Click, AddressOf ImageButtonClickHandler the problem I have in here is "aImageButton.Image = Image.FromFile("C:\vb.net\client\images\checkbox_checked.gif")" this line of code. If the app install in other computer, then it won't be able to find the same location. What should I do? Is there any way to change the location to fix the need? Lisa

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      There are (2) things you should know about using File Paths: 1) Don't EVER hard code path's like you did in this statement. 2) Don't EVER assume the file is in the "Current Directory" or "Working Directory". Always use fully qualified paths to files. Right now, you're probably trying to figure out how to use a fully qualified path without knowing what that path is??? Easy. What you should be doing is building a path using a "known" location as a reference. The Application.StartupPath property will tell you from which folder your .EXE was loaded, or launched, from. If your .GIF file is in the same folder as your .EXE, you have a "known" starting point. All you have to do now is build the complete path to the file. But wait a minute! Most beginners will use string concatenation to do this. Something like:

      Dim myGifFile As String = Application.StartupPath & "\\myGifFile.GIF"
      

      Don't EVER do this! Instead, use a class that's specifically designed to do this, and more, using the proper path seperation characters and build rules for the platform that it's running on. The Path class does just this. Specifically, what you're looking for is:

      Dim myGifFile As String = Path.Combine( Application.StartupPath, "myGifFile.GIF" )
      

      Path.Combine takes (2) arguments. The first being the beginning of the path and the second being the ending. You don't have to worry about prepended or appended path seperation characters either. Path.Combine will take care to make sure that the path it generates is in a legal format. Assuming that your .GIF image is in the same folder as the .EXE, your code should look more like this:

      ' Build a new button control
      Dim aImageButton As New Button
      With aImageButton
      Dim imagePath As String = Path.Combine( Application.StartupPath, "checkbox_checked.gif" )
      .Image = Image.FromFile( imagePath )
      .Name = entity
      .Text = ""
      .ImageAlign = ContentAlignment.MiddleCenter
      .FlatStyle = FlatStyle.Flat
      .Location = New Point(10, y)
      .Width = 20
      .Height = 20
      End With
      ' Attach the Click Event handler
      AddHandler aImageButton.Click, AddressOf ImageButtonclickHandler
      ' Finally, add the control to the form
      tpgContact.Controls.Add(aImageButton)

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      L 1 Reply Last reply
      0
      • D Dave Kreskowiak

        There are (2) things you should know about using File Paths: 1) Don't EVER hard code path's like you did in this statement. 2) Don't EVER assume the file is in the "Current Directory" or "Working Directory". Always use fully qualified paths to files. Right now, you're probably trying to figure out how to use a fully qualified path without knowing what that path is??? Easy. What you should be doing is building a path using a "known" location as a reference. The Application.StartupPath property will tell you from which folder your .EXE was loaded, or launched, from. If your .GIF file is in the same folder as your .EXE, you have a "known" starting point. All you have to do now is build the complete path to the file. But wait a minute! Most beginners will use string concatenation to do this. Something like:

        Dim myGifFile As String = Application.StartupPath & "\\myGifFile.GIF"
        

        Don't EVER do this! Instead, use a class that's specifically designed to do this, and more, using the proper path seperation characters and build rules for the platform that it's running on. The Path class does just this. Specifically, what you're looking for is:

        Dim myGifFile As String = Path.Combine( Application.StartupPath, "myGifFile.GIF" )
        

        Path.Combine takes (2) arguments. The first being the beginning of the path and the second being the ending. You don't have to worry about prepended or appended path seperation characters either. Path.Combine will take care to make sure that the path it generates is in a legal format. Assuming that your .GIF image is in the same folder as the .EXE, your code should look more like this:

        ' Build a new button control
        Dim aImageButton As New Button
        With aImageButton
        Dim imagePath As String = Path.Combine( Application.StartupPath, "checkbox_checked.gif" )
        .Image = Image.FromFile( imagePath )
        .Name = entity
        .Text = ""
        .ImageAlign = ContentAlignment.MiddleCenter
        .FlatStyle = FlatStyle.Flat
        .Location = New Point(10, y)
        .Width = 20
        .Height = 20
        End With
        ' Attach the Click Event handler
        AddHandler aImageButton.Click, AddressOf ImageButtonclickHandler
        ' Finally, add the control to the form
        tpgContact.Controls.Add(aImageButton)

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        L Offline
        L Offline
        Lisana
        wrote on last edited by
        #3

        Thanks..that's so helpful... I have another issue in my vb app. here is my coding: private function contactView() 'connect to data and do the query..... 'user the DataReader to read the value from the query... 'Query: SELECT primary_contact, entity_id from Conatcts Do While DR.Read() primaryContact = DR.GetBoolean(0) entity = DR.GetInt32(1) 'creat a image button If primaryContact = True Then Dim aImageButton As New Button With aImageButton Dim imagePath As String = Path.Combine(Application.StartupPath, "checkbox_checked.gif") .Image = Image.FromFile(imagePath) .Name = entity .Text = "" .ImageAlign = ContentAlignment.MiddleCenter .FlatStyle = FlatStyle.Flat .Location = New Point(10, y) .Width = 20 .Height = 20 End With ' Attach the Click Event handler AddHandler aImageButton.Click, AddressOf ImageButtonclickHandler ' Finally, add the control to the Form tpgContact.Controls.Add(aImageButton) Else Dim aImageButton As New Button With aImageButton Dim imagePath As String = Path.Combine(Application.StartupPath, "checkbox_blank.gif") .Image = Image.FromFile(imagePath) .Name = entity .Text = "" .ImageAlign = ContentAlignment.MiddleCenter .FlatStyle = FlatStyle.Flat .Location = New Point(10, y) .Width = 20 .Height = 20 End With ' Attach the Click Event handler AddHandler aImageButton.Click, AddressOf ImageButtonclickHandler ' Finally, add the control to the Form tpgContact.Controls.Add(aImageButton) End If y += 25 Loop end function Public Sub ImageButton1ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs) results = CType(CType(sender, Button).Name, Int32) Dim DA As New OleDbDataAdapter objConn.Open() DA.SelectCommand = New OleDbCommand DA.SelectCommand.Connection = objConn DA.SelectCommand.CommandType = CommandType.Text DA.SelectCommand.CommandText = "UPDATE Contacts

        D 1 Reply Last reply
        0
        • L Lisana

          Thanks..that's so helpful... I have another issue in my vb app. here is my coding: private function contactView() 'connect to data and do the query..... 'user the DataReader to read the value from the query... 'Query: SELECT primary_contact, entity_id from Conatcts Do While DR.Read() primaryContact = DR.GetBoolean(0) entity = DR.GetInt32(1) 'creat a image button If primaryContact = True Then Dim aImageButton As New Button With aImageButton Dim imagePath As String = Path.Combine(Application.StartupPath, "checkbox_checked.gif") .Image = Image.FromFile(imagePath) .Name = entity .Text = "" .ImageAlign = ContentAlignment.MiddleCenter .FlatStyle = FlatStyle.Flat .Location = New Point(10, y) .Width = 20 .Height = 20 End With ' Attach the Click Event handler AddHandler aImageButton.Click, AddressOf ImageButtonclickHandler ' Finally, add the control to the Form tpgContact.Controls.Add(aImageButton) Else Dim aImageButton As New Button With aImageButton Dim imagePath As String = Path.Combine(Application.StartupPath, "checkbox_blank.gif") .Image = Image.FromFile(imagePath) .Name = entity .Text = "" .ImageAlign = ContentAlignment.MiddleCenter .FlatStyle = FlatStyle.Flat .Location = New Point(10, y) .Width = 20 .Height = 20 End With ' Attach the Click Event handler AddHandler aImageButton.Click, AddressOf ImageButtonclickHandler ' Finally, add the control to the Form tpgContact.Controls.Add(aImageButton) End If y += 25 Loop end function Public Sub ImageButton1ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs) results = CType(CType(sender, Button).Name, Int32) Dim DA As New OleDbDataAdapter objConn.Open() DA.SelectCommand = New OleDbCommand DA.SelectCommand.Connection = objConn DA.SelectCommand.CommandType = CommandType.Text DA.SelectCommand.CommandText = "UPDATE Contacts

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Lisana wrote: I can't recall the function in the ImageButton1ClickHandler, what should I do? If you want to call the code in the handler, just move the code to another method outside the handler.

          Public Sub ImageButton1ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
          Dim results As Int32 = CType(CType(sender, Button).Name, Int32)
          BindDataSources(results)
          End Sub

          Public Sub BindDataSources(ByVal entityID As Int32)
          ' I'm assuming client_entityID is a "global" variable
          objConn.Open()
          Dim DA As New OleDbDataAdapter
          DA.SelectCommand = New OleDbCommand
          With DA.SelectCommand
          .Connection = objConn
          .CommandType = CommandType.Text
          .CommandText = "UPDATE Contacts set primary_contact = 0 WHERE client_entity_id = " & client_entityID
          .ExecuteNonQuery()
          End With
           
          Dim DA2 As New OleDbDataAdapter
          DA2.SelectCommand = New OleDbCommand
          With DA2.SelectCommand
          .Connection = objConn
          .CommandType = CommandType.Text
          .CommandText = "UPDATE Contacts set primary_contact = 1 WHERE entity_id = " & entityID
          .ExecuteNonQuery()
          End With
           
          objConn.Close()
          End Sub

          Now you can call BindDataSources from anywhere in your code, so long as you provide it an entityID. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          L 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Lisana wrote: I can't recall the function in the ImageButton1ClickHandler, what should I do? If you want to call the code in the handler, just move the code to another method outside the handler.

            Public Sub ImageButton1ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim results As Int32 = CType(CType(sender, Button).Name, Int32)
            BindDataSources(results)
            End Sub

            Public Sub BindDataSources(ByVal entityID As Int32)
            ' I'm assuming client_entityID is a "global" variable
            objConn.Open()
            Dim DA As New OleDbDataAdapter
            DA.SelectCommand = New OleDbCommand
            With DA.SelectCommand
            .Connection = objConn
            .CommandType = CommandType.Text
            .CommandText = "UPDATE Contacts set primary_contact = 0 WHERE client_entity_id = " & client_entityID
            .ExecuteNonQuery()
            End With
             
            Dim DA2 As New OleDbDataAdapter
            DA2.SelectCommand = New OleDbCommand
            With DA2.SelectCommand
            .Connection = objConn
            .CommandType = CommandType.Text
            .CommandText = "UPDATE Contacts set primary_contact = 1 WHERE entity_id = " & entityID
            .ExecuteNonQuery()
            End With
             
            objConn.Close()
            End Sub

            Now you can call BindDataSources from anywhere in your code, so long as you provide it an entityID. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            L Offline
            L Offline
            Lisana
            wrote on last edited by
            #5

            I think you misunderstood my meaning, I want to call the function contactView not the update query. Public Sub ImageButtonClickHandler(ByVal sender As Object, ByVal e As System.EventArgs) results = CType(CType(sender, Button).Name, Int32) contactView() 'give me error end sub it gave me error when I call the contactView() in the ImageButtonClickHandler The main thing I want to do in here is when the blankImageButton Click, then go the database to change the pimary_contact to 1, the other pimary_contact=1 to 0, at the same time, the button image changes as well, in the output, the imagebutton didn't change when I click, but the database is updated. I have to close the window, then reopen again, then it shows the change. Lisa

            D 1 Reply Last reply
            0
            • L Lisana

              I think you misunderstood my meaning, I want to call the function contactView not the update query. Public Sub ImageButtonClickHandler(ByVal sender As Object, ByVal e As System.EventArgs) results = CType(CType(sender, Button).Name, Int32) contactView() 'give me error end sub it gave me error when I call the contactView() in the ImageButtonClickHandler The main thing I want to do in here is when the blankImageButton Click, then go the database to change the pimary_contact to 1, the other pimary_contact=1 to 0, at the same time, the button image changes as well, in the output, the imagebutton didn't change when I click, but the database is updated. I have to close the window, then reopen again, then it shows the change. Lisa

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Whopps! My mistake. What's the error? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              L 2 Replies Last reply
              0
              • D Dave Kreskowiak

                Whopps! My mistake. What's the error? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                L Offline
                L Offline
                Lisana
                wrote on last edited by
                #7

                The main thing I want to do in here is when the blankImageButton Click, then go the database to change the pimary_contact to 1, the other pimary_contact=1 to 0, at the same time, the button image changes as well, in the output, the imagebutton didn't change when I click, but the database is updated. I have to close the window, then reopen again, then it shows the change. Lisa

                D 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Whopps! My mistake. What's the error? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  L Offline
                  L Offline
                  Lisana
                  wrote on last edited by
                  #8

                  expression is not a method. Lisa

                  1 Reply Last reply
                  0
                  • L Lisana

                    The main thing I want to do in here is when the blankImageButton Click, then go the database to change the pimary_contact to 1, the other pimary_contact=1 to 0, at the same time, the button image changes as well, in the output, the imagebutton didn't change when I click, but the database is updated. I have to close the window, then reopen again, then it shows the change. Lisa

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    You've got a major design flaw in there. Every time you call contactView, you're created new buttons. But, you're never removing the old ones that you created. What are you trying to do with the buttons? Implement paging? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    L 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      You've got a major design flaw in there. Every time you call contactView, you're created new buttons. But, you're never removing the old ones that you created. What are you trying to do with the buttons? Implement paging? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      L Offline
                      L Offline
                      Lisana
                      wrote on last edited by
                      #10

                      How can I remove the old buttons when before contactView() called? those buttons are checked and unchecked buttons, if you have 5 contacts, there is only one pimary contact that would be the checked buttons, if you want to change your pimary contact, then click one unchecked button, the one you just clicked will become the pimary contact(changed to checked button), the old pimary contact changed to unchecked button. like this: -Unchecked X checked 5 lists, when the second record click, then old new - - - X - - X - - - Lisa

                      R 1 Reply Last reply
                      0
                      • L Lisana

                        I creat a button with image menually, like below Dim aImageButton As New Button tpgContact.Controls.Add(aImageButton) aImageButton.Name = entity aImageButton.Text = "" aImageButton.Image = Image.FromFile("C:\vb.net\client\images\checkbox_checked.gif") aimagebutton.ImageAlign = ContentAlignment.MiddleCenter aimagebutton.FlatStyle = FlatStyle.Flat aImageButton.Location = New Point(10, y) aImageButton.Width = 20 aimagebutton.Height = 20 AddHandler aImageButton.Click, AddressOf ImageButtonClickHandler the problem I have in here is "aImageButton.Image = Image.FromFile("C:\vb.net\client\images\checkbox_checked.gif")" this line of code. If the app install in other computer, then it won't be able to find the same location. What should I do? Is there any way to change the location to fix the need? Lisa

                        R Offline
                        R Offline
                        rwestgraham
                        wrote on last edited by
                        #11

                        TextBox, Button, CheckBox and most (not all but most) NET controls can display images from an ImageList. You will find that your code is simpler, easier to deploy, and easier to manage in general if you just abandon the whole approach of loading images from a file. Put them in an ImageList control instead. Robert

                        1 Reply Last reply
                        0
                        • L Lisana

                          How can I remove the old buttons when before contactView() called? those buttons are checked and unchecked buttons, if you have 5 contacts, there is only one pimary contact that would be the checked buttons, if you want to change your pimary contact, then click one unchecked button, the one you just clicked will become the pimary contact(changed to checked button), the old pimary contact changed to unchecked button. like this: -Unchecked X checked 5 lists, when the second record click, then old new - - - X - - X - - - Lisa

                          R Offline
                          R Offline
                          rwestgraham
                          wrote on last edited by
                          #12

                          If there is some compelling reason whay you absolutely have to add the buttons at run-time - like the numebr of buttons changes depending on the number of contacts, then load them all one time when the form is initialized. After you create each button, also add it to a local collection. Then when a person clicks on a button, enumerate the local collection, set each button's state to unchecked, then continue to process the click event to perform the query and reset the newly checked contact. This will be much faster than trying to constantly add and remove buttons and their event handlers from the form. You also do not need to keep track of the current state of any given button, because you simply reset them all to unchecked whenever one is clicked. Unless you have a lot of buttons, simply enumerating the list will be about as fast, and much simpler than writing code to keep track of the the button state. Robert

                          L 1 Reply Last reply
                          0
                          • R rwestgraham

                            If there is some compelling reason whay you absolutely have to add the buttons at run-time - like the numebr of buttons changes depending on the number of contacts, then load them all one time when the form is initialized. After you create each button, also add it to a local collection. Then when a person clicks on a button, enumerate the local collection, set each button's state to unchecked, then continue to process the click event to perform the query and reset the newly checked contact. This will be much faster than trying to constantly add and remove buttons and their event handlers from the form. You also do not need to keep track of the current state of any given button, because you simply reset them all to unchecked whenever one is clicked. Unless you have a lot of buttons, simply enumerating the list will be about as fast, and much simpler than writing code to keep track of the the button state. Robert

                            L Offline
                            L Offline
                            Lisana
                            wrote on last edited by
                            #13

                            it's very complicate in this data binding..I have a very hard time to get around all of those. this is my form look like: PC|View|Name|Phone|email PC column: imagebutton View: button --when the user click button, then open a form to view the detail, and the user and change the data, when the form closed, in this form's data has to be updated as well, this is a hard part I haven't found a way to fix the problem..in my app now, I can't get to refresh and load the update data, I have to close this form and open again to get the newest data. Name: textbox phone: combobox, binding all the phone number in db to combobox email: button - when the user click the button, then auto start a outlook to send email. I can't use datagrid because I can't find a way to add those controls to datagrid. so I only can use the dataReader to menually creat those needed in my app. that is very annoying to do that. but I can't find any other way to do it. IF you know a better way to do it, please tell me. I will very appricate it. Lisa

                            D 1 Reply Last reply
                            0
                            • L Lisana

                              it's very complicate in this data binding..I have a very hard time to get around all of those. this is my form look like: PC|View|Name|Phone|email PC column: imagebutton View: button --when the user click button, then open a form to view the detail, and the user and change the data, when the form closed, in this form's data has to be updated as well, this is a hard part I haven't found a way to fix the problem..in my app now, I can't get to refresh and load the update data, I have to close this form and open again to get the newest data. Name: textbox phone: combobox, binding all the phone number in db to combobox email: button - when the user click the button, then auto start a outlook to send email. I can't use datagrid because I can't find a way to add those controls to datagrid. so I only can use the dataReader to menually creat those needed in my app. that is very annoying to do that. but I can't find any other way to do it. IF you know a better way to do it, please tell me. I will very appricate it. Lisa

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              Wow! Talk about doing it the hard way! :-D The DataGrid is the tool choice for this project, but admittedly, it's not an easy tool to learn to use. Ummmm... Try seeing what you can glean from this[^] little article by Omri. There's even a link in it to show you how to get a ComboBox column into a DataGrid. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                              L 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                Wow! Talk about doing it the hard way! :-D The DataGrid is the tool choice for this project, but admittedly, it's not an easy tool to learn to use. Ummmm... Try seeing what you can glean from this[^] little article by Omri. There's even a link in it to show you how to get a ComboBox column into a DataGrid. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                                L Offline
                                L Offline
                                Lisana
                                wrote on last edited by
                                #15

                                Thanks;) how about a button inside datagrid, and when it click, open another window? This is I always want to do it, but I just can't get around it, it includes some drawing and you need to creat a button by your own...do you have any experience for the button inside datagrid. I know the combobox inside datagrid is much easier to do it than button. I have buttons and combobox at the same time. Lisa

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups