C++ Builder

La Isla Bonita



Send Message (Win32 API): Send Message To a ComboBox Control
Send Message (Win32 API): Send Message To a ComboBox Control


Description

This example shows how to insert, add and select items in a ComboBox component (TComboBox) at runtaime through the Win32 API function SendMessage()

The Form contains a ComboBox (TComboBox), and 3 Buttons (TButton).

SendMessageToComboBox.cpp

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   // Use the CB INSERT STRING (Add to the beginning of the drop-down list)
   //
   char s[100];
   char sIndex[25];

   for (int i = 1; i <= 10; ++i)
   {
       strcpy (s, "Item ");
       itoa(i, sIndex, 10);
       strcat(s, sIndex);

       SendMessage(ComboBox1->Handle, CB_INSERTSTRING, 0,
                   (LPARAM)(LPCTSTR)s);
   }

   SendMessage(ComboBox1->Handle, CB_SETCURSEL, 0, 0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   // Use the CB ADD STRING (Add to the end of the drop-down list)
   //

   char s[100];
   char sIndex[25];

   for (int i = 1; i <=10; ++i)
   {
       strcpy (s, "Item ");
       itoa(i, sIndex, 10);
       strcat(s, sIndex);
       SendMessage(ComboBox1->Handle, CB_ADDSTRING, 0,
                   (LPARAM)(LPCTSTR)s);
   }

   SendMessage(ComboBox1->Handle, CB_SETCURSEL, 0, 0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
   // Clear the contents of the combo box
   //
   SendMessage(ComboBox1->Handle, CB_RESETCONTENT, 0, 0);
}

SendMessageToComboBox.h

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <sForms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TComboBox *ComboBox1;
        TButton *Button1;
        TButton *Button2;
        TButton *Button3;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall Button3Click(TObject *Sender);    
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.