Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Frame caption alignment

Posted on 2003-10-22
6
Medium Priority
?
471 Views
Last Modified: 2013-12-26
Is there anyway to have the caption of a Frame control centered automatically?
The align property is not available for this control

For my application the frame.caption changes constantly and is always aligned to the left.
Using spaces()  is not acurate because of the font.

0
Comment
Question by:kcmagicdata
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9604675
Hi!

The only way I could think of is to set the frame caption to "" (blank) and use a label to act as Frame caption. Note that Label has Allign and Autosize property which
makes it easy to 'manipulate'.

Here is the sample code.
On a empty Form put following objects:      three option buttons, one frame, one label (inside a frame), one textbox and one command button and then paste this code

--------------------------------------------------------------

Private Sub Command1_Click()
    Label1.Caption = Text1.Text                   'Update the caption
    If Option1.Value = True Then                  'Align left
        Label1.Left = 80
        Label1.Alignment = 0
    End If
    If Option2.Value = True Then                                        'Align right
        Label1.Left = Frame1.Width - Label1.Width - 80
        Label1.Alignment = 1
    End If
    If Option3.Value = True Then                                         'Center align
        Label1.Left = Frame1.Width / 2 - (Label1.Width / 2)
        Label1.Alignment = 2
    End If
End Sub

Private Sub Form_Load()
    Frame1.Top = 240                                        'Some positioning
    Frame1.Left = 240
    Frame1.Width = 2600
    Frame1.Height = 2600
    Frame1.Caption = ""

    Label1.Top = 0

    Option1.value = True
    Option1.Caption = "Left"
    Option2.Caption = "Right"
    Option3.Caption = "Center"

    If Option1.Value = True Then                                         'Align left
        Label1.Left = 80
        Label1.Alignment = 0
    End If
    If Option2.Value = True Then                                            'Align right
        Label1.Left = Frame1.Width - Label1.Width - 80
        Label1.Alignment = 1
    End If
    If Option3.Value = True Then                                                'Center align
        Label1.Left = Frame1.Width / 2 - (Label1.Width / 2)
        Label1.Alignment = 2
End If


End Sub

Private Sub Option1_Click()                                 'Align left
    Label1.Left = 80
    Label1.Alignment = 0
End Sub

Private Sub Option2_Click()                                              'Align right
    Label1.Left = Frame1.Width - Label1.Width - 80
    Label1.Alignment = 1
End Sub

Private Sub Option3_Click()                                                   'Center align
    Label1.Left = Frame1.Width / 2 - (Label1.Width / 2)
    Label1.Alignment = 2
End Sub
-------------------------------------------------------
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9604682
Don't forget to put Label1.Autosize property to True.   I forgot to include it to the code.
0
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 2000 total points
ID: 9604738
Try this example:

Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const BS_LEFT As Long = &H100
Private Const BS_RIGHT As Long = &H200
Private Const BS_CENTER As Long = &H300
Private Const BS_TOP As Long = &H400
Private Const BS_BOTTOM As Long = &H800
Private Const BS_VCENTER As Long = &HC00
Private Const BS_ALLSTYLES = BS_LEFT Or BS_RIGHT Or BS_CENTER Or BS_TOP Or BS_BOTTOM Or BS_VCENTER
Private Const GWL_STYLE& = (-16)
Public Enum bsHorizontalAlignments
    bsLeft = BS_LEFT
    bsRight = BS_RIGHT
    bsCenter = BS_CENTER
End Enum
Public Enum bsVerticalAlignments
    bsTop = BS_TOP
    bsBottom = BS_BOTTOM
    bsVCenter = BS_VCENTER
End Enum

Public Sub AlignTextCenter(fr As Frame, Optional ByVal HStyle As bsHorizontalAlignments = bsCenter, Optional ByVal VStyle As bsVerticalAlignments = bsVCenter)
    Dim oldStyle As Long
    ' get current style
    oldStyle = GetWindowLong(fr.hWnd, GWL_STYLE)
    ' clear existing alignment setting(s)
    oldStyle = oldStyle And (Not BS_ALLSTYLES)
    ' set new style and refresh
    Call SetWindowLong(fr.hWnd, GWL_STYLE, _
    oldStyle Or HStyle Or VStyle)
    fr.Refresh
End Sub

Private Sub Form_Load()
    AlignTextCenter Frame1, bsCenter, bsTop
End Sub

'Customized from the code from Richie_Simonetti at
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20177568.html

Hope this helps
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kcmagicdata
ID: 9604746
Sorry, im trying to avoid using another control to display the caption.
I need the frame controls caption to display the text
thanks for the suggestion
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 9604748
Even make it more fun by change this:

Public Sub AlignTextCenter(fr As Frame, Optional ByVal HStyle As bsHorizontalAlignments = bsCenter, Optional ByVal VStyle As bsVerticalAlignments = bsVCenter)

to:
   
Public Sub AlignTextCenter(fr As Object, Optional ByVal HStyle As bsHorizontalAlignments = bsCenter, Optional ByVal VStyle As bsVerticalAlignments = bsVCenter)

as you can try like:

AlignTextCenter Frame1, bsCenter, bsTop
    AlignTextCenter Command1, bsRight, bsBottom
    AlignTextCenter Check1, bsCenter, bsTop

cheers
0
 

Author Comment

by:kcmagicdata
ID: 9604763
thanks
you guys are a big help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question