CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2000
    Posts
    5

    CommandButton Style

    How can I change the style of a commandbutton dynamically created?

    My code:

    frm.Controls.Add "VB.CommandButton", "cmdTest"
    '
    '
    '
    set cmdAdicionar = mdgrDataGrid.Parent.Controls("cmdTest")
    cmdAdicionar.Top = lngTop + 50
    cmdAdicionar.Left = lngLeft + 50
    cmdAdicionar.Width = 200
    cmdAdicionar.Height = 100
    cmdAdicionar.Style = 1 '----- error -----
    cmdAdicionar.Picture = LoadResPicture(101, vbResBitmap)
    cmdAdicionar.ZOrder 0
    cmdAdicionar.TabStop = true
    cmdAdicionar.Visible = true




    The style property is read only at run time. :-(


  2. #2
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: CommandButton Style

    Alternative way is to load an array of control:

    1. In design view - add a command button (change the properties as needed) and change the Index property to 0 (it is better to have the Visible property set to FALSE)
    2. During the run time do the following:

    private Sub Form_Load()
    for i = 1 to 2
    Call LoadDynButton(i)
    Command1(i).Visible = true
    next i
    End Sub

    Sub LoadDynButton(byval nInd as Integer)
    on error GoTo trap_err

    Load Command1(nInd)
    Command1(nInd).Left = Command1(nInd - 1).Left + 20 + Command1(nInd).Width
    Exit Sub

    trap_err:
    MsgBox "error creating button #" & nInd & vbCrLf & Err.Description
    End Sub




    Play with the code so that it can cater for your needs. Good luck.

    -Cool Bizs

    Good Luck,
    -Cool Bizs

  3. #3
    Join Date
    Apr 2000
    Posts
    5

    Re: CommandButton Style

    Thanks, but I use the code posted before in a class that can be used in any form of my application. Because of that I can't use an array of controls. ¿Any suggestion?


  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: CommandButton Style

    You can change a button between Graphical and ordinary styles thus:

    '\\ API Declarations
    option Explicit
    '\\ Window specific information
    private Declare Function GetWindowLongApi Lib "user32" Alias "GetWindowLongA" (byval hwnd as Long, byval nIndex as Long) as Long
    private Declare Function SetWindowLongApi Lib "user32" Alias "SetWindowLongA" (byval hwnd as Long, byval nIndex as Long, byval dwNewLong as Long) as Long

    '\\ get Window Long Indexes...
    public Enum enGetWindowLong
    GWL_EXSTYLE = (-20)
    GWL_HINSTANCE = (-6)
    GWL_HWNDPARENT = (-8)
    GWL_ID = (-12)
    GWL_STYLE = (-16)
    GWL_USERDATA = (-21)
    GWL_WNDPROC = (-4)
    End Enum

    private Const BS_BITMAP = 80

    '\\ --[SetButtonStyle]----------------------------------------------------------------------
    '\\ Sets the style bit specified to the window specified. Note that many window style
    '\\ bits cannot be used at run time :. use this with caution
    '\\ ----------------------------------------------------------------------------------------
    '\\ You have a royalty free right to use, reproduce, modify, publish and mess with this code
    '\\ I'd like you to visit http://www.merrioncomputing.com for updates, but won't force you
    '\\ ----------------------------------------------------------------------------------------
    private Sub SetButtonStyle(BtnIn as CommandButton, Graphical as Boolean)

    Dim lStyle as Long
    Dim lRet as Long

    '\\ get the current setting of that style bit
    lStyle = GetWindowLongApi(BtnIn.hwnd, GWL_STYLE)

    If Graphical then
    '\\ Add the new style bit to it
    lStyle = lStyle Or BS_BITMAP
    else
    If (lStyle Or BS_BITMAP) = lStyle then
    lStyle = lStyle - BS_BITMAP
    End If
    End If

    '\\ set it to the window
    lRet = SetWindowLongApi(BtnIn.hwnd, GWL_STYLE, lStyle)

    BtnIn.Refresh

    End Sub




    Note that the VB runtime will not know you have done this, so the Command.Style member will not change - but you will be able to do graphical things with the button.

    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured