Ver Mensaje Individual
  #13  
Antiguo 18-12-2017
Avatar de aguml
aguml aguml is offline
Miembro
 
Registrado: may 2013
Posts: 885
Reputación: 14
aguml Va por buen camino
Bueno, esa parte ya la tenia solucionada viendo como se hacia en el codigo anterior que pusiste pero ahora estaba con el tema del evento OnChange el cual he solucionado asi:
ListBoxAutoSelect.h:
Código PHP:
//---------------------------------------------------------------------------

#ifndef ListBoxAutoSelectH
#define ListBoxAutoSelectH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
//---------------------------------------------------------------------------
class PACKAGE TListBoxAutoSelect : public TListBox
{
private:
    
int _index,_LastItemIndex;
    
TWndMethod _oldWndProc;
    
TNotifyEvent _vScroll;
    
TNotifyEvent _OnChange;
protected:
    
virtual void __fastcall WndProc(TMessage &msg) {
        if (
msg.Msg == WM_VSCROLL) {
            
_index = GetScrollPos(this->Handle, SB_VERT);
            
Selected[_index] = true;
            
            if(
_vScroll){
                
_vScroll(this);
            }
            if(
_LastItemIndex != ItemIndex){
                
_LastItemIndex = ItemIndex;
                if(
_OnChange){
                    
_OnChange(this);
                }
            }
        }else if (
msg.Msg == CN_COMMAND){
            if(
reinterpret_cast<TWMCommand&>(msg).NotifyCode == LBN_SELCHANGE){
                if(
_LastItemIndex != ItemIndex){
                    
_LastItemIndex = ItemIndex;
                    if(
_OnChange){
                        
_OnChange(this);
                    }
                }
            }
        }
        
TListBox::WndProc(msg);
    }
public:
    
__property TNotifyEvent OnVerticalScroll = {read = _vScroll, write = _vScroll};
    
__property TNotifyEvent OnChange = {read = _OnChange, write = _OnChange};
    
__property int CurrentIndex = {read = _index};
    
__fastcall TListBoxAutoSelect(TComponent* Owner);
    
__fastcall ~TListBoxAutoSelect();

__published:
};
//---------------------------------------------------------------------------
#endif 
ListBoxAutoSelect.cpp:
Código PHP:
//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "ListBoxAutoSelect.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TListBoxAutoSelect *)
{
    new 
TListBoxAutoSelect(NULL);
}
//---------------------------------------------------------------------------

__fastcall TListBoxAutoSelect::TListBoxAutoSelect(TComponent* Owner)
    : 
TListBox(Owner)
{
    
_oldWndProc = this->WindowProc;
    
_LastItemIndex=-1;
}
//---------------------------------------------------------------------------

__fastcall TListBoxAutoSelect::~TListBoxAutoSelect() {
    
this->WindowProc = _oldWndProc;
}
//---------------------------------------------------------------------------

namespace Listboxautoselect
{
    
void __fastcall PACKAGE Register()
    {
         
TComponentClass classes[1] = {__classid(TListBoxAutoSelect)};
         
RegisterComponents("Mis Componentes", classes, 0);
    }
}
//--------------------------------------------------------------------------- 
Tengo 18 ListBoxAutoSelect con lo que hago esto:
Código PHP:
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    ...
    
//Asigno mi funcion al evento OnVerticalScroll de los TListBoxAutoSelect
    
TListBoxAutoSelect *LB;
    for(
int i=1;i<10;i++){
        
LB=(TListBoxAutoSelect*)GroupBoxInventarioChris->FindChildControl("ListBoxCatArmaChris" + IntToStr(i));
        
LB->OnChange=OnChange;
    }
    for(
int i=1;i<10;i++){
        
LB=(TListBoxAutoSelect*)GroupBoxInventarioSheva->FindChildControl("ListBoxCatArmaSheva" + IntToStr(i));
        
LB->OnChange=OnChange;
    }
    ...
}

//---------------------------------------------------------------------------

void __fastcall TForm1::OnChange(TObject *Sender)
{
    
AnsiString nombre;
    
TComboBox *CB;
    
TListBoxAutoSelect *LB;
    
TGroupBox *GB;
    
LB=static_cast<TListBoxAutoSelect*>(Sender);
    
//Como tengo 2 GroupBox (uno para cada personaje) y el contenido es igual para ambos excepto en los nombres...
    //Dependiendo del nombre del personaje uso un GroupBox o el otro
    
if(LB->Name.SubString(15,5)=="Chris")
        
GB=GroupBoxInventarioChris;
    else  if(
LB->Name.SubString(15,5)=="Sheva")
        
GB=GroupBoxInventarioSheva;

    
//Obtengo el nombre del ComboBox y obtengo un puntero a el para poder usarlo
    
nombre = "ComboBox" + LB->Name.SubString(11,LB->Name.Length());
    
CB=(TComboBox*)GB->FindChildControl(nombre);

    
//Relleno el ComboBox que corresponde al TListBox que cambia y asigno su Hint
    
RellenarCombo(CB,LB->ItemIndex);
    
CB->Hint = CB->Items->Strings[CB->ItemIndex];
}
//--------------------------------------------------------------------------- 
Funciona perfecto pero me gustaria que me ayudaran a corregir posibles fallos que pueda tener y yo no veo debido a mi poca experiencia.
Mil gracias amigos.
Responder Con Cita