• 0

[vb .net] Find cursor position in textbox


Question

17 answers to this question

Recommended Posts

  • 0

Try: taken from http://www.vb-helper.com/howto_track_textbox_caret.html

Private Sub CheckPosition()

Dim char_pos As Long

Dim row As Long

Dim col As Long

char_pos = SendMessage(Text1.hwnd, EM_GETSEL, 0, 0)

char_pos = char_pos \ &H10000

row = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, _

char_pos, 0) + 1

col = char_pos - SendMessage(Text1.hwnd, EM_LINEINDEX, _

-1, 0) + 1

lblPosition.Caption = "(" & Format$(row) & ", " & _

Format$(col) & ")"

End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As _

Integer)

CheckPosition

End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As _

Integer)

CheckPosition

End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As _

Integer, X As Single, Y As Single)

CheckPosition

End Sub

Private Sub Text1_MouseUp(Button As Integer, Shift As _

Integer, X As Single, Y As Single)

CheckPosition

End Sub

  • 0
  bangbang023 said:
meaning natively it doesn't have support for it. There are some work around "hacks" but it's not worth it just to simplify the current line number method.

584831062[/snapback]

do you mean drag and drop files into the richtextbox?

  • 0
  bangbang023 said:
yes. works fine with textboxes but after trying ot get it to work with richtext and looking online, it seems as though it's not as "accessible" as it is with textboxes.

584833023[/snapback]

why is not as accessible? I'm looking at the control and it supports d-n-d.

  • 0
  bangbang023 said:
I don't know, but it doesn't work at all for me.

584834030[/snapback]

May I see your DragDrop handler code?

<edit>I just got it to work dragging from a listbox to a richtextbox.

This is all i needed in the richtextbox DragDrop event handler

richTextBox1.SelectedText = e.Data.GetData("System.String", true).ToString();

  • 0

im dragging and dropping from explorer.......I have a textbox in a child form. I put the drag drop event in the parent form and t picks it up fine. Does not do so with the richtextbox. I tired localizing the drop drop code from the parent form to the childform and have it only read drag drop events on the richtextbox itself and still nothing. This is the code I'm using in the parent form:

Private Sub mainForm_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            e.Effect = DragDropEffects.All
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub


    Private Sub mainForm_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
        Dim number As Integer = noteArray.Count
        Dim title As String

        'get file name and location
        Dim s() As String = e.Data.GetData("FileDrop", False)

        Dim i As Integer = 0
        While i &lt; s.Length
            'creates a new notepad window
            Call createNewPad()

            title = s(i).Substring(s(i).LastIndexOf("\") + 1)
            noteArray(number).text = title
            Me.tabPads.TabPages(number).Text = title

            'open file and input text into txtMain
            Dim dragStream As New System.IO.FileStream(s(i), IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
            Dim dragReader As New System.IO.StreamReader(dragStream)
            noteArray(number).txtMain.text = dragReader.ReadToEnd
            dragReader.Close()
            dragStream.Close()

            noteArray(number).txtMain.select(0, 0)
            noteArray(number).fileNameExists = True
            noteArray(number).contentchanged = False
            noteArray(number).filenamesaved = s(i)
            number = number + 1
            i = i + 1
        End While

        'give focus to NexPad
        Me.Activate()
        noteArray(i).txtMain.focus()

    End Sub

Even when I specifically set it to the rich text box, still no go.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Poorly or not, you're confronted to a decision when you visit this kind of website.
    • This tool is for ripping CDs not burning them. So it's to save cd audio tracks as files. Simple old tool to do it. For burning I used to use Nero Burning Tools back in the day rather than the ones you listed.
    • "The company has recently announced the forming of a new agentic AI team to develop an agentic AI framework for use in robotics." I wonder if any of their executives attented, "Microsoft Build 2025" last month?
    • Microsoft Build conference to leave Seattle after years in the city by Pradeep Viswanathan Microsoft Build is a flagship annual developer conference held by Microsoft since 2011. In 2017, Microsoft relocated Build from San Francisco to its home turf in Seattle. The proximity to its main campus allowed greater participation from its own engineers and executives, offering attendees a more integrated experience. Today, Jonathan Choe revealed on X that Microsoft has decided to move its Build developer conference out of Seattle. He discovered this information via an email sent by Visit Seattle to its members. Visit Seattle is a private, non-profit destination marketing organization that promotes travel to Seattle and King County. As a result of the move, Seattle-based hotels could lose approximately 9,314 room nights annually. Surprisingly, Visit Seattle’s message included several details about Microsoft’s reasons for the change. The primary reason appears to have come from within Microsoft itself. The company feels that Build lost momentum post-COVID, and the scheduling conflict with Google I/O, a competing event, has made it harder to attract the intended audience. Microsoft’s leadership believes they can better re-energize the program and boost attendance by moving it out of Seattle. The note even suggests that Build will likely be relocated to San Francisco or Las Vegas in 2026. Another factor contributing to the decision is the condition of the city. Last month, during the conference, Microsoft leadership and attendees walked between the Hyatt Regency and the Arch building on 8th Street. Reportedly, there were complaints about the general uncleanliness of the area, the visible presence of individuals using drugs, and unhoused individuals in a recurring tent in the Arch Tunnel. Visit Seattle believes these concerns also played a role in Microsoft’s final decision to relocate Build. Given Microsoft's high-profile presence and the economic impact of the event, this relocation may spark a political storm within the Seattle mayor’s office, drawing criticism over the city’s handling of downtown conditions.
    • This is very exciting, happy to see it come back to RC.
  • Recent Achievements

    • One Year In
      Vladimir Migunov earned a badge
      One Year In
    • One Month Later
      daelos earned a badge
      One Month Later
    • Week One Done
      daelos earned a badge
      Week One Done
    • Mentor
      Karlston went up a rank
      Mentor
    • One Month Later
      EdwardFranciscoVilla earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      493
    2. 2
      snowy owl
      252
    3. 3
      +FloatingFatMan
      250
    4. 4
      ATLien_0
      220
    5. 5
      +Edouard
      169
  • Tell a friend

    Love Neowin? Tell a friend!