Solved

Frame caption alignment

Posted on 2003-10-22
6
402 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
  • 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 50

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 50

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

776 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