Urgent Button Click Procedure Question

Hi there,

I i'm trying to write a simple code editor.

I have some buttons on toolbars which when clicked inserts code on the Editor.

e.g:  I click the Bold Button------>Editor shows <b></b>  

I need the cursor to go between the tags.

e.g: <b>cursor here</b>  and so on with other tags.

thanks

SFern.

 

 
sfernAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BradyPrincipal Data EngineerCommented:
Try this for now it works with editbox.

procedure TForm1.Button1Click(Sender: TObject); // Assuming you have button/function to add tag
begin
edit1.Text := '<b></b>';
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
edit1.SelStart := (length(edit1.Text) - 4);  // Once tags are inserted, no matter where user positions cursor it
                                                            // will type between the tags

end;

Are you going to be adding tags throughout the process and if so, what component do the tags get added to (Editbox/Memo) etc....

cheers

Elvin
sfernAuthor Commented:
I'm using SynEdit as the editor.

I need the user to see the cursor between the tags.

thanks

SFern.
Mark BradyPrincipal Data EngineerCommented:
Working on it now.  I don't have Synedit installed (separate component) but from what I have just researched on Google, you should be able to assign to string values from the control.  For eg;

label1.Caption  := IntToStr(length(SynEdit.Text));

So I suggest you may need to make a procedure that copies the lines.text into a string and then use a loop to check the string for any tags (or the latest tags) then sets the 'SelStart' pos from there.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

sfernAuthor Commented:
But i need to add the text, not to replace it.

For example SynEdit1.Text:=('whatever'); will replace all existing text and Line.Add will put it on a new line...a bit of a pain really :o/
Mark BradyPrincipal Data EngineerCommented:
Use a string to store the SynEdit1.text.

var s: string;

begin
s:= synedit1.lines.text;  // or s:= synedit1.text;   // can't test as I don't have the component like I said

Then you can search the string for the tags. Let me see if I can come up with a small procedure for you
sfernAuthor Commented:
OK Thanks :o)
geobulCommented:
Hi,

Would you show us your insert procedure or function? The code you are asking for might be easily added there.

Regards, Geo
Mark BradyPrincipal Data EngineerCommented:
Yes that would be a help to me to.  I have severall buttons creating the different tags <center></center> .  Need to get some sleep right now but if another expert posts the result before I wake up then goody goody gumdrops c:)
sfernAuthor Commented:
I've got toolbar buttons.

If i click on Bold Button for example, i need <b></b> to be placed on my editor (SynEdit but should work the same on a richedit or memo.), at the caret position and once there the cursor would be place between the tags.

You can do the tests on a memo, the code should be valid.

thanks

SFern.

Mark BradyPrincipal Data EngineerCommented:
Ok SFern I will test this in the morning.. ....
Elvin cachin sum zzzzZZZZZ's  :)
Steven-FernandezCommented:
If you want to place some string on the cursor position just define the value of the string and copy it to the clipboard...then paste it on to your Synedit component. It's an easy and effective way.

procedure TForm1.Button1Click(Sender: TObject);
var s: string;
begin
   s:=('<b></b>');
   Clipboard.AsText := s;
   SynEdit.PasteFromClipboard;
end;

p.s: Don't forget .... Uses Clipbrd :o)
sfernAuthor Commented:
Cool thanks that's part of my problem solved.

Now i just need to position the cursor between the tags :o)

Steven-FernandezCommented:
In that case you can do it another way.

Forget the Clipboard....

Try this:

with SynEdit do begin
          SelText := '<b></b>'; // place the text at the cursor position
          SetFocus;
          SelStart := SynEdit.SelStart - 4; //This will position the cursor 4 steps back, so it will stay between the tags
     end;

Hope it's want you need.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sfernAuthor Commented:
OK i'll try..thanks :o)
sfernAuthor Commented:
OK it does what i wanted it to do. Works fine!!!

Thanks a million for your time :o)

SFern.
Steven-FernandezCommented:
np :o)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.