Xaml.vb Gradient Color Code - vb.net

public Class BotaoEmergencia
Private pressedcolorvermelho As SolidColorBrush = "#d73f3e".ToBrush()
Private pressedfundoamarelo As SolidColorBrush = DirectCast(New BrushConverter().ConvertFrom("#FFF500"), SolidColorBrush)
Private pressedDisabledColor As SolidColorBrush = DirectCast(New BrushConverter().ConvertFrom("#DA251D"), SolidColorBrush)
Private pressedcontornolinhas As SolidColorBrush = DirectCast(New BrushConverter().ConvertFrom("#da251d"), SolidColorBrush)
Public Shared ReadOnly IsPressedProperty As DependencyProperty = DependencyProperty.Register("IsPressed", GetType(Boolean), GetType(BotaoEmergencia), New PropertyMetadata(False, AddressOf OnIsPressedChanged))
Public Property IsPressed() As Boolean
Get
Return DirectCast(GetValue(IsPressedProperty), Boolean)
End Get
Set(value As Boolean)
SetValue(IsPressedProperty, value)
End Set
End Property
Public Shared Sub OnIsPressedChanged(source As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim botao As BotaoEmergencia = TryCast(source, BotaoEmergencia)
botao.ActualizarDesenho()
End Sub
Private Sub UserControl_IsEnabledChanged(sender As Object, e As DependencyPropertyChangedEventArgs) Handles Me.IsEnabledChanged
ActualizarDesenho()
End Sub
Protected Sub ActualizarDesenho()
If IsEnabled Then
If IsPressed Then
Canvas.SetTop(elpCamadaVermelha1, 9.45)
Canvas.SetLeft(elpCamadaVermelha1, -20)
Canvas.SetTop(elpCamadaVermelha2, -122)
Canvas.SetLeft(elpCamadaVermelha2, -61)
Canvas.SetTop(elpContornoBotao, 16.8)
Canvas.SetLeft(elpContornoBotao, 78)
elpContornoBotao.Width = 52
elpContornoBotao.Height = 52
elpContornoBotao.Stroke = If(IsEnabled, pressedcontornolinhas, pressedcontornolinhas)
elpCamadaVermelha1.Fill = If(IsEnabled, pressedcolorvermelho, pressedcolorvermelho)
elpCamadaVermelha2.Stroke = If(IsEnabled, pressedcontornolinhas, pressedcontornolinhas)
elpCamadaVermelha2.StrokeThickness = 0.8
elpFundoAmarelo.Fill = If(IsEnabled, pressedfundoamarelo, pressedfundoamarelo)
Hello guys im making some buttons for a software that im working, and i need to give some 3D vision thats why i wanna use gradient.
This is my code for solidcolorbrush, i wanna make some Gradientcolorbrush but i don't know how can i make it from here...
Can you guys help?

Got it thx anyway -> -> ->
Private normalgradientbigcircle As LinearGradientBrush = New LinearGradientBrush(Color.FromRgb(219, 142, 141), Color.FromRgb(212, 7, 7), New Point(0.5, 0), New Point(0.5, 1))

Related

Find control by text and assign custom property with Ctype or item is control in dictionary

I have a EmailTextbox like this:
Imports System.ComponentModel
Public Class EmailTextBox
Inherits TextBox
Private _error As String = ""
Public Sub New()
''
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim brush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, brush, 0, 0)
End Sub
Protected Overrides Sub OnValidating(e As CancelEventArgs)
MyBase.OnValidating(e)
Dim isValidEmail As Boolean = ValidatedEmail(Me.Text)
Dim _emailNotValid As String = Global.WindowsApp1.My.Resources.MessageResource.EmailNotValid
Dim _errorBackColor As String = Global.WindowsApp1.My.Resources.MessageResource.ErrorBackColor
e.Cancel = Not isValidEmail Or Me.ValidationError <> ""
'
If Me.ValidationError <> "" Then
_emailNotValid = Me.ValidationError
End If
'
If Not isValidEmail Then
errProvider.SetError(Me, _emailNotValid)
Me.BackColor = ColorTranslator.FromHtml(_errorBackColor)
Else
errProvider.SetError(Me, "")
Me.BackColor = Color.White
End If
End Sub
' custom property for database validation
Public Property ValidationError() As String
Get
Return _error
End Get
Set(ByVal Value As String)
_error = Value
End Set
End Property
End Class
I drag it into form and set name is txtEmail. How do i set ValidationError prop in two cases like this:
Case 1:
CType(Me.Controls("txtEmail"), TextBox).ValidationError = 456
Case 2:
Private Items As New Dictionary(Of String, Control) From {
{"txtEmail", txtEmail}
}
Items("txtEmail").ValidationError = 456
or even shorter:
Me.Controls("txtEmail").ValidationError = 456
Thanks for your answer.
These are some options for achieving what you're looking for.
Private Sub SetValidator()
'if you know that there is only one
Dim myCtrl As EmailTextBox = Me.Controls.OfType(Of EmailTextBox).First
myCtrl.ValidationError = 457
'if you want to find by name
DirectCast(Me.Controls.Find("txtEmail", True)(0), EmailTextBox).ValidationError = 456
End Sub

lists on custom listview are all the same, should be different in Visual Basic

How to display a list on a custom listview that varies like this :
true
Not like this:
false
where to see each other, everything is the same?
I created Daftar.vb using User Controls (Windows Forms) and I created a custom list view using FlowLayoutPanel and in it I entered the Daftar Toolbox
code :
Daftar.vb
Imports System.ComponentModel
Public Class Daftar1
#Region "Properties"
Private _title As String
Private _message As String
Private _icon As Image
<Category("Custom Props")>
Public Property Title As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
judul.Text = value
End Set
End Property
<Category("Custom Props")>
Public Property Message As String
Get
Return _message
End Get
Set(ByVal value As String)
_message = value
deskripsi.Text = value
End Set
End Property
<Category("Custom Props")>
Public Property Icon As Image
Get
Return _icon
End Get
Set(ByVal value As Image)
_icon = value
gambar.Image = value
End Set
End Property
#End Region
End Class
RestaurantApp.vb
Public Class RestaurantApp
Private Sub PopulateItems()
Dim listItems As Daftar1() = New Daftar1(20) {}
Dim i As Integer
For i = 0 To listItems.Length - 1
listItems(i) = New Daftar1()
listItems(i).Title = "vdsvhd"
listItems(i).Message = "Penjelasan"
If i <> 0 Then
panelDaftar.Controls.Add(listItems(i))
End If
Next
End Sub
Private Sub RestaurantApp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Me.PopulateItems()
End Sub
End Class
After I search on Stackoverflow and Youtube, I've managed to find the answer. The following is the code for RestaurantApp.vb :
Public Class RestaurantApp
Private Sub PopulateItems()
Dim listItems As Daftar1() = New Daftar1(5) {}
Dim judul As String() = New String(4) {"Burger", "Sosis", "Nasi", "Pizza", "Ayam"}
Dim penjelasan As String() = New String(4) {"a", "b", "c", "d", "e"}
Dim i As Integer
For i = 0 To listItems.Length - 1
If i < judul.Length And i < penjelasan.Length Then
listItems(i) = New Daftar1()
listItems(i).Title = judul(i)
listItems(i).Message = penjelasan(i)
If i <> 0 Then
panelDaftar.Controls.Add(listItems(i))
End If
End If
Next
End Sub
Private Sub RestaurantApp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Me.PopulateItems()
End Sub
End Class
Thank you #anu6is and Stackoverflow

add button to TreeNode

Good day.
There is a custom control that adds a button to each Node
Imports System.Windows.Forms.VisualStyles
Public Class CustomTreeView
Inherits TreeView
Private buttonRect As New Rectangle(80, 2, 50, 26)
Private ReadOnly stringFormat As StringFormat
Public Sub New()
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
DrawMode = TreeViewDrawMode.OwnerDrawText
ShowLines = False
FullRowSelect = True
ItemHeight = 30
stringFormat = New StringFormat With {
.Alignment = StringAlignment.Near,
.LineAlignment = StringAlignment.Center
}
End Sub
Protected Overrides Sub OnDrawNode(ByVal e As DrawTreeNodeEventArgs)
e.Graphics.DrawString(e.Node.Text, Me.Font, New SolidBrush(Me.ForeColor), e.Bounds, stringFormat)
ButtonRenderer.DrawButton(e.Graphics, New Rectangle(e.Node.Bounds.Location + New Size(buttonRect.Location), buttonRect.Size), "btn", Me.Font, True, If(e.Node.Tag IsNot Nothing, CType(e.Node.Tag, PushButtonState), PushButtonState.Normal))
End Sub
Protected Overrides Sub OnNodeMouseClick(ByVal e As TreeNodeMouseClickEventArgs)
Select Case e.Node.Tag
Case Nothing, Is <> PushButtonState.Pressed
Return
End Select
e.Node.Tag = PushButtonState.Normal
MessageBox.Show(e.Node.Text & " clicked")
' force redraw
e.Node.Text = e.Node.Text
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
Dim tnode As TreeNode = GetNodeAt(e.Location)
If tnode Is Nothing Then
Return
End If
Dim btnRectAbsolute As New Rectangle(tnode.Bounds.Location + New Size(buttonRect.Location), buttonRect.Size)
If btnRectAbsolute.Contains(e.Location) Then
tnode.Tag = PushButtonState.Pressed
tnode.Text = tnode.Text
End If
End Sub
End Class
tell me how you can display the button only to the first (main) nod?
And how, when you click on this button, not display a message, but let's say call some procedure?
There is no built-in button tree node. But you can create a custom tree node having a button yourself. This custom tree node inherits from TreeNode. To improve extensibility, we declare an interface for tree nodes having a DrawNode method:
Imports System.Windows.Forms.VisualStyles
Public Interface ICustomDrawTreeNode
Sub DrawNode(ByVal e As DrawTreeNodeEventArgs, buttonState As PushButtonState)
End Interface
We also create a module containing some settings used in the custom tree view and in the custom tree node
Module Settings
Public ReadOnly ButtonRect As New Rectangle(80, 2, 50, 26)
Public ReadOnly TextStringFormat = New StringFormat() With {
.Alignment = StringAlignment.Near,
.LineAlignment = StringAlignment.Center,
.FormatFlags = StringFormatFlags.NoClip Or StringFormatFlags.FitBlackBox Or StringFormatFlags.LineLimit
}
End Module
We can then implement a button node like this
Imports System.Windows.Forms.VisualStyles
Public Class ButtonTreeNode
Inherits TreeNode
Implements ICustomDrawTreeNode
Private ReadOnly buttonText As String
Public Sub New(text As String, buttonText As String)
MyBase.New(text)
Me.buttonText = buttonText
End Sub
Public Sub DrawNode(e As DrawTreeNodeEventArgs, buttonState As PushButtonState) _
Implements ICustomDrawTreeNode.DrawNode
Dim font As Font = e.Node.TreeView.Font
' Draw Text to the left of the button
Dim rect As Rectangle = New Rectangle(
e.Node.Bounds.Location,
New Size(Settings.ButtonRect.Left, e.Bounds.Height))
e.Graphics.DrawString(e.Node.Text, font, Brushes.Black, rect, Settings.TextStringFormat)
' Draw the button
rect = New Rectangle(
e.Node.Bounds.Location + Settings.ButtonRect.Location,
Settings.ButtonRect.Size)
ButtonRenderer.DrawButton(e.Graphics, rect, buttonText, font, True, buttonState)
End Sub
End Class
It has a Private ReadOnly buttonText As String to store the text of the button. The normal node text and the button text are passed in the constructor of ButtonTreeNode:
Public Sub New(text As String, buttonText As String)
The DrawNode method will be called be the CustomTreeView in OnDrawNode.
In CustomTreeView I declared a NodeButtonClick event that will be raised when the button of a node is clicked. You can then handle this event in the form. When you select the CustomTreeView in the designer, this new event will appear in the "Action" section of the events.
Imports System.ComponentModel
Imports System.Windows.Forms.VisualStyles
Public Class CustomTreeView
Inherits TreeView
<Category("Action")>
Public Event NodeButtonClick(e As TreeNodeMouseClickEventArgs)
Private _isButtonPressed As Boolean
Public Sub New()
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
DrawMode = TreeViewDrawMode.OwnerDrawText
ShowLines = False
FullRowSelect = True
ItemHeight = 30
End Sub
Protected Overrides Sub OnDrawNode(e As DrawTreeNodeEventArgs)
Dim customDrawNode As ICustomDrawTreeNode = TryCast(e.Node, ICustomDrawTreeNode)
If customDrawNode Is Nothing Then ' Normal text node.
e.Graphics.DrawString(e.Node.Text, Font, Brushes.Black, e.Node.Bounds, Settings.TextStringFormat)
Else
customDrawNode.DrawNode(e, If(_isButtonPressed, PushButtonState.Pressed, PushButtonState.Normal))
End If
End Sub
Protected Overrides Sub OnNodeMouseClick(e As TreeNodeMouseClickEventArgs)
If _isButtonPressed Then
_isButtonPressed = False
Refresh()
Dim buttonNode = TryCast(e.Node, ButtonTreeNode)
If buttonNode IsNot Nothing Then
RaiseEvent NodeButtonClick(e)
End If
End If
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
Dim buttonNode = TryCast(GetNodeAt(e.Location), ButtonTreeNode)
If buttonNode IsNot Nothing Then
Dim btnRectAbsolute As New Rectangle(
buttonNode.Bounds.Location + Settings.ButtonRect.Location,
Settings.ButtonRect.Size)
_isButtonPressed = btnRectAbsolute.Contains(e.Location)
If _isButtonPressed Then
Refresh()
End If
End If
End Sub
End Class
In the form you can write
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TreeView1.Nodes.Add("Text")
TreeView1.Nodes.Add(New ButtonTreeNode("Caption", "Button"))
End Sub
Private Sub TreeView1_NodeButtonClick(e As TreeNodeMouseClickEventArgs) _
Handles TreeView1.NodeButtonClick
MessageBox.Show(e.Node.Text & " clicked")
End Sub
End Class
This adds a normal text node and a custom button node to the TreeView. It also handles the NodeButtonClick of the custom TreeView.

Changing 'lamp' Colour Indicator within the Graphical User Interface (Visual Studio 2019)

I would like to change the colour within a single circular indicator within a Graphical User Interface, so that it shows when an action is completed or when it fails ['two tone green/red LED']. I've looked through the inbuilt presets within the Toolbox but have been unable find anything.
I would therefore be grateful for any assistance.
I've found this code on the msdn.microsoft.com forum, which changes the colour of the centre of the 'dot' when you press the RadioButton.
Private Sub RadioButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles RadioButton1.Paint, RadioButton2.Paint
If DirectCast(sender, RadioButton).Checked Then
e.Graphics.FillEllipse(Brushes.Red, New RectangleF(2.5, 4.7, 7.2, 7.2))
End If
So have incorporated it into my code, its not at all elegant and there is clearly room for improvement, but it does work.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If My.Computer.Network.Ping("192.168.0.1") Then
RadioButton1.ForeColor = Color.Green
RadioButton1.ForeColor = Color.Black
Else
RadioButton1.ForeColor = Color.Red
RadioButton1.ForeColor = Color.Black
End If
End Sub
Private Sub RadioButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles RadioButton1.Paint
If My.Computer.Network.Ping("192.168.0.1") Then
e.Graphics.FillEllipse(Brushes.Green, New RectangleF(2.5, 4.7, 7.2, 7.2))
Else
e.Graphics.FillEllipse(Brushes.Red, New RectangleF(2.5, 4.7, 7.2, 7.2))
End If
End Sub
Explanation: when the 'Test Network' button is pressed it sends out a network ping, and depending upon the return the Network RadioButton 'dot' changes colour to either Green or Red,
Here's ON/OFF LED control.
Add a new class to your project, name it say OnOffLed.vb, copy the code below and paste it in the new class.
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class OnOffLed
Inherits Panel
Public Enum LedState
[On]
Off
End Enum
Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or
ControlStyles.OptimizedDoubleBuffer Or
ControlStyles.ResizeRedraw Or
ControlStyles.UserPaint, True)
UpdateStyles()
End Sub
Private _state As LedState = LedState.Off
Public Property State As LedState
Get
Return _state
End Get
Set(value As LedState)
_state = value
Invalidate()
End Set
End Property
Private _onText As String
Public Property OnText As String
Get
Return _onText
End Get
Set(value As String)
_onText = value
Invalidate()
End Set
End Property
Private _offText As String
Public Property OffText As String
Get
Return _offText
End Get
Set(value As String)
_offText = value
Invalidate()
End Set
End Property
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim rec As New Rectangle(2, 2, Height - 5, Height - 5)
Dim recText As New Rectangle(Height + 2, 1, Width - (Height - 2), Height)
Dim G As Graphics = e.Graphics
G.SmoothingMode = SmoothingMode.AntiAlias
G.Clear(Parent.BackColor)
If _state = LedState.On Then
Dim cb As New ColorBlend With {
.Colors = {Color.Green, Color.DarkGreen, Color.Green},
.Positions = {0, 0.5, 1}
}
Using lgb As New LinearGradientBrush(rec, Color.Empty, Color.Empty, 90.0F) With {.InterpolationColors = cb}
G.FillEllipse(lgb, rec)
End Using
Else
Dim cb As New ColorBlend With {
.Colors = {Color.Red, Color.DarkRed, Color.Red},
.Positions = {0, 0.5, 1}
}
Using lgb As New LinearGradientBrush(rec, Color.Empty, Color.Empty, 90.0F) With {.InterpolationColors = cb}
G.FillEllipse(lgb, rec)
End Using
End If
G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
Using br As New SolidBrush(ForeColor)
Using sf As New StringFormat With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Center}
G.DrawString(If(_state = LedState.On, _onText, _offText), Font, br, recText, sf)
End Using
End Using
End Sub
End Class
Rebuild your project.
In the ToolBox under your project's component tab, you'll find the new control. OnOffLed. Drop it in your form as you drop any other control.
You can toggle the state through the State property, set different text if you need that for each state through the OnText and OffText properties.
Usage Example:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If My.Computer.Network.Ping("192.168.2.01") Then
OnOffLed1.State = OnOffLed.LedState.On
Else
OnOffLed1.State = OnOffLed.LedState.Off
End If
End Sub
Good luck.

Vertical text in windows form using onPaint

I found this Vertical Text on Button Control I thought that looks easy but I can't get it to work.
Public Class VerticalButton3
Inherits System.Windows.Forms.Button
Private _VerticalText As String
Public Property VerticalText() As String
Get
Return _VerticalText
End Get
Set(ByVal value As String)
_VerticalText = value
End Set
End Property
Private Fmt As New StringFormat
Public Sub New()
Fmt.Alignment = StringAlignment.Center
Fmt.LineAlignment = StringAlignment.Center
End Sub
Protected Overrides Sub OnPaint(ByVal PaintEvt As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint (PaintEvt)
PaintEvt.Graphics.TranslateTransform(Width, 0)
PaintEvt.Graphics.RotateTransform (90)
PaintEvt.Graphics.DrawString(_VerticalText, Font, Brushes.Black, New Rectangle(0, 0, Height, Width), Fmt)
End Sub
End Class
But I get both vertical and horizontal text.
I tried to use Public Overrides Property Text() similar to Vertical Label Control in VB.NET but that didn't work either
How do I get the vertical text only?
I tested your code. It works as you require if you set the Text Property to ""
Here is the code I tried
Private WithEvents vbtn As New VerticalButton3
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vbtn.Top = 0
vbtn.Left = 0
vbtn.Text = "" ' Note Text property is set to ""
vbtn.VerticalText = "Vertical"
vbtn.Height = 100
Controls.Add(vbtn)
End Sub
Alternatively you can use Text="" in the Constructor of your class
Public Sub New()
Fmt.Alignment = StringAlignment.Center
Fmt.LineAlignment = StringAlignment.Center
Text = ""
End Sub
Edit: I think it is a better to override the Text Property itself, because if by mistake the Text property is set the both may show up. Here is how you can override the Text Property (you may no longer need the VerticalText property).
Public Shadows Property Text
Get
Return _VerticalText
End Get
Set(ByVal value)
_VerticalText = value
End Set
End Property