tags:

views:

256

answers:

3

Hi, I'm still on D2007 and have need to create a unicode enabled virtual keyboard. So I'm using TMS's unicode buttons to simulate the keys, but what I want to avoid is having to assign

mmo.Lines.Text := mmo.Lines.Text + Button1.Caption;

~50 times. There just has to be a better way to send one character at a time to the memo. Is there a way to use the Tag property in the Object Inspector and assign a value from there, or how?

How would you do this?

+7  A: 

Assign the same OnClick event handler to all your buttons. Instead of referring to each button by name, as you did above with Button1, use the Sender parameter. That's what it's there for. It tells you which control's event was triggered to cause the handler to run.

Sender has the static type TObject. When one of your buttons is clicked, Sender will have the run-time type TButton, or whatever actual class you're using. To get your code to compile , you'll need to type-cast. For example:

procedure TKeyboardForm.ButtonClick(Sender: TObject);
begin
  mmo.Lines.Text := mmo.Lines.Text + (Sender as TButton).Caption;
end;
Rob Kennedy
+1  A: 

A speedbutton of some kind may be a better choice for the buttons, since they don't grab focus. Then the blinking cursor would remain in the memo.

You may get the same result by tweeking the properties of the button you use. I don't know the tms-button, but you can try to set tabstop to false.

Vegar
A: 

I'm getting maybe a little old and I live out in the country so I'm probably just behind the times. I don't know if "snarg" means "I'm still not getting it" or maybe "Sorry, I found my error." On the chance that it means the latter, it seems like this suggestion from Rob Kennedy was right on the money:

procedure TKeyboardForm.ButtonClick(Sender: TObject);
begin
  mmo.Lines.Text := mmo.Lines.Text + (Sender as TButton).Caption;
end;

If you put a break point right on the line inside the event handler and evaluate TButton(Sender).Caption, doesn't it show you the caption of the button you pressed? I see that you are iterating through all the buttons now. If all you want is the caption, it seems like that code should solve it.

Jack