vb.net

Change button(textbox etc..) backcolor and forecolor with rgb slider from another form


I made a form with a RGB slider which generates a color, and in that when I close this form my r,g,b variables (integers) can be sent to my other form to be used to change the selected button's color. Here's what I got so far... (some of the code is in French rouge =red , vert = green and bleu=blue as for lbl stands for label and tb for trackbar)

Public Property r As Integer
Public Property g As Integer
Public Property b As Integer

Private Sub tbrouge_Scroll(sender As Object, e As EventArgs) Handles tbrouge.Scroll
    lblrouge.Text = tbrouge.Value
    prgb.BackColor = Color.FromArgb(tbrouge.Value, tbvert.Value, tbbleu.Value)
End Sub

Private Sub tbvert_Scroll(sender As Object, e As EventArgs) Handles tbvert.Scroll
    lblvert.Text = tbvert.Value
    prgb.BackColor = Color.FromArgb(tbrouge.Value, tbvert.Value, tbbleu.Value)
End Sub

Private Sub tbbleu_Scroll(sender As Object, e As EventArgs) Handles tbbleu.Scroll
    lblbleu.Text = tbbleu.Value
    prgb.BackColor = Color.FromArgb(tbrouge.Value, tbvert.Value, tbbleu.Value)
End Sub

Private Sub btn_ok_Click(sender As Object, e As EventArgs) Handles btn_ok.Click
    r = tbrouge.Value
    g = tbvert.Value
    b = tbbleu.Value
    Me.Close()
End Sub

Solution

  • Use a custom event.

    Public Class frmColorChange
     Public Property r As Integer
     Public Property g As Integer
     Public Property b As Integer
     Public Event ColorChanged(r As Double, g As Double, b As Double)
    Private Sub tbrouge_Scroll(sender As Object, e As EventArgs) Handles tbrouge.Scroll
     lblrouge.Text = tbrouge.Value
     prgb.BackColor = Color.FromArgb(tbrouge.Value, tbvert.Value, tbbleu.Value)
    End Sub
    
    Private Sub tbvert_Scroll(sender As Object, e As EventArgs) Handles tbvert.Scroll
     lblvert.Text = tbvert.Value
     prgb.BackColor = Color.FromArgb(tbrouge.Value, tbvert.Value, tbbleu.Value)
    End Sub
    
    Private Sub tbbleu_Scroll(sender As Object, e As EventArgs) Handles tbbleu.Scroll
     lblbleu.Text = tbbleu.Value
     prgb.BackColor = Color.FromArgb(tbrouge.Value, tbvert.Value, tbbleu.Value)
    End Sub
    
    Private Sub btn_ok_Click(sender As Object, e As EventArgs) Handles btn_ok.Click
     r = tbrouge.Value
     g = tbvert.Value
     b = tbbleu.Value
     'raise the event
     RaiseEvent ColorChanged(r, g, b)
     Me.Close()
    End Sub
    End Class
    

    Usage:

    'on the calling form
    Dim frm As New frmColorChanged
    Addhandler frm.ColorChanged, AddressOf ColorChanged
    frm.ShowDialog()
    
    
    'event handler
    Private Sub ColorChanged(r As Double, g As Double, b As Double)
      'use the variables to set the new color
    End Sub