Results 1 to 5 of 5
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: frmHeader - initializing the form

    Nearly done. Here's the code sequence to initialize the entire form. We're making use of strGP again to set default values or to obtain existing values from the INI file.

    When this post is completed you should be able to activate your form (the old F5 function key trick) and see it blossom in a beautiful blue-on-green colour scheme. You'll still have four ungainly buttons (Colours, Fonts, Captions, icons) on the screen, and we'll deal with them in the next post. I have attached a (locked) sample.dot for your inspection. You should see a toolbar with a single command cmd_Gui. That will throw up the form and do little else.

    Two functions appear here. Immediately below an update to the "UserForm_Initialize" function, and then a new generalized function "cmd_UserFormInitialize"

    <pre>Private Sub UserForm_Initialize()
    ' Procedure : UserForm_Initialize
    ' Description: Initialize the user form.
    ' By: Chris Greaves Inc.
    ' Inputs: None
    ' Returns: None
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By a call from the form.

    ' Looks like a simple cover function, right?
    ' In production forms I include calls to the Fun4X utilitiy buttons
    Call cmd_UserFormInitialize(Me) ' colour the GUI
    Call cmdRefresh_Click

    End Sub



    Public Sub cmd_UserFormInitialize(frmMe As UserForm)
    ' Procedure : cmd_UserFormInitialize
    ' Description: Generic GUI form initialization routine.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: The name of a form.
    ' Returns: None.
    ' Assumes: None.
    ' Side Effects: None.
    ' Tested: By a call from the user.

    ' Obtain the current or default colours from the INI file.
    Dim strColours As String
    strColours = Trim(strGp(strcApplication, strcColours,_
    strcDefaultColours)) & strcINIFileDelimiter
    Dim strR As String ' Red component
    Dim strB As String ' Blue component
    Dim strG As String ' Green component
    ' We now make repeated calls to strSplitStringAt to obtain
    ' successive sub-strings of the colour set.
    ' strip the Origin and base figures.
    strR = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    strR = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)

    ' Controls background

    ' obtain the controls background colour combinations.
    strR = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    strB = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    strG = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    Dim myControl As Control
    ' set the controls background colour.
    For Each myControl In frmMe.Controls
    myControl.BackColor = RGB(Val(strR), Val(strB), Val(strG))
    Next myControl

    ' Controls Foreground

    ' obtain the controls foreground colour combinations.
    strR = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    strB = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    strG = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    ' set the controls foreground colour.
    For Each myControl In frmMe.Controls
    On Error Resume Next
    myControl.ForeColor = RGB(Val(strR), Val(strB), Val(strG))
    Next myControl

    ' Forms Background

    ' obtain the form background colour combinations.
    strR = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    strB = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    strG = strSplitStringAt(strColours, strcINIFileDelimiter, True)
    strColours = strSplitStringAt(strColours, strcINIFileDelimiter, False)
    ' set the form background colour.
    frmMe.BackColor = RGB(Val(strR), Val(strB), Val(strG))

    ' Control Fonts

    Dim strFonts As String
    strFonts = Trim(strGp(strcApplication, strcFonts, "")) & strcINIFileDelimiter
    If Len(strFonts) > 1 Then ' we assume the fonts string is a valid format
    Dim strTypeFace As String
    Dim strPointSize As String
    strTypeFace = strSplitStringAt(strFonts, strcINIFileDelimiter, True)
    strFonts = strSplitStringAt(strFonts, strcINIFileDelimiter, False)
    strPointSize = strSplitStringAt(strFonts, strcINIFileDelimiter, True)
    For Each myControl In frmMe.Controls
    On Error GoTo FontsFailed
    myControl.Object.FontName = strTypeFace
    myControl.Object.FontSize = strPointSize
    Next myControl
    FontsFailed:
    On Error GoTo 0
    Else
    End If
    End Sub
    </pre>


    The function above includes the code for setting customized fonts.
    Attached Files Attached Files

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: frmHeader - initializing the form

    Chris,

    for the control loop, could you use something like:

    <pre> For Each myControl In frmAddContact.Controls
    If (TypeOf myControl Is TextBox Or _
    TypeOf myControl Is Label) Then
    myControl.Object.FontName = "Arial"
    myControl.Object.FontSize = 11
    End If
    Next myControl

    </pre>


    - obviously expanding the controls to be checked

    This avoids the use of "On error" coding which I abhor
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: frmHeader - initializing the form

    >This avoids the use of "On error" coding which I abhor

    I agree. I hate the darn thing.

    Initially (when I was REALLY naive) I coded a simple loop for each control.

    Then I ran into errors, which I tracked down to non-textbox, non-lable controls. I added a conditional, somewhat like yours.

    Then, I suspect, I tripped over another type I'd not foreseen.

    At this point (especially when in a hurry to get something out of the door) I usually throw up my hands and realize that I could trip over discoveries and/or new MSoft features down the road, so i fall back on the dreaded ON ERROR.


    As you can see, I'm still in the "what's the use" state with this code. Once I am assured of a definitive set of controls (such as your example shows), I'm happy to remove the ON ERROR.


    IOW my code does fluctuate. Today's sample has a "repaint", but I've no idea why I put it there. I probably thought it would fix a problem; I fixed the problem somewhere else, but forgot about a stopgap measure.

  4. #4
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: frmHeader - initializing the form

    Chris,

    OK, fair point. And I guess, that even if you did have an authoritative list, someone could add a reference which included another control, and then you're up sheep creek again.

    But then, perhaps you need to isolate your testing of controls to a separate routine- which returns a "hasText" property. At least, that way, "On Error" can be kept out of the main code.

    It's probably a routine which has a use from many other routines.

    Just a thought
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: frmHeader - initializing the form

    > authoritative list, someone could add a reference which included
    My primary fear. And this is utility code. I build an appliaction by drag-and-dropping entire modules. I don't ever want to edit the code again.


    > separate routine- which returns a "hasText" property. At least, that

    This I like. And you know I'm into tiny building blocks. I might choose to call it something more specific, such as "Can be Coloured", and have THAT function call one-liners, such as "is text" and "is label" and "is button".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •