Getting Carret Position in a Textbox

afwasborstel
05-29-2006, 02:17 PM
I'm creating a program in wich one can type text in a textbox and that text displayed over a bitmap. With drawstring etc.. Now I want so simulate a caret so that it looks that the bitmap behaves like a textbox...only much bigger and stuf..

I can get the position with TextBox1.SelectionStart and then with measurestring calculate the position where I want my caret to be(in same position as in the textbox). I use a timer to get the SelectionStart..

Only problem is when I looke in my systemmonitor...the usage of memory is decreasing rapidly and the drops and starts rebuilding...So I guess this is not such a good idea...

Are there any methods out there wich would help me to resolve the selection start in a textbox without the use of a timer?

jo0ls
05-30-2006, 07:10 PM
It sounds like you are not disposing of something - a Graphics or a Bitmap:

Public Class Form1

Private WithEvents t As Timer

Sub New()
InitializeComponent()
t = New Timer
t.Interval = 50
t.Start()
End Sub

Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
Dim tempBM As New Bitmap(1000, 1000)
Dim g As Graphics = Graphics.FromImage(tempBM)
g.MeasureString("One of the most striking differences between a cat and a lie " & _
"is that the cat has only nine lives. - QOTD", Me.Font, New SizeF(tempBM.Width, tempBM.Height))
' With these dispose calls commented out, memory usage builds up until the
' garbage collector happens to free resources. So look at that...
' Then uncomment them, to see the difference when you free the resources explicitly
' g.Dispose()
' tempBM.Dispose()
End Sub

End Class


Note: if you are reassining to a variable, then call dispose before you reassign.

Public Class Form1

Private WithEvents t As Timer
Private tempBM As Bitmap
Private g As Graphics

Sub New()
InitializeComponent()
t = New Timer
t.Interval = 50
t.Start()
End Sub

Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
If tempBM IsNot Nothing Then tempBM.Dispose()
If g IsNot Nothing Then g.Dispose()
tempBM = New Bitmap(1000, 1000)
g = Graphics.FromImage(tempBM)
g.MeasureString("One of the most striking differences between a cat and a lie " & _
"is that the cat has only nine lives. - QOTD", Me.Font, New SizeF(tempBM.Width, tempBM.Height))
End Sub

End Class

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum