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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a form at cursor location in a textbox 4

Status
Not open for further replies.

ZmrAbdulla

Technical User
Apr 22, 2003
4,364
AE
Is that possible to open small form at the current cursor location of a multiline=true textbox?
I am trying to imitate the intelliscence of the vb code window.
It was possible with vb6 API calls. No idea how with .net.

Thanks

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
on wich event do you want to do it?
is it the mouseposition or the actual cursor in the textbox?

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I am doing code editor. when certain char typed into the textbox the small form should popup at the position of the cursor. Or when you press a certain combination of keys.

It is like when you type "Me." into the code editor the intellisence pops up, or when you press CTRL+Space in the VBA code window.
hope I explained well

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
You may be able to use a context menu and populate the menu items of it. I know it's fairly easy to add one if you just want the user to right click but it appears wherever the mouse pointer is. It may be that you could adapt this to pop it up whenever a user enters a combination of keys instead.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I thought about the popup menu.
One of the main reason I want to avoid using popup menu is it will be having more than 30 items which will look ugly when it pops up. If it is small form then I can use a listbox for items then act as per item selected.
I will be happy if it is anything & easier

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
here is a solution to do it
Code:
    Private Sub txtWriter_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtWriter.MouseUp
        If e.Button = MouseButtons.Right Then
            With frmMySmallForm
                .Show()
                .SetDesktopLocation(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y)
            End With
        End If
    End Sub

Still I need to find the solution to use it with key combination.
Any help will be appreatiated.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
that's a tricky one, and I think you will need to reuse those vb6 api's.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I'm sure you can do this without the API's, I just haven't figured out how yet! I'm playing around with the HotSpot property of the Form and Textbox at the moment but I'm not quite there yet! Once I've figured out the co-ordinates of the cursor (the one in the textbox, not the mouse cursor) then it should be relatively straight forward.

It's whether I can get those or not though..!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I think the cordinates are
Windows.Forms.Cursor.Position.X Windows.Forms.Cursor.Position.Y

My question is how do you say
[tt]
If key = CTRL & L then
frmMyForm.Show()
End if[/tt]

The rest is easy..

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
Once I've figured out the co-ordinates of the cursor

that's the tricky part.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
You're definately right there Chrissie...the best results I've had so far is:
Code:
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim frmMySmallForm As New Form2
        With frmMySmallForm
            .Show()
            .SetDesktopLocation(TextBox1.Location.X - TextBox1.PointToClient(New Point(TextBox1.Cursor.HotSpot.X, TextBox1.Cursor.HotSpot.Y)).X, TextBox1.Location.Y - TextBox1.PointToClient(New Point(TextBox1.Cursor.HotSpot.X, TextBox1.Cursor.HotSpot.Y)).Y)
        End With
    End Sub
Which seems to keep the form in the top left corner of the TextBox but it's proving to be quite tricky!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
And here is the keycode combination from google groups
Code:
  Private Sub txtWriter_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWriter.KeyDown
        If e.Modifiers = Keys.Control Then
            If e.KeyCode = Keys.L Then
                 'Do whatever
             End If
        End If
    End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
To incorporate the CTRL + L combination, here's my latest attempt:
Code:
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.Control = True And e.KeyCode = Keys.L Then
            Dim frmMySmallForm As New Form2
            With frmMySmallForm
                .Show()
                .SetDesktopLocation(TextBox1.Location.X - TextBox1.PointToClient(New Point(TextBox1.Cursor.HotSpot.X, TextBox1.Cursor.HotSpot.Y)).X, TextBox1.Location.Y - TextBox1.PointToClient(New Point(TextBox1.Cursor.HotSpot.X, TextBox1.Cursor.HotSpot.Y)).Y)
            End With
        End If
    End Sub
Hopefully someone cleverer than me will be able to extend this to get the correct co-ordinates!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I haven't had a chance to experiment with this yet, but I was thinking along the lines of using MeasureString to work out where in the TextBox the cursor is and then add this to the .Top and .Left of the TextBox, thus giving the .Top and .Left of the pop-up form. I'll hopefully have a chance to play with this tonight.

[vampire][bat]
 
Hmm, going with E&F's theory. If you could ge the text from the line, and the font being used, you could use MeasureString to find the x value, and then use the line number and font height to get the y value.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I'm close:

Code:
 Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown

    If e.KeyCode = Keys.Decimal Then
      Dim f2 As New Form2
      Dim pt As New Point
      pt = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
      pt.X += RichTextBox1.Location.X + Me.Location.X + 5
      pt.Y += RichTextBox1.Location.Y + Me.Location.Y + 21 + 5
      f2.FormBorderStyle = FormBorderStyle.SizableToolWindow
      f2.Opacity = 0.25
      f2.StartPosition = FormStartPosition.Manual
      f2.Location = pt
      f2.Show()
    End If

  End Sub

The hard coded number are based on border widths and title bar height (I think) and should be available from the Registry - I posted a reply to Ruffnekk (he was after color names for the various Window elements - and I used the Registry to itemise the elements) - so a variation of that code whould pick up that info.

Please note I've use the RichTextBox, so that I could use GetPositionFromCharIndex.

Hope this helps.


[vampire][bat]
 
Nice solution E&F. Hope you don't mind but I've used your code and extended it a bit as it got me interested!

I'll include the whole form in case anyone just wants to copy and paste the code I have so far without creating all the controls:
Code:
Public Class Form5
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'RichTextBox1
        '
        Me.RichTextBox1.Location = New System.Drawing.Point(16, 24)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.Size = New System.Drawing.Size(256, 216)
        Me.RichTextBox1.TabIndex = 0
        Me.RichTextBox1.Text = ""
        '
        'ListBox1
        '
        Me.ListBox1.Items.AddRange(New Object() {"Item1", "Item2"})
        Me.ListBox1.Location = New System.Drawing.Point(96, 88)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(120, 95)
        Me.ListBox1.TabIndex = 1
        Me.ListBox1.Visible = False
        '
        'Form5
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.RichTextBox1)
        Me.KeyPreview = True
        Me.Name = "Form5"
        Me.Text = "Form5"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown

        ListBox1.Visible = False
        If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
            Dim curPoint As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
            curPoint.Offset(RichTextBox1.Left, RichTextBox1.Top + RichTextBox1.Font.Height)
            ListBox1.Location = curPoint
            ListBox1.Visible = Not ListBox1.Visible
            ListBox1.BringToFront()
        End If

    End Sub

    Private Sub Form5_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        ListBox1.Visible = False
    End Sub
End Class


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Have a try with this:

Form1 - the main form:

Code:
  Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown

    If e.KeyCode = Keys.Decimal Then
      Dim f2 As New Form2(RichTextBox1)
      Dim pt As New Point
      pt = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
      pt.X += RichTextBox1.Location.X + Me.Location.X + 5
      pt.Y += RichTextBox1.Location.Y + Me.Location.Y + 21 + 5
      f2.FormBorderStyle = FormBorderStyle.SizableToolWindow
      f2.Opacity = 0.4
      f2.StartPosition = FormStartPosition.Manual
      f2.Location = pt
      f2.Show()
    End If

  End Sub

Form2 - the Popup form
Code:
  Private callersRTB As RichTextBox

  Public Sub New(ByVal rtb As RichTextBox)

    Me.new()
    callersRTB = rtb

  End Sub

  Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

    callersRTB.AppendText(ListBox1.SelectedItem.ToString)
    Timer1.Enabled = True

  End Sub

  Private Sub ListBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListBox1.KeyPress

    If e.KeyChar = Chr(13) Then
      callersRTB.AppendText(ListBox1.SelectedItem.ToString)
      Timer1.Enabled = True
    End If

  End Sub

  Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Timer1.Enabled = False
    Me.Close()

  End Sub

The Timer's interval is left at the default 100 and the Timer is intially set to false. For some reason, the DoubleClick event crashes if it just consists of:

callersRTB.AppendText(ListBox1.SelectedItem.ToString)
Me.Close

whereas the KeyPress Event doesn't. Why I don't know but for some reason the code in double click attempts to access the list box after close is called????? The timer solves it.

The ListBox is set to Dock.Fill


I haven't modified the Form1 code to get the size values from the Registry, but the necessary key is:

HKEY_CURRENT_USER\Control Panel\Desktop\WindowsMetrics

My post of 12 Nov 05 15:35 in thread796-1149139 will show you how to get the Registry data.

Hope this helps.


[vampire][bat]
 
Oh, and I tried it using a ListBox on the same form rather than creating an instance of a new form (makes no difference I guess, but it just seemed easier!).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top