none
GetCharIndexFromPosition Maximum Index = 65535 RRS feed

  • Question

  • Hi!

    I am trying to implement a drag&drop function in a textbox.

    Unfortunately there is a problem when using GetCharIndexFromPosition:

    The maximum index it detects is 65535, after that value, the index resets and starts from 0 to 65535 again and again.

    So it is impossible to drop text into a textbox beyond index 65535.

    Is there any way I can solve this?

    Thank you very much!!

        Private Sub TextBox1_DragOver(sender As Object, e As DragEventArgs) Handles TextBox1.DragOver
            TextBox1.Select()
            Dim index As Long = TextBox1.GetCharIndexFromPosition(TextBox1.PointToClient(Cursor.Position))
            TextBox1.SelectionStart = index
            TextBox1.SelectionLength = 0
        End Sub

    Saturday, September 23, 2017 11:14 PM

Answers

All replies

  • You can use String.Substring to do that.

    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Saturday, September 23, 2017 11:32 PM ...wrong overload
    Saturday, September 23, 2017 11:26 PM
  • What do you mean?

    All I want to know is how to detect an index position greater than 65535.

    How could Substring help me?


    • Edited by vanerio Saturday, September 23, 2017 11:31 PM
    Saturday, September 23, 2017 11:31 PM
  • What do you mean?

    All I want to know is how to detect an index position greater than 65535.

    How could Substring help me?


    I guess I misunderstood.

    You want to know the zero-based index of a particular substring then?

    If so, use .IndexOf.

    That's the first of them (if there's more than one).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, September 23, 2017 11:36 PM
  • No!!

    I want to use the GetCharIndexFromPosition function, and I want it to return a value greater than 65535.

    Saturday, September 23, 2017 11:37 PM
  • No!!

    I want to use the GetCharIndexFromPosition function, and I want it to return a value greater than 65535.

    Then you want to get the value specifically from a TextBox:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.getcharindexfromposition(v=vs.110).aspx

    It shows that the return is an Int32 so that means that the maximum is Int32.Max. I can't imagine why you're seeing what you are.

    Sorry that I can't help...


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, September 23, 2017 11:45 PM
  • No!!

    I want to use the GetCharIndexFromPosition function, and I want it to return a value greater than 65535.

     Are you are using a TextBox or a RichTextBox?  By default,  a TextBox's MaxLength property is 32767 characters and a RichTextBox's MaxLength property is 2147483647.

     Try either setting the MaxLength property of the TextBox to a higher value or try using a RichTextBox.  A RichTextBox has the GetCharIndexFromPosition function too.


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Frank L. Smith Sunday, September 24, 2017 12:00 AM
    • Edited by IronRazerz Sunday, September 24, 2017 12:01 AM
    Saturday, September 23, 2017 11:56 PM
  • Thank you but MaxLength is not the problem here
    Sunday, September 24, 2017 12:10 AM
  • Int32 maximum is 2147483647

    As I have said I want to use GetCharIndexFromPosition in order to get the index from mouse position over the textbox. It is a basic drag&drop function in most text editors to insert text at mouse position.

    • Edited by vanerio Sunday, September 24, 2017 12:14 AM
    Sunday, September 24, 2017 12:11 AM
  • Int32 maximum is 2147483647

    This is a known issue with the textbox.  Please see the discussion here:
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/5f8a35ef-dd21-41f7-a7e9-8f631b6f5045

    The workaround is to create your own GetCharIndexFromPosition that doesn't rely on the API function and use it in a derived text box class.

    • Marked as answer by vanerio Sunday, September 24, 2017 9:23 PM
    Sunday, September 24, 2017 1:54 AM
  • Thank you very much!

    So sad to hear that :(

    Could you please help me at creating this own function? I don´t even know how to start it

    Thank you again!!!

    Sunday, September 24, 2017 9:27 AM
  • Thank you but MaxLength is not the problem here

     As i said,  use a RichTextBox instead of a TextBox.  The RichTextBox does not have this problem and would probably be easier than creating a workaround for the problem.

        Private Sub RichTextBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseMove
            Me.Text = RichTextBox1.GetCharIndexFromPosition(e.Location).ToString
        End Sub
     

     

     The form's caption text shows the index which is correct...

     


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, September 24, 2017 10:53 AM
    • Proposed as answer by dbasnett Sunday, September 24, 2017 12:57 PM
    Sunday, September 24, 2017 10:48 AM
  • Thank you IronRazerz!

    Of course richtextbox is a good alternative and it works really well with drag&drop. But I don´t know if it could work for my project as I need it to be just plain text (I don´t want the users could drop Rich Text into it).

     
    • Edited by vanerio Sunday, September 24, 2017 1:43 PM
    Sunday, September 24, 2017 1:42 PM
  • (I don´t want the users could drop Rich Text into it).

    What gets updated when the user drops something into a RTB is up to you. 

    Sunday, September 24, 2017 9:15 PM
  • Correct. I hoped there would be an easy solution for the GetCharIndexFromPosition workaround (easier and faster than the richtextbox solution).

    Thank you!

    Sunday, September 24, 2017 9:23 PM
  • Correct. I hoped there would be an easy solution for the GetCharIndexFromPosition workaround (easier and faster than the richtextbox solution).

    Thank you!

     As Acamar has said,  you have full control over what is allowed to be dropped on and added to the RTB.  You can just check if the Data of the dragged object is Ansi or Unicode text before allowing it to be dropped.  The example below works for dragging text into the RTB from other apps like Wordpad and does not even require the use of the GetCharIndexFromPosition method.

     This example only requires a RichTextBox added to a form to test.  Note the commented out line,  you can use it if you want to see all the data formats like Rtf,  images,  ole objects,  and so on.  This will allow you to see what types you need to filter out.  It does not seem like it would be very difficult to switch to a RTB from a TextBox.

     
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            RichTextBox1.AllowDrop = True
        End Sub
    
        Private Sub RichTextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles RichTextBox1.DragDrop
            RichTextBox1.SelectedText = e.Data.GetData(GetType(String)).ToString
        End Sub
    
        Private Sub RichTextBox1_DragEnter(sender As Object, e As DragEventArgs) Handles RichTextBox1.DragEnter
            'RichTextBox2.Lines = e.Data.GetFormats 'this shows the formats that the Data object contains so that you can see which ones you might need to filter out
    
            'this allows Ansi or Unicode text to be dropped.  Rich Text formatting and ole objects are not copied to the RTB, only the text is copied.
            If e.Data.GetDataPresent(DataFormats.Text) OrElse e.Data.GetDataPresent(DataFormats.UnicodeText) Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.None
            End If
        End Sub
    End Class
    

     

     


    If you say it can`t be done then i`ll try it

    Monday, September 25, 2017 1:12 AM
  • Thank you very much to everybody for your time and help!
    Monday, September 25, 2017 10:50 AM