How to change the colour of an object by pressing a button in a different form?

samhobbs 41 Reputation points
2021-11-09T13:20:11.347+00:00

Right now I am trying to program system in a game I am making where the player is able to change the colour of the game's paddle from a different form. The way it works is this: there will be a form (ColourChoose) containing multiple buttons of different colours. When one of these buttons is pressed, the program should change the colour of the paddle, which is located in the GameBoard window. The issue is that, when I press on one of the buttons and the GameBoard loads up, the paddle doesn't appear. Physically, it is still there (because you can see the ball bouncing off of it), but it is not visible on the GameBoard. I have not made any changes whatsoever to the visibility of the paddle, all I have done is simply tried to change the paddle's colour.

I have 2 pieces of code for this (due to being in two different forms). This is the first piece of code from the ColourChoose window:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Colour = Color.Red
End Sub

And this is the 2nd piece of code from the GameBoard window, which is supposed to be linked to the code from ColourChoose:

   Private Sub GameBoard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Colour As Color = ColourChoose.Colour
        Paddle.BackColor = Colour
End Sub

How do I make it so that the button on one form will change the colour of another object located in a different form WITHOUT the paddle hiding itself for some reason?

UPDATE: For some reason, my code in the GameBoard window is in the section that handles the LOADING of the board. For some reason my paddle doesn't have a dedicated section of code, as the code for the paddle's movement is kinda all over the place, so the paddle itself has no dedicated code.

Developer technologies VB
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2021-11-10T07:09:43.477+00:00

    Hi @GG01Coding-3690 ,
    I make some changes based on your code.
    Here is my result.
    148093-202111101.gif

    Here is the code.

    GameBoard

        Public paddlecolor As Color = Color.Orange  
        Private Sub GameBoard_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated  
            Paddle.BackColor = paddlecolor  
        End Sub  
    

    ColorChoose

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            GameBoard.paddlecolor = Color.Blue  
        End Sub  
      
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            GameBoard.paddlecolor = Color.Red  
        End Sub  
    

    Hope the code could be helpful.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-11-09T14:56:01.383+00:00

    Usually this simple solution works:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        GameBoard.Paddle.Colour = Color.Red
    End Sub
    

    Also change the Modifiers properties of Paddle control (in Form Designer) to "Public".

    0 comments No comments

  2. Peter Fleischer (former MVP) 19,341 Reputation points
    2021-11-18T16:13:12.137+00:00

    Hi,
    you can use Binding and NotifyPropertyChanged like in following demo:

    Imports System.ComponentModel  
    Imports System.Runtime.CompilerServices  
      
    Public Class Form26  
      
      Private lbl As New Label With {.Location = New Point(10, 50), .Size = New Drawing.Size(100, 100)}  
      Private vm As New ViewModel  
      Private mnu As New MainMenu()  
      Private WithEvents mnuItem As New MenuItem With {.Text = "Choose Color"}  
      
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Me.Menu = mnu  
        mnu.MenuItems.Add(mnuItem)  
        Me.Controls.Add(lbl)  
        lbl.DataBindings.Add("BackColor", vm, "LabelColor")  
      End Sub  
      
      Private Sub btn_Click(sender As Object, e As EventArgs) Handles mnuItem.Click  
        Dim dialog As New ColourChoose(vm) With {.Text = "ColourChoose", .Height = 100, .Width = 250}  
        dialog.ShowDialog()  
      End Sub  
      
      Public Class ViewModel  
        Implements INotifyPropertyChanged  
      
        Private _labelColor As Color = Color.Yellow  
        Public Property LabelColor As Color  
          Get  
            Return Me._labelColor  
          End Get  
          Set(value As Color)  
            Me._labelColor = value  
            OnPropertyChanged()  
          End Set  
        End Property  
      
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
        Friend Sub OnPropertyChanged(<CallerMemberName> Optional propName As String = "")  
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))  
        End Sub  
      End Class  
      
      Public Class ColourChoose  
        Inherits Form  
      
        Public Sub New(vm As ViewModel)  
          Me.vm = vm  
        End Sub  
      
        Private vm As ViewModel  
      
        Private WithEvents btn1 As New Button With {.Text = "Red", .Top = 10, .Left = 10, .Tag = Color.Red}  
        Private WithEvents btn2 As New Button With {.Text = "Green", .Top = 10, .Left = 150, .Tag = Color.Green}  
        Private Sub ColourChoose_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
          Me.Controls.AddRange(New Control() {btn1, btn2})  
        End Sub  
        Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click, btn2.Click  
          vm.LabelColor = CType(CType(sender, Control).Tag, Color)  
        End Sub  
      End Class  
      
    End Class  
    

    Result:

    150736-x.gif

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.