Results 1 to 7 of 7

Thread: [RESOLVED] Change Font Color Using ComboBox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2019
    Posts
    65

    Resolved [RESOLVED] Change Font Color Using ComboBox

    Hi,

    I have this code and need to enable the color option, instead of fix:
    Code:
    Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
                              g.DrawString(Form2.display_data_1.Text, New Font("courier new", 10), New SolidBrush(Color.White), New Rectangle(10, 10, 1000, 1000))
                              g.Dispose()
    how can I linked it to this code? I need to get rid the Textbox and linked it to above, but how?
    Code:
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Select Case ComboBox1.SelectedIndex
                Case 0
                    TextBox1.ForeColor = System.Drawing.Color.Yellow
                Case 1
                    TextBox1.ForeColor = System.Drawing.Color.Red
                Case 2
                    TextBox1.ForeColor = System.Drawing.Color.Blue
                Case 3
                    TextBox1.ForeColor = System.Drawing.Color.Black
            End Select
    
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: Change Font Color Using ComboBox

    How about you provide an actual explanation of what you're trying to achieve? We can't tell you how to achieve something if we don't know what that something is and your description is vague at best.

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

    Re: Change Font Color Using ComboBox

    If the idea is that you want to load every color into a ComboBox and then dynamically update the ForeColor of a TextBox when the value changes, take a look at this example:
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.DisplayMember = "Text"
            ComboBox1.ValueMember = "Value"
    
            ' loop over every color
            For Each knownColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
                Dim c = Color.FromKnownColor(knownColor)
    
                ' add it to the combobox
                ComboBox1.Items.Add(New ComboBoxItem With {
                    .Text = c.Name,
                    .Value = c
                })
            Next
        End Sub
    
        Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
            ' try to get the selected color
            Dim selectedColor = TryCast(ComboBox1.SelectedItem, ComboBoxItem)
            If (selectedColor IsNot Nothing) Then
                ' set the forecolor
                TextBox1.ForeColor = selectedColor.Value
            End If
        End Sub
    
    End Class
    
    Public Class ComboBoxItem
    
        Public Property Text As String
        Public Property Value As Color
    
    End Class
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2019
    Posts
    65

    Re: Change Font Color Using ComboBox

    Quote Originally Posted by jmcilhinney View Post
    How about you provide an actual explanation of what you're trying to achieve? We can't tell you how to achieve something if we don't know what that something is and your description is vague at best.
    sorry, actually I want to make the "New SolidBrush(Color.White)" in my previous code to be a variable color option, not just one fix color.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2019
    Posts
    65

    Re: Change Font Color Using ComboBox

    Quote Originally Posted by dday9 View Post
    If the idea is that you want to load every color into a ComboBox and then dynamically update the ForeColor of a TextBox when the value changes, take a look at this example:
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.DisplayMember = "Text"
            ComboBox1.ValueMember = "Value"
    
            ' loop over every color
            For Each knownColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
                Dim c = Color.FromKnownColor(knownColor)
    
                ' add it to the combobox
                ComboBox1.Items.Add(New ComboBoxItem With {
                    .Text = c.Name,
                    .Value = c
                })
            Next
        End Sub
    
        Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
            ' try to get the selected color
            Dim selectedColor = TryCast(ComboBox1.SelectedItem, ComboBoxItem)
            If (selectedColor IsNot Nothing) Then
                ' set the forecolor
                TextBox1.ForeColor = selectedColor.Value
            End If
        End Sub
    
    End Class
    
    Public Class ComboBoxItem
    
        Public Property Text As String
        Public Property Value As Color
    
    End Class
    I can do it with text box, the problem is that I draw a string and the full code is like this (you can see my intention in red highlighted code where I want to make it variable):

    Code:
    Private Sub CAPTURAR(ByVal sender As Object, ByVal eventArgs As NewFrameEventArgs)
    
            Me.Invoke(Sub()
    
                          If ButtonVIDEO.BackColor = Color.Gainsboro Then 'IF YOU ARE NOT RECORDING ......
                              BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'PUT THE DATA IN THE BITMAP
                              PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'PRESENTS THEM AT THE PICTURE BOX
    
    
                              Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
                              g.DrawString(Form2.display_data_1.Text, New Font("courier new", 10), New SolidBrush(Color.White), New Rectangle(10, 10, 1000, 1000))
                              g.Dispose()
    
                      
    
                          GC.Collect()
                      End Sub)
    
        End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: Change Font Color Using ComboBox

    Quote Originally Posted by joko markono View Post
    sorry, actually I want to make the "New SolidBrush(Color.White)" in my previous code to be a variable color option, not just one fix color.
    Then use a variable instead of a hard-coded Color value. What's the actual problem here?

    By the way, you should ALWAYS dispose objects that support it after you're finished with them. A SolidBrush is such an object. You should create such objects with a Using statement, so they are implicitly disposed at the end of the block.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2019
    Posts
    65

    Re: Change Font Color Using ComboBox

    Quote Originally Posted by jmcilhinney View Post
    Then use a variable instead of a hard-coded Color value. What's the actual problem here?

    By the way, you should ALWAYS dispose objects that support it after you're finished with them. A SolidBrush is such an object. You should create such objects with a Using statement, so they are implicitly disposed at the end of the block.
    thank you jmcilhinney and dday9. got it works like this:

    Code:
    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            ColorDialog1.ShowDialog()
        End Sub
    
     Private Sub CAPTURAR(ByVal sender As Object, ByVal eventArgs As NewFrameEventArgs)
    
            Dim drawColour As Color = ColorDialog1.Color
            Dim drawBrush As New SolidBrush(drawColour)
    
            Me.Invoke(Sub()
    
                          If ButtonVIDEO.BackColor = Color.Gainsboro Then 'IF YOU ARE NOT RECORDING ......
                              BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'PUT THE DATA IN THE BITMAP
                              PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'PRESENTS THEM AT THE PICTURE BOX
    
                              Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
                                      g.DrawString(Form2.display_data_1.Text, New Font("courier new", 10), drawBrush, New Rectangle(10, 10, 1000, 1000))
    
                                      g.Dispose()
                              GC.Collect()
                      End Sub)
    
        End Sub
    I added the ColorDialog control on the design form.

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