Author Topic: Extending ADHMI Controls  (Read 7437 times)

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Extending ADHMI Controls
« on: October 19, 2015, 11:39:10 AM »
I'd like to add a Label to the Pilotlight Control.

When the color changes it would display a piece of text, ie Open/Close, On/Off, Up/Down, etc.

I'm developing my first ADHMI project and like the looks of the pilot light, and would be using it for various different items.

I've written a lot of VB code over the years, but very few controls, and none in quite some time.

I tried looking for instructions on something like this, but couldn't find anything similar.

Thanks.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Extending ADHMI Controls
« Reply #1 on: October 19, 2015, 05:35:02 PM »
Modifying controls or writing custom controls can get quite involved. When we give our training, it can take up to 2 weeks of training before people master custom controls.

So let's start with the bare minimum to give you something to start playing with. Right Click the folder AdvancedHMIControls\PurchasedControls and select Add->New Class.

Name the class PilotLightEx and add this code:
Code: [Select]
Public Class PilotLightEx
    Inherits AdvancedHMIControls.PilotLight

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)

        e.Graphics.DrawString("My added Text", Me.Font, Brushes.Black, 5, 5)
    End Sub
End Class

Now build the project and see if you have a PilotLightEx in your ToolBox

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Extending ADHMI Controls
« Reply #2 on: October 20, 2015, 07:53:39 AM »
Archie,

I appreciate the help.  This worked well for a start.

Of course I'd like to make most of that into a set of properties/states.

Let me see what I can come up with and get back to you.

I realize it won't be easy, but as I've learned over the years, most things aren't.

Thanks,

Rich

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Extending ADHMI Controls
« Reply #3 on: October 20, 2015, 08:11:18 AM »
If you were able to get that much working, let's take another step. Add this code to your PilotLightEx:
Code: [Select]
    Protected Overrides Sub OnValueChanged(e As EventArgs)
        MyBase.OnValueChanged(e)

        If MyBase.Value = "True" Then
            MyBase.Text = "On"
        Else
            MyBase.Text = "off"
        End If
    End Sub
« Last Edit: October 20, 2015, 08:13:28 AM by Archie »

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Extending ADHMI Controls
« Reply #4 on: October 20, 2015, 09:35:27 AM »
I went a bit beyond in playing around.

Here is what I have now.
Code: [Select]

Public Class PilotLightEx
    Inherits AdvancedHMIControls.PilotLight

    Private Textx As Single
    Private Texty As Single

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim myTextSize As New SizeF
        If Me.Value = True Then
            myTextSize = e.Graphics.MeasureString(LightColorOnText, Me.Font)
            CenterText(myTextSize)

            e.Graphics.DrawString(LightColorOnText, Me.Font, Brushes.Black, Textx, Texty)
        Else
            myTextSize = e.Graphics.MeasureString(LightColorOffText, Me.Font)
            CenterText(myTextSize)

            e.Graphics.DrawString(LightColorOffText, Me.Font, Brushes.Wheat, Textx, Texty)
        End If
    End Sub

    Private m_LightColorOnText As String = "ON"
    Public Property LightColorOnText As String
        Get
            Return m_LightColorOnText
        End Get
        Set(value As String)
            m_LightColorOnText = value
        End Set
    End Property

    Private m_LightColorOffText As String = "Off"
    Public Property LightColorOffText As String
        Get
            Return m_LightColorOffText
        End Get
        Set(value As String)
            m_LightColorOffText = value
        End Set
    End Property

    Private Sub CenterText(txtSize As SizeF)
        If Me.LegendPlate = LegendPlates.Large Then
            Texty = ((Me.Height * 0.68)) - (txtSize.Height / 2)
        Else
            Texty = (Me.Height * 0.59) - (txtSize.Height / 2)
        End If

        Textx = (Me.Width / 2) - (txtSize.Width / 2)
    End Sub

End Class


The height Centering code is a little hokey but effective.
I tried messing with the colors, but ran into a roadblock making the properties.
I haven't played with it yet, but would like a way for the font to be different.

I'm sure you know better ways to do this, but it does work.


rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Extending ADHMI Controls
« Reply #5 on: October 20, 2015, 12:00:48 PM »
ok, I have the different font working with this code.

First the Property
Code: [Select]
Private m_LightColorTextFont As System.Drawing.Font = Me.Font
    Public Property LightColorTextFont As System.Drawing.Font
        Get
            Return m_LightColorTextFont
        End Get
        Set(value As System.Drawing.Font)
            m_LightColorTextFont = value
        End Set
    End Property

and changes to the onPaint routine.
Code: [Select]

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim myTextSize As New SizeF
        If Me.Value = True Then
            myTextSize = e.Graphics.MeasureString(LightColorOnText, LightColorTextFont)
            CenterText(myTextSize)

            e.Graphics.DrawString(LightColorOnText, LightColorTextFont, Brushes.Black, Textx, Texty)
        Else
            myTextSize = e.Graphics.MeasureString(LightColorOffText, LightColorTextFont)
            CenterText(myTextSize)

            e.Graphics.DrawString(LightColorOffText, LightColorTextFont, Brushes.Wheat, Textx, Texty)
        End If
    End Sub

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Extending ADHMI Controls
« Reply #6 on: October 20, 2015, 04:55:33 PM »
If your objective is to place text on the Legend plate, you can leverage Mybase.Text and let the base class do all of the positioning.

For instance:
Code: [Select]
Protected Overrides Sub OnValueChanged(e As EventArgs)
    MyBase.OnValueChanged(e)

     If MyBase.Value = "True" Then
            MyBase.Text = LightColorOnText
            MyBase.Font = LightColorOnTextFont
        Else
            MyBase.Text = LightColorOffText
            MyBase.Font = LightColorOffTextFont
        End If
    End Sub

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Extending ADHMI Controls
« Reply #7 on: October 20, 2015, 05:47:28 PM »
I was hoping to center the text on the lamp itself.

When I'm back inthe office tomorrow I will post a pic of what I have so far.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Extending ADHMI Controls
« Reply #8 on: October 20, 2015, 06:04:39 PM »
This is the trick I use for centering text:
Code: [Select]
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
Dim TextRectangle As New Rectangle(0.Me.Height*0.2.Me.Width,Me.Height*0.8)
e.graphics.DrawString(MyBase.Text, MyBase.Font, TextBrush, TextRectangle, sf)


rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Extending ADHMI Controls
« Reply #9 on: October 21, 2015, 07:41:51 AM »
See the attached for what I have so far.

I like your solution better.
I'll integrate it and go from there.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Extending ADHMI Controls
« Reply #10 on: October 21, 2015, 08:44:51 AM »
Nice! Looks like you pretty much got it.

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Extending ADHMI Controls
« Reply #11 on: October 21, 2015, 08:47:33 AM »
Your centering trick worked great.
The code is much cleaner now.

The only thing left on my wish list is to be able to change the color of the text with properties.

I would be ok with either a short list to choose from or the full color pallet.
Hopefully you have a trick for this too.

Thanks again for all of your help.
I know this thread will come in handy for any future mods I come up with.

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Extending ADHMI Controls
« Reply #12 on: October 21, 2015, 10:07:33 AM »
Ok, I figured out the Text color properties.

It brings up the entire color pallet for choice.

You're doing some great work here Archie. 


joaco1993

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Extending ADHMI Controls
« Reply #13 on: April 10, 2016, 07:21:10 PM »
Hi ! I have downloaded the light.vb and opened. I can see the code but how to I do to see the graphics ??

thanks!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Extending ADHMI Controls
« Reply #14 on: April 10, 2016, 07:54:09 PM »
Hi ! I have downloaded the light.vb and opened. I can see the code but how to I do to see the graphics ??

thanks!
Did you right click the PurchasedControls folder in Solution Explorer and add the file to that?