Author |
Topic: Multiple text part selection in textbox (Read 272 times) |
|
JohnnyD
Guru
member is offline

"If something should, then it doesn't, isn't, can't, hasn't or won't"

Gender: 
Posts: 654
|
 |
Multiple text part selection in textbox
« Thread started on: Apr 5th, 2012, 6:53pm » |
|
Hi,
Is there a way to highlight multiple parts of text in a textbox?
I want to make a file comparison prog that will highlight only the portions of text that are different, not just the line(s) that the text resides on.
So if I have:
AAAABAAAACAAAADAAAA
in one text and
AAAAEAAAAFAAAAGAAAA
in the other, then B,C,D,E,F & G. will be highlighted, the rest won't.
JohnnyD.
|
|
Logged
|
|
|
|
JohnnyD
Guru
member is offline

"If something should, then it doesn't, isn't, can't, hasn't or won't"

Gender: 
Posts: 654
|
 |
Re: Multiple text part selection in textbox
« Reply #2 on: Apr 7th, 2012, 6:07pm » |
|
Hi Alyce (it's OK, I know what a textbox is!!)
It's not a case of placing multiple controls, it's a case of highlighting several pieces of text in one textbox.
I've had a think and have done it with a couple of graphicboxes. I parse the text in a file one character at a time and if different from the 2nd file, change the colour properties of the print commmand for that character.
It's a bit slow at the moment but I've got a handle on how to speed it up. I'll post it when done.
JohnnyD.
|
|
Logged
|
|
|
|
Alyce Watson
Administrator
member is offline

The help file is your best friend.

Gender: 
Posts: 14310
|
 |
Re: Multiple text part selection in textbox
« Reply #3 on: Apr 7th, 2012, 6:37pm » |
|
JohnnyD, I'm glad you found something that works.
The reason I suggested a richedit control is that it allows you to style the text many ways. Text can appear in a single control in many font faces and sizes and colors, for instance.
|
|
Logged
|
Alyce Liberty BASIC Workshop - a complete IDE for Liberty BASIC
Alyce's Restaurant for Liberty BASIC code, tools and references
|
|
|
Stefan Pendl
Global Moderator
member is offline

Computers are like babies, you must teach them what you like them to do ...

Gender: 
Posts: 4998
|
 |
Re: Multiple text part selection in textbox
« Reply #4 on: Apr 8th, 2012, 04:18am » |
|
You could mimic selection by changing the background color for the text in the rich-edit control, I think.
|
|
Logged
|
Stefan - Homepage
Please give credit if you use code I post, no need to ask for permission.
Liberty BASIC Pro 4.04, Windows 7 Home Premium x64 SP1, AMD Turion X2 RM-70 2GHz, 4GB RAM
|
|
|
Alyce Watson
Administrator
member is offline

The help file is your best friend.

Gender: 
Posts: 14310
|
 |
Re: Multiple text part selection in textbox
« Reply #5 on: Apr 8th, 2012, 07:56am » |
|
on Apr 8th, 2012, 04:18am, Stefan Pendl wrote:| You could mimic selection by changing the background color for the text in the rich-edit control, I think. |
|
My riched DLL allows setting background color for all text in control, not for selections.
|
|
Logged
|
Alyce Liberty BASIC Workshop - a complete IDE for Liberty BASIC
Alyce's Restaurant for Liberty BASIC code, tools and references
|
|
|
MKnarr
Senior Member
member is offline


Gender: 
Posts: 305
|
 |
Re: Multiple text part selection in textbox
« Reply #6 on: Apr 8th, 2012, 08:59am » |
|
on Apr 8th, 2012, 04:18am, Stefan Pendl wrote:| You could mimic selection by changing the background color for the text in the rich-edit control, I think. |
|
Yes that is possible and I used to use that technique to search for a word or phrase in a manual that was displayed in a rich edit control.
Here is a sample program.
Code: nomainwin
'This line must be added to the beginning of the text to be searched
'so the RTF window knows that it is an rtf and the color table is included.
'You can change the amount of each color to change the highlight color.
'It is currentlky set to yellow with red255 and green255.
a$="{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 arial;}} {\colortbl ;\red255\green255\blue0;}"
'This line is the text with the above line added.
text$=a$+"The quick brown fox jumps over the lazy old dog. This is the string that will be searched. AAABBBCCCAAABBBCCC"
WindowWidth = 550
WindowHeight = 400
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
button #main.b1, "Search", [search], UL, 10, 300, 70,40
textbox #main.tb1,100, 310, 200, 25
open "untitled" for dialog_modal as #main
print #main, "font arial bold 10"
#main, "trapclose [closemain]"
open "RICHED32.DLL" for dll as #re
hT=CreateTextEdit(hWnd(#main), 13,20,515,250)
call SetWindowText hT,text$
originaltext$=text$ 'Savbe the original text.
wait
[search]
#main.tb1,"!contents? searchstring$"
searchstring$=lower$(trim$(searchstring$))
if searchstring$="" then wait
searchlength=len(searchstring$)
text$=originaltext$
txt$=lower$(text$)
position=instr(txt$,"blue0;}")+2
foundflag=0 'Set to one if the search srtring is found.
while instr(txt$,searchstring$,position)>0
a=len(text$)
start=instr(txt$,searchstring$,position)
found$=mid$(text$,start,searchlength)
text$=left$(text$,start-1)+"\highlight1"+found$+" \highlight0"+right$(text$,a-(start+searchlength)+1)
txt$=lower$(text$)
position=start+searchlength+22 'This sets the new position +22 characters for the two highlights.
foundflag=1 'The search sttring is found.
wend
if foundflag=1 then 'Print the text$ in the rtf window with the highlights added.
call SetWindowText hT,""
call SetWindowText hT,text$
else
notice "Note!"+chr$(13)+ chr$(34)+searchstring$+chr$(34)+" Not Found."
call SetWindowText hT,originaltext$ 'Put the oroiginal text into the rtf window.
end if
#main.tb1,""
#main.tb1, "!setfocus"
wait
[closemain]
close #main 'The window must be closed before RICHED32.DLL.
close #re
end
sub SetWindowText hWnd, txt$ 'Places text in Help and Manual windows
callDLL #user32, "SetWindowTextA",hWnd As ulong,txt$ As ptr,result As void
end sub
FUNCTION CreateTextEdit(hWin, x, y, w, h)
style = _WS_CHILDWINDOW OR _WS_BORDER OR _WS_VISIBLE or _ES_MULTILINE or _WS_VSCROLL or _ES_READONLY
hInst=GetWindowLong(hWin, _GWL_HINSTANCE)
callDLL #user32,"CreateWindowExA",_
0 As long,"RichEdit" As ptr,_
"" As ptr, style As long,_
x As long,y As long,w As long,h As long,_
hWin As ulong, 0 As long, hInst As ulong,_
0 As long, CreateTextEdit As long
END FUNCTION
FUNCTION GetWindowLong(hWin, type)
callDLL #user32, "GetWindowLongA", hWin As ulong, type As long, GetWindowLong As long
END FUNCTION
Not a simple solution but it does work.
|
| « Last Edit: Apr 8th, 2012, 11:58am by MKnarr » |
Logged
|
|
|
|
JohnnyD
Guru
member is offline

"If something should, then it doesn't, isn't, can't, hasn't or won't"

Gender: 
Posts: 654
|
 |
Re: Multiple text part selection in textbox
« Reply #7 on: Apr 9th, 2012, 5:58pm » |
|
Hi all,
Thanks for all your suggestions. I get richtext now and should have grasped what was being said earlier.
Excuse me for being a bit thick!
JohnnyD
|
|
Logged
|
|
|
|
JohnnyD
Guru
member is offline

"If something should, then it doesn't, isn't, can't, hasn't or won't"

Gender: 
Posts: 654
|
 |
Re: Multiple text part selection in textbox
« Reply #8 on: Apr 13th, 2012, 04:34am » |
|
Hi all,
OK. Probably aimed at Alyce this one.
I have now rejigged the program to use the LBRTF2.DLL and is working quite well.
Now two things. Is it possible to set the background colour of single characters rather than the whole page?
I also want to be able to move both scrollbars together so that I can view both files/positions together.
I can do this with graphicboxes & textboxes using:
Code:
[trackloop]
scan
calldll #kernel32,"Sleep",100 as ulong,r as void
calldll #user32,"GetScrollPos",hT1 As ulong,_SBS_VERT as ulong,pos as ulong
calldll #user32,"SetScrollPos",hT2 as ulong,_SBS_VERT as ulong,pos as ulong,1 as boolean,r as ulong
calldll #user32,"SendMessageA",hT2 As ulong,_EM_GETFIRSTVISIBLELINE As Ulong,0 As ulong,0 As ulong,result As ulong
goto[trackloop]
hT1 = textbox #1, hT2= textbox #2.
This works quite well and #2 will follow #1, but won't work with LBRTF2.
Any suggestions?
JohnnyD.
|
|
Logged
|
|
|
|
Alyce Watson
Administrator
member is offline

The help file is your best friend.

Gender: 
Posts: 14310
|
 |
Re: Multiple text part selection in textbox
« Reply #9 on: Apr 13th, 2012, 06:57am » |
|
on Apr 13th, 2012, 04:34am, JohnnyD wrote: Is it possible to set the background colour of single characters rather than the whole page?
I also want to be able to move both scrollbars together so that I can view both files/positions together.
|
|
In an earlier post in this thread I said, "My riched DLL allows setting background color for all text in control, not for selections." I do not know how to do this for a riched control via API, either. Sorry.
You are not using the correct API calls to scroll the text in the controls. I've written a routine for you. Note that you should get the handles one time at the beginning of the program, not each time you want to scroll.
I exposed the handles of the controls (use GetHandle) so that you can send any message to the controls that richedit controls can accept, so you are not limited to the functions provided by my DLL.
Code:
[scroll]
calldll #rich, "GetHandle",_
ID as long,_ 'ID of control
handle1 as ulong 'returns Windows handle
calldll #rich, "GetHandle",_
ID2 as long,_ 'ID of control
handle2 as ulong 'returns Windows handle
calldll #user32,"SendMessageA",handle1 As ulong,_EM_GETFIRSTVISIBLELINE As Ulong,_
0 As ulong,0 As ulong,firstLine1 As ulong
calldll #user32,"SendMessageA",handle2 As ulong,_EM_GETFIRSTVISIBLELINE As Ulong,_
0 As ulong,0 As ulong,firstLine2 As ulong
numLines=firstLine1-firstLine2
calldll #user32,"SendMessageA",handle2 As ulong,_EM_LINESCROLL As Ulong,_
0 As ulong,numLines As ulong,result As ulong
wait
|
|
Logged
|
Alyce Liberty BASIC Workshop - a complete IDE for Liberty BASIC
Alyce's Restaurant for Liberty BASIC code, tools and references
|
|
|
JohnnyD
Guru
member is offline

"If something should, then it doesn't, isn't, can't, hasn't or won't"

Gender: 
Posts: 654
|
 |
Re: Multiple text part selection in textbox
« Reply #10 on: Apr 13th, 2012, 6:33pm » |
|
OK, can't do coloured background selections, sorry, didn't read that bit. Coloured text will work OK.
As I said, the scroll routine works for ordinary multiline textboxes & graphicboxes, I have used it, but it doesn't work with LBRTF2, even with the GetHandle call.
I can't get your routine to work. The thumb of box #2 stays put. I still need to put something in a loop that will constantly read the position of thumb #1 and set thumb #2.
Code so far:
Code:
nomainwin
open "LBRTF2.dll" for dll as #rich
dim info$(4,4)
WindowWidth=1055:WindowHeight=530
UpperLeftX=(DisplayWidth-WindowWidth)/2
UpperLeftY=(DisplayHeight-WindowHeight)/2
open "File Compare" for window as #main
#main,"trapclose [quit]"
hWin=hwnd(#main)
for a=1 to 2
aa=((a-1)*515)+5
calldll #rich,"CreateRichEd",hWin as ulong,aa as ulong,5 as ulong,512 as ulong,230 as ulong,TextID as ulong
ID(a)=TextID
calldll #rich,"SetBackgroundColor",TextID as ulong,255 as ulong,255 as ulong,255 as ulong,result as void
calldll #rich,"SetSelectionColor",TextID as long,0 as long,0 as long,0 as long,result as void
' height/pts:bold :italic :underline:strikeout
calldll #rich,"SetDefaultFont",TextID as long,"Kaiti" as ptr,8 as long,0 as long,0 as long,0 as long,0 as long,result as void
next a
ID=ID(1):ID2=ID(2)
calldll #rich, "GetHandle",ID as ulong,handle1 as ulong
calldll #rich, "GetHandle",ID2 as ulong,handle2 as ulong
filedialog "Open File","*.*",file1$
if file1$="" then wait
open file1$ for input as #1
txt$(1)=input$(#1,lof(#1))
close #1
filedialog "Open File","*.*",file2$
if file2$="" then wait
open file2$ for input as #2
txt$(2)=input$(#2,lof(#2))
close #2
for a=1 to 2
TextID=ID(a):txt$=txt$(a)
calldll #rich,"DoSetFocus",TextID as ulong,result as void
calldll #rich,"LoadText",TextID as ulong,txt$ as ptr,result as void
next a
for a=1 to len(txt$(1))
if mid$(txt$(1),a,1)<>mid$(txt$(2),a,1) then
char1=a-1:char2=a
for b=1 to 2
TextID=ID(b)
calldll #rich,"SetSelection",TextID as ulong,char1 as ulong,char2 as ulong,result as void
calldll #rich,"SetSelectionColor",TextID as ulong,0 as ulong,255 as ulong,0 as ulong,result as void
next b
end if
next a
calldll #rich,"SetSelection",TextID as ulong,0 as ulong,0 as ulong,result as void
[scroll]
ID=ID(1)
ID2=ID(2)
calldll #rich, "GetHandle",_
ID as long,_ 'ID of control
handle1 as ulong 'returns Windows handle
calldll #rich, "GetHandle",_
ID2 as long,_ 'ID of control
handle2 as ulong 'returns Windows handle
calldll #user32,"SendMessageA",handle1 As ulong,_EM_GETFIRSTVISIBLELINE As Ulong,_
0 As ulong,0 As ulong,firstLine1 As ulong
calldll #user32,"SendMessageA",handle2 As ulong,_EM_GETFIRSTVISIBLELINE As Ulong,_
0 As ulong,0 As ulong,firstLine2 As ulong
numLines=firstLine1-firstLine2
calldll #user32,"SendMessageA",handle2 As ulong,_EM_LINESCROLL As Ulong,_
0 As ulong,numLines As ulong,result As ulong
wait
JohnnyD
|
|
Logged
|
|
|
|
Alyce Watson
Administrator
member is offline

The help file is your best friend.

Gender: 
Posts: 14310
|
 |
Re: Multiple text part selection in textbox
« Reply #11 on: Apr 13th, 2012, 8:58pm » |
|
You need to put something in a loop? In the loop that reads the files and colors the text?
Regardless, here is a modification of your program. There is a button to call the [scroll] routine. You'd INSTEAD call it from within your loop and make it a sub routine that ends with return instead of wait.
I changed sizes to fit on my small notebook.
Code: nomainwin
open "LBRTF2.dll" for dll as #rich
dim info$(4,4)
WindowWidth=800:WindowHeight=530
UpperLeftX=(DisplayWidth-WindowWidth)/2
UpperLeftY=(DisplayHeight-WindowHeight)/2
button #main.scroll, "Scroll",[scroll],UL,10,400
open "File Compare" for window as #main
#main,"trapclose [quit]"
hWin=hwnd(#main)
for a=1 to 2
aa=((a-1)*315)+5
calldll #rich,"CreateRichEd",hWin as ulong,aa as ulong,5 as ulong,312 as ulong,230 as ulong,TextID as ulong
ID(a)=TextID
calldll #rich,"SetBackgroundColor",TextID as ulong,255 as ulong,255 as ulong,255 as ulong,result as void
calldll #rich,"SetSelectionColor",TextID as long,0 as long,0 as long,0 as long,result as void
' height/pts:bold :italic :underline:strikeout
calldll #rich,"SetDefaultFont",TextID as long,"Kaiti" as ptr,8 as long,0 as long,0 as long,0 as long,0 as long,result as void
next a
ID=ID(1):ID2=ID(2)
calldll #rich, "GetHandle",ID as ulong,handle1 as ulong
calldll #rich, "GetHandle",ID2 as ulong,handle2 as ulong
filedialog "Open File","*.*",file1$
if file1$="" then wait
open file1$ for input as #1
txt$(1)=input$(#1,lof(#1))
close #1
filedialog "Open File","*.*",file2$
if file2$="" then wait
open file2$ for input as #2
txt$(2)=input$(#2,lof(#2))
close #2
for a=1 to 2
TextID=ID(a):txt$=txt$(a)
calldll #rich,"DoSetFocus",TextID as ulong,result as void
calldll #rich,"LoadText",TextID as ulong,txt$ as ptr,result as void
next a
for a=1 to len(txt$(1))
if mid$(txt$(1),a,1)<>mid$(txt$(2),a,1) then
char1=a-1:char2=a
for b=1 to 2
TextID=ID(b)
calldll #rich,"SetSelection",TextID as ulong,char1 as ulong,char2 as ulong,result as void
calldll #rich,"SetSelectionColor",TextID as ulong,0 as ulong,255 as ulong,0 as ulong,result as void
next b
end if
next a
calldll #rich,"SetSelection",TextID as ulong,0 as ulong,0 as ulong,result as void
wait
[quit]
close #main:close#rich:end
[scroll]
calldll #user32,"SendMessageA",handle1 As ulong,_EM_GETFIRSTVISIBLELINE As Ulong,_
0 As ulong,0 As ulong,firstLine1 As ulong
calldll #user32,"SendMessageA",handle2 As ulong,_EM_GETFIRSTVISIBLELINE As Ulong,_
0 As ulong,0 As ulong,firstLine2 As ulong
numLines=firstLine1-firstLine2
calldll #user32,"SendMessageA",handle2 As ulong,_EM_LINESCROLL As Ulong,_
0 As ulong,numLines As ulong,result As ulong
wait
|
|
Logged
|
Alyce Liberty BASIC Workshop - a complete IDE for Liberty BASIC
Alyce's Restaurant for Liberty BASIC code, tools and references
|
|
|
Stefan Pendl
Global Moderator
member is offline

Computers are like babies, you must teach them what you like them to do ...

Gender: 
Posts: 4998
|
 |
Re: Multiple text part selection in textbox
« Reply #12 on: Apr 14th, 2012, 07:05am » |
|
Just to make sure, are you both using the latest version of the DLL, since there was an update recently, if I remember correctly.
You can't use the EM_SETSEL message to select multiple portions of the displayed text, but the highlight-tag method should be easy to use.
Another way would be using Johns' Scintilla-framework, since that DLL is meant to be used for things like your comparison tool.
|
|
Logged
|
Stefan - Homepage
Please give credit if you use code I post, no need to ask for permission.
Liberty BASIC Pro 4.04, Windows 7 Home Premium x64 SP1, AMD Turion X2 RM-70 2GHz, 4GB RAM
|
|
|
JohnnyD
Guru
member is offline

"If something should, then it doesn't, isn't, can't, hasn't or won't"

Gender: 
Posts: 654
|
 |
Re: Multiple text part selection in textbox
« Reply #13 on: Apr 15th, 2012, 6:39pm » |
|
Hi Stefan/Alyce,
I got the updated .DLL and the scroll routine now works. I didn't know about the update as it's not one I use much. I have used it in the past, but I don't tend to write much software anymore as I left that job over a year ago, so am unaware of changes.
I'm now very much on track with the project, although it's a bit long winded. I will post more later.
Thanks for all the input.
JohnnyD.
|
|
Logged
|
|
|
|
|