Solved

Frame caption alignment

Posted on 2003-10-22
6
443 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 52

Accepted Solution

by:
Ryan Chong earned 500 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 52

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month5 days, 23 hours left to enroll

626 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