BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Modal TEdit?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development)
View previous topic :: View next topic  
Author Message
Mike King
Guest





PostPosted: Thu Jun 09, 2005 3:54 pm    Post subject: Modal TEdit? Reply with quote



I'm creating a control that derives from TSpeedButton. When the button is
down and when the user double-clicks the button, I want a TEdit control to
appear in the center of the TSpeedButton. This TEdit control with allow the
user to edit the caption of the TSpeedButton. When the TEdit control is on
the screen, I don't want the user to be able to click on any other
control/form in the application. I have been able to lock the keyboard
input to the TEdit but not Mouse input. Below is a complete working example
of the problem. Double-click a TSpeedButton then try double-clicking
another TSpeedButton. I don't want to be able to click the other
TSpeedButton.

P.S. I know the code is ugly... it's prototype code.


########## BEGIN FORM'S DFM ###########
object Form1: TForm1
Left = 209
Top = 171
Width = 366
Height = 222
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object SpeedButton1: TSpeedButton
Left = 44
Top = 28
Width = 81
Height = 37
GroupIndex = 1
Down = True
OnDblClick = SpeedButtonDblClick
end
object SpeedButton2: TSpeedButton
Left = 44
Top = 76
Width = 81
Height = 37
GroupIndex = 1
OnDblClick = SpeedButtonDblClick
end
end
########## END FORM'S DFM #############

########## BEGIN FORM'S 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::SpeedButtonDblClick(TObject *Sender)
{
TSpeedButton* pSB = (TSpeedButton*) Sender;
TCEdit* pEdit = new TCEdit(pSB);

pEdit->Parent = pSB->Parent;
pEdit->SpeedButton = pSB;

pEdit->BevelKind = bkFlat;
pEdit->Width = pSB->Width * 0.8;
pEdit->Top = pSB->Top + pSB->Height / 2 - pEdit->Height / 2;
pEdit->Left = pSB->Left + pSB->Width / 2 - pEdit->Width / 2;

pEdit->OnKeyPress = EditKeyPress;
pEdit->OnExit = EditExit;
pEdit->Text = pSB->Caption;

pEdit->SetFocus();
SetCapture(pEdit->Handle);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::EditKeyPress(TObject *Sender, Char &Key)
{
if (Key == 13 /*Enter*/)
{
TCEdit* pEdit = (TCEdit*) Sender;
TSpeedButton* pSB = (TSpeedButton*) pEdit->SpeedButton;

ReleaseCapture();
pSB->Caption = pEdit->Text;
pEdit->Parent = NULL;
}
else if (Key == 27 /*Esc*/)
{
TCEdit* pEdit = (TCEdit*) Sender;

ReleaseCapture();
pEdit->Parent = NULL;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::EditExit(TObject *Sender)
{
TCEdit* pEdit = (TCEdit*) Sender;
pEdit->SetFocus();
}
//---------------------------------------------------------------------------
########## END FORM'S CPP #############

########## BEGIN FORM'S H #############
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TSpeedButton *SpeedButton1;
TSpeedButton *SpeedButton2;
void __fastcall SpeedButtonDblClick(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);

void __fastcall EditKeyPress(TObject *Sender, Char &Key);
void __fastcall EditExit(TObject *Sender);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

class TCEdit : public TEdit
{
private:
TSpeedButton* m_SB;
public:
__fastcall TCEdit(TComponent* Owner);
void __fastcall CreateParams(TCreateParams &Params);

__property TSpeedButton* SpeedButton = { read=m_SB, write=m_SB };
};

//---------------------------------------------------------------------------
__fastcall TCEdit::TCEdit(TComponent* Owner) : TEdit(Owner)
{
m_SB = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TCEdit::CreateParams(TCreateParams &Params)
{
TEdit::CreateParams(Params);
Params.Style = Params.Style | ES_CENTER;// ES_MULTILINE;
}
//---------------------------------------------------------------------------
########## END FORM'S H ###############


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Jun 09, 2005 5:44 pm    Post subject: Re: Modal TEdit? Reply with quote




"Mike King" <emailMK (AT) excite (DOT) com> wrote


Quote:
When the TEdit control is on the screen, I don't want the user to
be able to click on any other control/form in the application.

That is not a good design choice, and is non-standard behavior for embedded
editors. Users expect to be able to click anywhere, and have those kind of
editors simply disappear when they lose focus. That is what you should be
doing in your code.


Gambit



Back to top
Mike King
Guest





PostPosted: Thu Jun 09, 2005 6:36 pm    Post subject: Re: Modal TEdit? Reply with quote



"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote

Quote:

"Mike King" <emailMK (AT) excite (DOT) com> wrote in message
news:42a8667a$1 (AT) newsgroups (DOT) borland.com...

When the TEdit control is on the screen, I don't want the user to
be able to click on any other control/form in the application.

That is not a good design choice, and is non-standard behavior for
embedded
editors. Users expect to be able to click anywhere, and have those kind
of
editors simply disappear when they lose focus. That is what you should be
doing in your code.


Gambit

You're right. I was going down the wrong path. I developed an elegant
solution. See following code.


########## BEGIN FORM'S 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)
{
TSpeedButtonEdit* pEdit1 = new TSpeedButtonEdit(this);
TSpeedButtonEdit* pEdit2 = new TSpeedButtonEdit(this);

pEdit1->Parent = this;
pEdit2->Parent = this;

pEdit1->Top = 20;
pEdit2->Top = 100;
pEdit1->Left = 10;
pEdit2->Left = 10;
pEdit1->Height = 50;
pEdit2->Height = 50;
pEdit1->Width = 150;
pEdit2->Width = 150;

pEdit1->GroupIndex = 1;
pEdit2->GroupIndex = 1;
}
//---------------------------------------------------------------------------

void __fastcall TSpeedButtonEdit::SpeedButtonDblClick(TObject *Sender)
{
ChangeEditVisibility(true);
}
//---------------------------------------------------------------------------

__fastcall TSpeedButtonEdit::TSpeedButtonEdit (Classes::TComponent* AOwner)
: TSpeedButton(AOwner)
{
m_Edit = new TCEdit(this);
OnDblClick = SpeedButtonDblClick;
}
//---------------------------------------------------------------------------

void __fastcall TSpeedButtonEdit::ChangeEditVisibility ( bool visible )
{
if (visible)
{
m_Edit->Parent = Parent;
m_Edit->SpeedButton = this;

m_Edit->BevelKind = bkFlat;
m_Edit->Width = Width * 0.8;
m_Edit->Top = Top + Height / 2 - m_Edit->Height / 2;
m_Edit->Left = Left + Width / 2 - m_Edit->Width / 2;

m_Edit->Text = Caption;

m_Edit->Visible = true;
m_Edit->SetFocus();
}
else
m_Edit->Visible = false;

}
//---------------------------------------------------------------------------

void __fastcall TSpeedButtonEdit::AcceptText( )
{
Caption = m_Edit->Text;
m_Edit->Visible = false;
}
//---------------------------------------------------------------------------

void __fastcall TSpeedButtonEdit::WndProc(Messages::TMessage &Message)
{
if (Message.Msg == CM_BUTTONPRESSED)
{
TSpeedButtonEdit* pSB = (TSpeedButtonEdit*)Message.WParam;

if (pSB != this)
AcceptText();
}

TSpeedButton::WndProc(Message);
}
//---------------------------------------------------------------------------



//---------------------------------------------------------------------------
__fastcall TCEdit::TCEdit(TComponent* Owner) : TEdit(Owner)
{
m_SB = NULL;
OnKeyPress = EditKeyPress;
OnExit = EditExit;
}
//---------------------------------------------------------------------------

void __fastcall TCEdit::CreateParams(TCreateParams &Params)
{
TEdit::CreateParams(Params);
Params.Style = Params.Style | ES_CENTER;
}
//---------------------------------------------------------------------------

void __fastcall TCEdit::EditKeyPress(TObject *Sender, Char &Key)
{
if (Key == 13 /*Enter*/)
{
m_SB->Caption = Text;
m_SB->ChangeEditVisibility(false);
}
else if (Key == 27 /*Esc*/)
{
m_SB->ChangeEditVisibility(false);
}
}
//---------------------------------------------------------------------------

void __fastcall TCEdit::EditExit(TObject *Sender)
{
m_SB->ChangeEditVisibility(false);
}
//---------------------------------------------------------------------------
########## END FORM'S CPP #############

########## BEGIN FORM'S H #############
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <ActnList.hpp>
#include <ActnMan.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

class TCEdit;

class TSpeedButtonEdit : public TSpeedButton
{
private:
TCEdit* m_Edit;
public:
__fastcall TSpeedButtonEdit(Classes::TComponent* AOwner);

void __fastcall ChangeEditVisibility ( bool visible );
void __fastcall AcceptText( );

void __fastcall SpeedButtonDblClick(TObject *Sender);

virtual void __fastcall WndProc(Messages::TMessage &Message);
};
//---------------------------------------------------------------------------

class TCEdit : public TEdit
{
private:
TSpeedButtonEdit* m_SB;
public:
__fastcall TCEdit(TComponent* Owner);

void __fastcall CreateParams(TCreateParams &Params);

__property TSpeedButtonEdit* SpeedButton = { read=m_SB, write=m_SB };

void __fastcall EditKeyPress(TObject *Sender, Char &Key);
void __fastcall EditExit(TObject *Sender);
};
//---------------------------------------------------------------------------
########## END FORM'S H ###############



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.