Thread: Create event for run-time button?

  1. #1
    Robert
    Guest

    Question Create event for run-time button?

    Hello people!

    Just learned how to make controls at runtime (my program contains about 12 or more, I am using 2 string array to make pairs of TLabel and TControl - usually a TButton, or TSpeedButton) and now I'm stuck: how do I make all those buttons do somenthing?

    I need to create the event that makes the buttons useful.
    Could event be created at run-time, same like buttons?

    I create the buttons in a for(; loop (this are the arrays for), because the in the final project I might have different buttons that I'm using now.
    I am using BCBuilder 5.3 (on Windows 98).

    Thanks in advance
    [email protected]

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    I don't know what BCBuilder 5.3 is, and I don't really know what you're asking.
    Could event be created at run-time, same like buttons?
    Do you mean that you want something to happen when you use one of these controls? If you do, I can tell you how to.

  3. #3
    Robert
    Guest

    Event for an run time button

    Thanks for using your time with this!

    So: I have created a button at run time, looks nice, shows the caption I need, colors, position, and all others, but when I click it nothing happens! I don't know how to create an event (at run time or not) like OnClick, which would make the button do something when I click on it.

    BCB 5 or BCBuilder is Borland C++ Builder 5.0 (actually, mine is 5.3), an visual C++ development interface.

    I am not an natural born english speaker, so you might have difficulties understanding me, please say so. I am doing my best.

    Thanks
    Robert

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Ok, I assume you are talking about Windows controls, so that is what I will show you.
    Whether you create your control by a resource file or directly in your code, it should have an ID number. It is usually defined in a header file that is #include'd in the resource file. This number is how you identify if the control has been triggered. Look in your Message loop function, the one with all the WM_ commands. Add a case for WM_COMMAND if it's not already there. Within that add a switch statement checking for the LOWORD(the first 16 bits) of the wParam. Then add a case statement for the ID of you button. Under this you can do all you want when you press the button.
    PHP Code:
    case WM_COMMAND:
         switch(
    LOWORD(wParam))
           {
               case 
    IDC_BUTTON//Replace this with your buttons ID
                      //Do whatever
                
    break;
            }     
    break; 
    I hope this helps you! Reply to this message if something if still stumping you.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    30
    Yes, I am talking about Windows controls (btw - is there any difference between controls and components? - this might be Borland again).

    But, there is a problem: I use Borland C++, so I do not see WM_COMMAND, for example. Look what makes my control appears:
    "
    TSpeedButton *AA= new TSpeedButton(Panel1);
    AA->Left = 5;
    AA->Top = 5;
    AA->Width = 100;
    AA->Caption = "ABC";
    AA->Flat = true;
    AA->Layout = blGlyphLeft;
    AA->Margin = 18;
    AA->Parent = Form1->Panel1;
    "
    As you can see, ID number, wParam, LOWORD, are not used. Gee, I'm lost, but I'm digging!

    But if we take it methodically:

    Let's say I have just created the control. In your way, not knowing the ID, what would you do to find it?

    Best regards
    ___________________________________________
    I supose Sworensen was right.
    Now I C.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    In "my" way you set the ID number when you make the control. I'm afraid I dont know anything about Borland C++, or the programming technique you are using. Very sorry :-(, but if you post a message asking for help from people that use BCB, I'm sure you will get some. I would advise though, to use the standard MFC coding, like in my reply. The wParam and LOWORD are things in normal VWindows programming. Sorry I couldn't help you.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    30

    Thanks

    I will do that. Thank you very much

    Robert.
    Now I C.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    30

    Show me


    I would advise though, to use the standard MFC coding, like in my reply. The wParam and LOWORD are things in normal VWindows programming.
    Can you show me a piece of code how you would do this? I am curious if I can compile it. Maybe I'll switch to MFC.

    Robert
    Now I C.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Ok, but VWindows was a typo, no V. It's not something special. Here is the Windows template from Dev-C++:
    PHP Code:
    #include <windows.h>

    /* Declare Windows procedure */
    LRESULT CALLBACK WindowProcedure(HWNDUINTWPARAMLPARAM);
    /* Make the class name into a global variable */
    char szClassName[ ] = "WindowsApp";
    int WINAPI WinMain(HINSTANCE hThisInstanceHINSTANCE hPrevInstanceLPSTR lpszArgumentint nFunsterStil)

    {
        
    HWND hwnd;               /* This is the handle for our window */
        
    MSG messages;            /* Here messages to the application are saved */
        
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

        /* The Window structure */
        
    wincl.hInstance hThisInstance;
        
    wincl.lpszClassName szClassName;
        
    wincl.lpfnWndProc WindowProcedure;      /* This function is called by windows */
        
    wincl.style CS_DBLCLKS;                 /* Catch double-clicks */
        
    wincl.cbSize sizeof(WNDCLASSEX);

        
    /* Use default icon and mouse-pointer */
        
    wincl.hIcon LoadIcon(NULLIDI_APPLICATION);
        
    wincl.hIconSm LoadIcon(NULLIDI_APPLICATION);
        
    wincl.hCursor LoadCursor(NULLIDC_ARROW);
        
    wincl.lpszMenuName NULL/* No menu */
        
    wincl.cbClsExtra 0;                      /* No extra bytes after the window class */
        
    wincl.cbWndExtra 0;                      /* structure or the window instance */
        /* Use light-gray as the background of the window */
        
    wincl.hbrBackground = (HBRUSHGetStockObject(LTGRAY_BRUSH);

        
    /* Register the window class, if fail quit the program */
        
    if(!RegisterClassEx(&wincl)) return 0;

        
    /* The class is registered, let's create the program*/
        
    hwnd CreateWindowEx(
               
    0,                   /* Extended possibilites for variation */
               
    szClassName,         /* Classname */
               
    "Windows App",         /* Title Text */
               
    WS_OVERLAPPEDWINDOW/* default window */
               
    CW_USEDEFAULT,       /* Windows decides the position */
               
    CW_USEDEFAULT,       /* where the window ends up on the screen */
               
    544,                 /* The programs width */
               
    375,                 /* and height in pixels */
               
    HWND_DESKTOP,        /* The window is a child-window to desktop */
               
    NULL,                /* No menu */
               
    hThisInstance,       /* Program Instance handler */
               
    NULL                 /* No Window Creation data */
               
    );

        
    /* Make the window visible on the screen */
        
    ShowWindow(hwndnFunsterStil);
        
    /* Run the message loop. It will run until GetMessage( ) returns 0 */
        
    while(GetMessage(&messagesNULL00))
        {
               
    /* Translate virtual-key messages into character messages */
               
    TranslateMessage(&messages);
               
    /* Send message to WindowProcedure */
               
    DispatchMessage(&messages);
        }

        
    /* The program return-value is 0 - The value that PostQuitMessage( ) gave */
        
    return messages.wParam;
    }

    /* This function is called by the Windows function DispatchMessage( ) */
    LRESULT CALLBACK WindowProcedure(HWND hwndUINT messageWPARAM wParamLPARAM lParam)
    {
        switch (
    message)                  /* handle the messages */
        
    {
               case 
    WM_DESTROY:
               
    PostQuitMessage(0);        /* send a WM_QUIT to the message queue */
               
    break;
               default:                   
    /* for messages that we don't deal with */
               
    return DefWindowProc(hwndmessagewParamlParam);
        }
        return 
    0;

    Hope it helps you.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    30

    I will try

    btw, do you know some good books I can learn MFC from? Did you use any?

    Robert
    Now I C.

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Robert, you cannot create the event runtime in BCB. You have to create the event in advance, or make temple functions. When you have created you control, assign the OnClick functionpointer to the function you have created. The problem with creating controls at runtime is that you must know before runtime what they should do. If you know that, you are lucky. Its the same with MFC. You must know in advance what pressing the button will do. Acctually, when running any w32 program, the controles are created runtime, even if you draw them on a form in BCB at designtime.

    So: To get reaction from a runtime created control, you must know what it should do in advance. Create the OnClick function and assign the OnClick-event to your function.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    30
    Originally posted by ninebit
    Robert, you cannot create the event runtime in BCB
    That's harsh! Do I have to quit? :-((
    You have to create the event in advance, or make temple functions.
    And this would be...?
    When you have created you control, assign the OnClick functionpointer to the function you have created. The problem with creating controls at runtime is that you must know before runtime what they should do.
    Let's say that no matter the control, they do pretty much the same thing, only parameters are different - as number, type, or values, ie they would call the same function.
    If you know that, you are lucky. Its the same with MFC.
    You mean that MFC is not any better than BCB at this point? Rectification: BCB implements all that MFC does?
    So: To get reaction from a runtime created control, you must know what it should do in advance.
    More or less...
    Create the OnClick function and assign the OnClick-event to your function.
    This changes the problem. I must think of an array of controls and events or something, I don't know in advance how many controls I will need. I've seen a tut on bcbdev.com saying pretty much the same, but I was still hoping. Well, no more.
    Anyone: does C# something like this? :-) Anybody else?

    Robert
    Now I C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a function pointer from a button click event?
    By dxfoo in forum Windows Programming
    Replies: 20
    Last Post: 01-29-2008, 03:35 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. How to do a mouse over event on a button?
    By GUIPenguin in forum C# Programming
    Replies: 1
    Last Post: 07-06-2006, 10:34 AM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  5. How can I create a time delay?
    By Guideon72 in forum C Programming
    Replies: 14
    Last Post: 10-25-2001, 02:28 PM