Results 1 to 14 of 14

Thread: [RESOLVED] Textbox Cursor Size

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Resolved [RESOLVED] Textbox Cursor Size

    Would anyone know how to change the size of the cursor (IBeam) for a textbox, i.e: The blinker at the beginning of the textbox to the size of at least one character.

    Cheers

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Textbox Cursor Size

    Do you actually mean the width?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Textbox Cursor Size

    Yeah ive seen the cursor change event and ive tested some sorta of coding that i thought might work but didn't (probably isnt even the right one) aha

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Textbox Cursor Size

    What you see in a TextBox is not the cursor in that sense. Anything to do with the cursor in WinForms is related to the mouse pointer. What you're talking about is called the caret.

    This code is C# but it seems to address what you want to do:

    http://stackoverflow.com/questions/6...nforms-textbox

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Textbox Cursor Size

    cheers for that although i have no idea how C# works so i wouldnt be able to convert it into vb.net

  6. #6
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: Textbox Cursor Size

    google convert C# to vb.net, then copy and paste
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Textbox Cursor Size

    oh yes thats great. although now im placing the code in the form and its generated a load of issues for other tools. so i didnt save the program so i must be doing something wrong. heres the code: just to confirm, where does each bit go, i tried it and it nearly mucked up the whole thing.

    Code:
    Public Partial Class Form1
    	Inherits Form
    	<DllImport("user32.dll")> _
    	Private Shared Function CreateCaret(hWnd As IntPtr, hBitmap As IntPtr, nWidth As Integer, nHeight As Integer) As Boolean
    	End Function
    	<DllImport("user32.dll")> _
    	Private Shared Function ShowCaret(hWnd As IntPtr) As Boolean
    	End Function
    
    	Public Sub New()
    		InitializeComponent()
    	End Sub
    
    	Private Sub Form1_Shown(sender As Object, e As EventArgs)
    		CreateCaret(textBox1.Handle, IntPtr.Zero, 10, textBox1.Height)
    		ShowCaret(textBox1.Handle)
    	End Sub
    End Class

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Textbox Cursor Size

    Quote Originally Posted by Sean.Downes View Post
    cheers for that although i have no idea how C# works so i wouldnt be able to convert it into vb.net
    C# works pretty much the same as VB but with a C-based syntax rather than a BASIC-based syntax. For instance, that code has a handler for the form's Shown event, just as you would in VB. It uses the DllImport attribute to decorate Windows API functions, just as you would in VB. Etc.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Textbox Cursor Size

    Quote Originally Posted by Sean.Downes View Post
    oh yes thats great. although now im placing the code in the form and its generated a load of issues for other tools. so i didnt save the program so i must be doing something wrong. heres the code: just to confirm, where does each bit go, i tried it and it nearly mucked up the whole thing.

    Code:
    Public Partial Class Form1
    	Inherits Form
    	<DllImport("user32.dll")> _
    	Private Shared Function CreateCaret(hWnd As IntPtr, hBitmap As IntPtr, nWidth As Integer, nHeight As Integer) As Boolean
    	End Function
    	<DllImport("user32.dll")> _
    	Private Shared Function ShowCaret(hWnd As IntPtr) As Boolean
    	End Function
    
    	Public Sub New()
    		InitializeComponent()
    	End Sub
    
    	Private Sub Form1_Shown(sender As Object, e As EventArgs)
    		CreateCaret(textBox1.Handle, IntPtr.Zero, 10, textBox1.Height)
    		ShowCaret(textBox1.Handle)
    	End Sub
    End Class
    Get rid of the constructor and the event handler actually has to hand the event. Try creating a handler for the Load event, or any event for that matter, and you'll see the obvious difference.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Textbox Cursor Size

    oh geee, i have no idea, im not a very good at programming, i barely know the basics :/ thanks anyhow for your help appreciated

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Textbox Cursor Size

    vb.net Code:
    1. Public Class Form1
    2.     Declare Auto Function CreateCaret Lib "user32" (hWnd As IntPtr, hBitmap As IntPtr, nWidth As Integer, nHeight As Integer) As Boolean
    3.     Declare Auto Function ShowCaret Lib "user32" (hWnd As IntPtr) As Boolean
    4.  
    5.     Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
    6.         CreateCaret(TextBox1.Handle, IntPtr.Zero, 10, TextBox1.Height)
    7.         ShowCaret(TextBox1.Handle)
    8.     End Sub
    9. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Textbox Cursor Size

    Hi thanks, this works great but for some reason it adds the text im typing into a different textbox, ive checked the code just incase i put the wrong textbox number and i definently havent and also when i click a different textbox it returns to the normal skinny blinker

    thank

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Textbox Cursor Size

    Only one control can own the text caret at the time so Windows will automatically destroy and recreate the caret when you move focus between controls. Move the two lines of code from the Form_Load event to the GotFocus event of the TextBox in question.
    Last edited by Joacim Andersson; Apr 30th, 2013 at 03:08 AM.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Textbox Cursor Size

    Works perfect thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width