按TAB键,光标在EDIT中移动
有一个EDIT 文本框 里面有一个时间值 如 12:11 ,现在我想按一下TAB 键 ,只选中前2位数即12 ,在按以下TAB 键 就可选中后2位即11 ,请高手指点如何解决,在线等,谢谢!!!
[解决办法]
void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
Edit1-> SelStart = 0;
Edit1-> SelLength = 2;
}
//---------------------------------------
void __fastcall TForm1::Edit1Exit(TObject *Sender)
{
if(Edit1-> SelStart == 0)
{
Edit1-> SetFocus();
Edit1-> SelStart = 3;
Edit1-> SelLength = 2;
}
}
//---------------------------------------
[解决办法]
给你个完整的代码吧
.h文件
//---------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TEdit *Edit1;
private:// User declarations
void __fastcall NewWndProc(TMessage& Message);
public:// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
.cpp文件
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
TWndMethod OldWndProc;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
OldWndProc = Edit1-> WindowProc;
Edit1-> WindowProc = NewWndProc;
}
//---------------------------------------
void __fastcall TForm1::NewWndProc(TMessage& Message)
{
if (Message.Msg == WM_KEYUP&&Message.WParam==9)
{
static flag=0;
if(flag%2==0)
{
Edit1-> SelStart = 0;
Edit1-> SelLength = 2;
}
else
{
Edit1-> SelStart = Edit1-> Text.Length()> =2?Edit1-> Text.Length()-2:0;
Edit1-> SelLength = 2;
}
flag++;
}
OldWndProc(Message);
}
[解决办法]
.h
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TEdit *Edit1;
TEdit *Edit2;
void __fastcall Edit2Enter(TObject *Sender);
void __fastcall Edit2Click(TObject *Sender);
private:// User declarations
bool E2f;//判断edit2是否有资格获得焦点
void __fastcall NewWndProc(TMessage& Message);
public:// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
.cpp
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
TWndMethod OldWndProc;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
E2f=false;
OldWndProc = Edit1-> WindowProc;
Edit1-> WindowProc = NewWndProc;
}
//---------------------------------------
void __fastcall TForm1::NewWndProc(TMessage& Message)
{
if (Message.Msg == WM_KEYUP&&Message.WParam==9)
{
static flag=0;
if(flag%2==0)
{
Edit1-> SelStart = 0;
Edit1-> SelLength = 2;
E2f=false;
}
else
{
Edit1-> SelStart = Edit1-> Text.Length()> =2?Edit1-> Text.Length()-2:0;
Edit1-> SelLength = 2;
E2f=true;
}
flag++;
}
OldWndProc(Message);
}
void __fastcall TForm1::Edit2Enter(TObject *Sender)
{
if(E2f==false)Edit1-> SetFocus();
}
//---------------------------------------
void __fastcall TForm1::Edit2Click(TObject *Sender)
{
E2f=true;
Edit2-> SetFocus();
}
//---------------------------------------
[解决办法]
对Edit1和Edit2的 onEnter和onExit事件控制就可以了
void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
Edit1-> SelStart = 0;
Edit1-> SelLength = 2;
}
//---------------------------------------
void __fastcall TForm1::Edit1Exit(TObject *Sender)
{
if(Edit1-> SelStart == 0)
{
Edit1-> SetFocus();
Edit1-> SelStart = 3;
Edit1-> SelLength = 2;
}
}
//---------------------------------------
void __fastcall TForm1::Edit2Enter(TObject *Sender)
{
Edit2-> SelStart = 0;
Edit2-> SelLength = 2;
}
//---------------------------------------
void __fastcall TForm1::Edit2Exit(TObject *Sender)
{
if(Edit2-> SelStart == 0)
{
Edit2-> SetFocus();
Edit2-> SelStart = 3;
Edit2-> SelLength = 2;
}
}
//---------------------------------------