新手求助!Memo中输入的字符如何实时显示在某个Label上

zymsbjm 2005-06-04 01:48:02
我动态生成多个Label,想把它们和Memo连起来,先选中某个标签,然后在Memo中输入文本,在Memo1KeyDown、Memo1KeyPress、Memo1KeyUp三个事件中响应同时改变标签。

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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <ExtDlgs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TScrollBox *ScrollBox1;
TImage *Image;
TOpenPictureDialog *OpenPictureDialog1;
TMemo *Memo1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Memo1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall Memo1KeyPress(TObject *Sender, char &Key);
void __fastcall Memo1KeyUp(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall FormCreate(TObject *Sender);
void __fastcall LabelMouseDown(TObject *Sender,TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall LabelMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall LabelMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
AnsiString FileName1;
AnsiString Name;
bool lbDown;
int x;
int y;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif



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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TLabel *Label[10];
int i=0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (!OpenPictureDialog1->Execute()) return;
FileName1 = OpenPictureDialog1->FileName;
Image->Picture->LoadFromFile(FileName1);
Image->Height=Image->Picture->Height;
Image->Width=Image->Picture->Width;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
Memo1->Visible=true;
AnsiString str_obj_text="ÇëÊäÈëÎı¾";
Memo1->Text=str_obj_text;

Label[i]=new TLabel(this);
Label[i]->Caption="ÇëÊäÈëÎı¾";
Label[i]->Left=150*i;
Label[i]->Top=150;
Label[i]->Width=100;
Label[i]->Height=30;
Label[i]->Parent=ScrollBox1;
Label[i]->OnMouseDown=LabelMouseDown;
Label[i]->OnMouseMove=LabelMouseMove;
Label[i]->OnMouseUp=LabelMouseUp;
Label[i]->Transparent=true;
Label[i]->Show();
i=i+1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key)
{
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Memo1KeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::LabelMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
lbDown=true;
x=X;
y=Y;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabelMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
lbDown=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabelMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(!lbDown)
return;
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Left+=(X-x);
Label->Top+=(Y-y);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{

lbDown=false;
}
//---------------------------------------------------------------------------
...全文
84 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
samchoy 2005-06-05
  • 打赏
  • 举报
回复
以下是我测试通过的,几个有改动的地方

TForm1 *Form1;
TLabel *Label[10];
TLabel *LabelShow = 0;
int i=0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner), lbDown(false)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString str_obj_text="ÇëÊäÈëÎı¾";
Memo1->Text=str_obj_text;

if (i < 10)
{
Label[i]=new TLabel(this);
Label[i]->Caption="ÇëÊäÈëÎı¾";
Label[i]->Left=150*i;
Label[i]->Top=150;
Label[i]->Width=100;
Label[i]->Height=30;
Label[i]->Parent=ScrollBox1;
Label[i]->OnMouseDown=LabelMouseDown;
Label[i]->OnMouseMove=LabelMouseMove;
Label[i]->OnMouseUp=LabelMouseUp;
Label[i]->Transparent=true;
Label[i]->Show();
i=i+1;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Memo1Change(TObject *Sender)
{
if (LabelShow)
LabelShow->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------
zymsbjm 2005-06-05
  • 打赏
  • 举报
回复
我改成这样怎么还是没反应呀


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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TLabel *Label[10];
TLabel *LabelShow;
int i=0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (!OpenPictureDialog1->Execute()) return;
FileName1 = OpenPictureDialog1->FileName;
Image->Picture->LoadFromFile(FileName1);
Image->Height=Image->Picture->Height;
Image->Width=Image->Picture->Width;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString str_obj_text="ÇëÊäÈëÎı¾";
Memo1->Text=str_obj_text;

i=i+1;
Label[i]=new TLabel(this);
Label[i]->Caption="ÇëÊäÈëÎı¾";
Label[i]->Left=150*i;
Label[i]->Top=150;
Label[i]->Width=100;
Label[i]->Height=30;
Label[i]->Parent=ScrollBox1;
Label[i]->OnMouseDown=LabelMouseDown;
Label[i]->OnMouseMove=LabelMouseMove;
Label[i]->OnMouseUp=LabelMouseUp;
Label[i]->Transparent=true;
Label[i]->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
LabelShow->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key)
{
LabelShow->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Memo1KeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
LabelShow->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::LabelMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
lbDown=true;
x=X;
y=Y;
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
Label->Transparent=false;
LabelShow=Label;


}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabelMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
lbDown=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabelMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(!lbDown)
return;
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Left+=(X-x);
Label->Top+=(Y-y);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{

lbDown=false;
}
//---------------------------------------------------------------------------
zymsbjm 2005-06-05
  • 打赏
  • 举报
回复
多谢samchoy,^-^。
samchoy 2005-06-05
  • 打赏
  • 举报
回复
在Memo1事件处理程序中的Sender其实就是Memo1,所以用你的方法是不可能实现的。
你可以定义一个TLabel的全局变量,在LabelMouseDown事件中给它赋值为选中的那个Label,然后在Memo1的事件中对这个全局变量进行操作,只需要在Memo1KeyPress事件中处理即可。
zymsbjm 2005-06-04
  • 打赏
  • 举报
回复
我的Label都是动态生成的
ad9329 2005-06-04
  • 打赏
  • 举报
回复


Label1->Caption=Memo1->Text;

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧