Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API Question 2

Status
Not open for further replies.

Collin

Programmer
Jul 18, 1999
46
US
I'am trying to convert a string length from a text control into the textbox's physical cordinates so that I can pop-up a listbox just to the right of the user text entry.<br>
What I have tryed is to.<br>
1. Get the device context for the textbox, GetDC<br>
2. Get the text extents using GetTextPoint32<br>
3. Show the listbox at the text controls left property plus the text extent.<br>
I guess what I'am after is how to convert the textbox's devices's logical cordinates into the textbox's physical cordinates<br>
any help would be greatly appreciated, if I'am on the wrong track please give me some sugestions.<br>
Thanks<br>
<br>

 
Well try a more simplistic approach.<br>
Each item on a form has a Top, Left, Width and Height<br>
so your list boxes top could be<br>
List1.top = Text1.top + Text1.Height + 100<br>
the Left could be<br>
List1.left = Text1.Left + Text1.Width + 50<br>
Note. 100 and 50 is a staring point to place the List box a little away from the exact edge of the text box..<br>
OK<br>
Or if you want you can also use Text1.height - somevalue or you can get creative. The values in the Hieght are just number and can be manipulated as such.<br>
These are just examples but you can get the idea from that.<br>
I have done similar things to what you are doing and this is the approach I take.<br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Thanks, but I'am trying to pop-up the listbox inside of a multiline textbox based upon where the user is typing at, basicaly I want to pop up the listbox next to the caveat based upon some key user inputed text.
 
Collin, I assume you are trying to emulate the VB editor popup properties list box. I've tried to do the same using straight VB but found the task hopelessly complex. It's a no-brainer to make a list box appear next to the mouse cursor in a text box and it only requires about three lines of code to determine the linear position of the text cursor in the string of characters in a text box.... but mapping the physical position of a text cursor in a multi-line text box eludes me.<br>
This is an interesting discussion and I hope one of our gurus can answer it for the benefit of all. Meanwhile, Collin, you might try approximating the location of the list box based on the last known mouse cursor position or simply give it an arbitrary position on the form, as DougP suggested.
 
Alt,<br>
<br>
If you see a guru - tell him to give me a call would you?<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Hey Mike what's your number<br>
HA HA<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Try this:<br>
<FONT FACE=monospace><br>
Option Explicit<br>
<br>
Private Type POINTAPI<br>
&nbsp;&nbsp;&nbsp;&nbsp;x As Long<br>
&nbsp;&nbsp;&nbsp;&nbsp;y As Long<br>
End Type<br>
<br>
Private Declare Function GetCaretPos Lib &quot;user32&quot; (lpPoint As POINTAPI) As Long<br>
<br>
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim MyPoint As POINTAPI<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim XPos As Long<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim YPos As Long<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;If Button = vbRightButton Then<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ScaleMode = vbPixels<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Call GetCaretPos(MyPoint)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XPos = MyPoint.x + Text1.Left<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YPos = MyPoint.y + Text1.Top<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.Print (&quot;XPos:&quot; & XPos & &quot; YPos:&quot; & YPos)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PopupMenu mnuPopup, 0, XPos, YPos<br>
&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
End Sub<br>
</font><br>
<br>
It only works on Windows 95 or later, but that shouldn't be a problem. <br>
<br>
There are two undesired side effect in this code.<br>
<br>
1) Is that if the MLE control has a scroll bar, and the caret (the thing that points where the next typed entry will go) has scrolled up or down out of the visible range, the popup will appear in a strange location. That's easy to fix with some bounds checking, though.<br>
<br>
2) If the user has installed the Desktop Update with IE4, the right button causes a text-editting popup to appear, rather than your own popup. A workaround to this is if they move their mouse a little off the undesired popup, and click the right button again, your popup appears. I looked through the properties and windows messages to send, but didn't spot anything that looked like it would turn this off. Oh, and using the middle button is out, because the Intellimouse uses it for it's auto-scroll feature. Bummer.<br>
<br>
But in any case, I hope this helps.<br>
<br>
Chip H.<br>
<br>

 
Chip - excellent, looks easy to use as well - I was thinking about this and could pinpoint 3 or 4 places where I'd like to use it - Thanks.<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Chip, I adapted your code so it wouldn't rely on a mouse event. It triggers after a certain string is keyed into the text box. A list box appears at the end of the string &quot;the answer is &quot; and allows the user to select from the list.<br>
It's pretty darn crude but the functionality is more in line with what Collin and I have been looking for.<br>
<br>
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)<br>
Text1.SetFocus<br>
SendKeys List1.Text, True<br>
List1.Visible = False<br>
Found = False<br>
End Sub<br>
<br>
Private Static Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)<br>
<br>
Dim MyPoint As POINTAPI<br>
Dim XPos As Long<br>
Dim YPos As Long<br>
<br>
Search$ = &quot;the answer is &quot;<br>
Tx$ = String$(Len(Search$) + 4, &quot; &quot;) + Text1.Text<br>
<br>
If InStr(Tx$, Search$) And Not Found Then<br>
&nbsp;&nbsp;&nbsp;&nbsp;Text1.SetFocus<br>
&nbsp;&nbsp;&nbsp;&nbsp;SendKeys Chr$(255)<br>
&nbsp;&nbsp;&nbsp;&nbsp;DoEvents<br>
&nbsp;&nbsp;&nbsp;&nbsp;Cpos = InStr(Text1, Chr$(255))<br>
&nbsp;&nbsp;&nbsp;&nbsp;SendKeys &quot;{BS}&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;Found = True<br>
&nbsp;&nbsp;&nbsp;&nbsp;DoEvents<br>
&nbsp;&nbsp;&nbsp;&nbsp;If InStr(Mid$(Tx$, Cpos + 3, Len(Search$) + 1),_<br>
Search$) Then<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Call GetCaretPos(MyPoint)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XPos = MyPoint.X + Text1.Left<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YPos = MyPoint.Y + Text1.Top<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List1.Visible = True<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List1.Move XPos, YPos + 12<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List1.SetFocus<br>
&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
End If<br>
<br>
End Sub<br>
<br>
Of course, The &quot;Found&quot; boolean would have to be declared public and List1 would have to include some items.<br>
The SendKeys Chr$(255) and the subsequent backspace in the KeyUp sub just ensures that the Instr searches relative to the current position in Text1 and &lt;yuk yuk&gt; propagates my Tek-Tips handle.<br>
<br>
Alt<b>255</b><br>

 
<br>
Mike & Alt255 --<br>
<br>
Glad to be of service!<br>
<br>
Chip H.<br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top