-
Sep 5th, 2023, 02:43 AM
#1
Thread Starter
Lively Member
[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
-
Sep 5th, 2023, 06:30 AM
#2
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.
-
Sep 5th, 2023, 08:48 AM
#3
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
-
Sep 5th, 2023, 08:31 PM
#4
Thread Starter
Lively Member
Re: Change Font Color Using ComboBox
 Originally Posted by jmcilhinney
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.
-
Sep 5th, 2023, 08:37 PM
#5
Thread Starter
Lively Member
Re: Change Font Color Using ComboBox
 Originally Posted by dday9
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
-
Sep 5th, 2023, 09:38 PM
#6
Re: Change Font Color Using ComboBox
 Originally Posted by joko markono
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.
-
Sep 6th, 2023, 01:53 AM
#7
Thread Starter
Lively Member
Re: Change Font Color Using ComboBox
 Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|