I AM DOING PROJECT IN VB 6.0 AND USING RICHTEXTBOX
WHEN WE Type in “vo” IN RICHTEXTBOX and press “Ctrl + Enter”. The word “void” must be compeleted.LIKE THAT I HAVE TO DO SAME FOR ALL KEYWORDS CAN ANYONE SUGGEST
Printable View
I AM DOING PROJECT IN VB 6.0 AND USING RICHTEXTBOX
WHEN WE Type in “vo” IN RICHTEXTBOX and press “Ctrl + Enter”. The word “void” must be compeleted.LIKE THAT I HAVE TO DO SAME FOR ALL KEYWORDS CAN ANYONE SUGGEST
You will have to store the words being looked up somewhere (vo could have been vocabulary or some other word)... you could return a subset of those words in a floating listbox (your other thread) for word completion... if only one word matches then complete the word.
instead of all words,we need auto completion for only keywords.so,"for example,if we type v and press ctrl+enter,then void should be completed.otherwise we can go on with our word.no need of auto completion then"is there any possibility for this without use of listbox
exactly, how will the computer know that void is one of the possible words when competed? where does the list of words that will be autocompleted reside?
As leinad31 posted, you'll need to store the list of keywords somewhere and paste them in the RTB when matching word found.
If you don't want to show a floating listbox, then there is a problem. If user types very few letters then how will you decide which one to paste ?
Run the code below.
there are 3 keywords that starts wih "d" - double, default and do.
But if user just types "d" and Ctrl+Enter, the searching code will always pickup the first match ("double").
Similarly, if you type "do", then it can't decide between "double" and "do".
So, give the user a floating listbox like VB IDE (similar to the code posted in your other thread) to choose form.
vb Code:
' add a RichTextBox in a form and paste this code Option Explicit Private Declare Function LockWindowUpdate Lib "user32" ( _ ByVal hwndLock As Long) As Long Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _ ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByRef lParam As Any) As Long Private Const EM_LINEINDEX As Long = &HBB Private KeyWords() As String Private Sub Form_Load() KeyWords = Split("auto,const,double,float,int,short,struct,unsigned,break,continue,else,for,long,signed,switch,case,default,enum,void,goto,register,sizeof,typedef,volatile,char,do,extern,if,return,static,union,while", ",") End Sub Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer) Dim CtrlDown As Boolean Dim LastSpace As Long Dim FindString As String Dim NewString As String Dim LineStart As Long CtrlDown = (Shift And vbCtrlMask) > 0 If CtrlDown And KeyCode = 13 Then If RichTextBox1.SelStart > 0 Then ' get first character of line --> LineStart = SendMessage(RichTextBox1.hwnd, EM_LINEINDEX, RichTextBox1.GetLineFromChar(RichTextBox1.SelStart), 0&) If LineStart = RichTextBox1.SelStart - 1 Then LastSpace = LineStart Else ' YOU NEED TO ADD CHECK FOR MORE TERMINAL CHARACTERS HERE --> LastSpace = InStrRev(RichTextBox1.Text, " ", RichTextBox1.SelStart) End If If LastSpace >= 0 Then FindString = Mid$(RichTextBox1.Text, LastSpace + 1, RichTextBox1.SelStart - LastSpace) If FindString <> "" Then NewString = GetAutoCompleteText(FindString) If NewString <> "" Then LockWindowUpdate RichTextBox1.hwnd RichTextBox1.SelStart = LastSpace RichTextBox1.SelLength = Len(FindString) RichTextBox1.SelText = NewString LockWindowUpdate 0 End If End If End If End If KeyCode = 0 End If End Sub Private Function GetAutoCompleteText(FindString As String) As String Dim i As Long For i = 0 To UBound(KeyWords) If Left$(KeyWords(i), Len(FindString)) = FindString Then GetAutoCompleteText = KeyWords(i) Exit Function End If Next GetAutoCompleteText = "" End Function
If you decide to use a ListBox, see this thread (uses LB_FINDSTRING message).
There is another Message, LB_FINDSTRINGEXACT, that will give you exact match.
My idea is, (like VB)
1. Load the list of keywords in the listbox. keep it in sorted order and keep it hidden.
2. Detect Ctrl+Enter and get text to search (using above code)
3. Search the listview using LB_FINDSTRINGEXACT.
4. If match found, paste the string in RTB.
5. If exact match not found, show the listbox like your other thread.
6. find/highlight the matching text using LB_FINDSTRING
your code written in post 5 is working properly.but we have encountered a small problem.if we want to perform auto complete at the starting of line i.e (no space is prezent in that line yet),then that code can't work as we are checking for " " in richtextbox using instrrev.
Yes. That code is not complete. :pQuote:
Originally Posted by RAKESHTATI
In addition to space (" "), you'll need to add check for puntuation marks, vbNewLine, first-word-of-document etc 'word-termination-characters'.
GUD MRNG.
if we check for the first word of the document,how can we get autocomplete when we press ctrl+enter after "in" instead of after "i".
how can we overcome this?
I've edited my previous code. try this time. Solves first-char-of-document problem.
The "in"/"i" problem is causing because we are only searching for space (" ").
Add search for vbLf, then it will not happen,
ok thanks a lot mr.iprank it is working as we need