Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i change color of picturebox choose by another form2 picturebox
consider i have four picture box in my form1 as boot form
now i have second form with four backcolored picturebox with differently colored picturebox

how when run the application
and click a picture-box from2 populates
now color has to be supplied by this

use the method sender...

What I have tried:

VB
Dim jb As PictureBox = DirectCast(sender, PictureBox)
      jb.BackColor = form1.sender.backcolor
Posted
Updated 7-Jul-16 2:41am
v2
Comments
CoderzF1 6-Jul-16 15:42pm    
what exactly are you trying to do here? are you trying to make something like a color picker? if so, have you tried using a color dialog instead of another form with colored pictureboxes? your question is not entirely clear.
Member 10974007 6-Jul-16 20:28pm    
yes a color picker don't like the inbuilt color picker

1 solution

Try these steps:

Step 1: First off let us start with a blank project for learning purposes.
So start by creating a new project and name it what ever you want.
After the project loads, proceed to step 2.

Step 2: Go ahead and add a second form to your project. Maybe call this form frmColorPicker. On frmColorPicker, Add the number of pictureboxes you want for the color palette and set their BackColor properties to the desired colors.
    -Double click any of the PictureBoxes to bring up the Code Editor. You will notice that it will bring up the PictureBoxes's Click Handler.
    -On the line where it says Private Sub Picturebox_Click.....Handles...
we are going to tell the parser that this Sub Procedure will handle more than one object's click event by appending each object name separated by a comma
VB
Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click '<--This is where we append the objects
    ActivePbox.BackColor = sender.backcolor
    Me.Close()
End Sub

    -Also while we are in the code editor, Go ahead and add the code below
VB
Dim ActivePbox As PictureBox
Public Function SelectColor(PictureboxToChange As PictureBox)
    ActivePbox = PictureboxToChange
    Me.ShowDialog()
End Function

    -And add the code from the click event handler from above inside the picturebox's click event handler.

Step 3: Go back to Form1. Add the pictureboxes you want to the form.
    -Double click one of the pictureboxes to open the code editor. You will notice again that the click event handler will be there. Go ahead and append the other pictureboxes like you did in step 2.
    -Here we will add the following code.
VB
frmColorPicker.SelectColor(sender)


----------------------------------------------------------------------------------
CHECK YOUR CODE:
Form1 code should look similar to the following code:
VB
Public Class Form1

    Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click, PictureBox3.Click, PictureBox4.Click
        frmColorPicker.SelectColor(sender)
    End Sub

End Class


frmColorPicker code should look similar to the following code:
VB
Public Class frmColorPicker
    Dim ActivePbox As PictureBox
    Public Function SelectColor(PictureboxToChange As PictureBox)
        ActivePbox = PictureboxToChange
        Me.ShowDialog()
    End Function

    Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click
        ActivePbox.BackColor = sender.backcolor
        Me.Close()
    End Sub
End Class


----------------------------------------------------------------------------------
Does this help you to do what you are wanting?
 
Share this answer
 
v2
Comments
Member 10974007 7-Jul-16 11:30am    
can i have full codes ?
as am new to vb.net ...
dont work me
Member 10974007 7-Jul-16 11:32am    
there are multiple picture box in form-1(like 4 of them) and 12 picture in form-2 (colors )
i need full explanation
CoderzF1 18-Jul-16 22:11pm    
out of curiosity, did this solution help you to do what you are trying to do? i havent heard back in a couple of weeks, so i wasnt sure if you have seen the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900