D
Devon_Nullman
Guest
The example is a form with one textbox (TxtSource) that is filled in with "C:\protecte" - adding "d" to the end is supposed to change things but only by using a timer does it seem to work.
should look like this:
then change to this:
How can I do this without a timer? - The code has a paint handler and a standalone drawing sub but they don't do anything
Code:
Option Strict On
Public Class Form1
Private Source As String
Private Locked As Boolean
Private WithEvents PTimer As New Timer With {.Interval = 100, .Enabled = True}
Private Sub SetPrompt()
Dim Prompt As String = ""
Dim PromptSize As SizeF
If Not Locked Then
Prompt = "Not Locked"
Else
Prompt = "Locked"
End If
Using g As Graphics = TxtSource.CreateGraphics
PromptSize = g.MeasureString(Prompt, TxtSource.Font)
g.DrawString(Prompt, Me.Font, Brushes.Gray, New PointF(TxtSource.Width - PromptSize.Width, (TxtSource.Height - PromptSize.Height) / 2))
End Using
End Sub
Private Sub TestFolderName()
If IsProtected(Source) Then
TxtSource.ForeColor = Color.Red
Locked = True
Else
TxtSource.ForeColor = Color.Black
Locked = False
End If
End Sub
Private Function IsProtected(Foldername As String) As Boolean
Return Foldername.ToUpper = "C:\PROTECTED"
End Function
Private Sub TxtSource_TextChanged(sender As Object, e As EventArgs) Handles TxtSource.TextChanged
Source = TxtSource.Text
TestFolderName()
'TxtSource.Invalidate() 'Does not do anything
'TxtSource.Refresh() 'Does not do anything
'SetPrompt() 'Does not do anything
End Sub
Private Sub PTimer_Tick(sender As Object, e As EventArgs) Handles PTimer.Tick
SetPrompt() ' Kind of works
End Sub
Private Sub TxtSource_Paint(sender As Object, e As PaintEventArgs) Handles TxtSource.Paint
Dim Prompt As String = ""
Dim PromptSize As SizeF
If Not Locked Then
Prompt = "Not Locked"
Else
Prompt = "Locked"
End If
Using g As Graphics = e.Graphics
PromptSize = g.MeasureString(Prompt, TxtSource.Font)
g.DrawString(Prompt, Me.Font, Brushes.Gray, New PointF(TxtSource.Width - PromptSize.Width, (TxtSource.Height - PromptSize.Height) / 2))
End Using
End Sub
End Class
Continue reading...
should look like this:
then change to this:
How can I do this without a timer? - The code has a paint handler and a standalone drawing sub but they don't do anything
Code:
Option Strict On
Public Class Form1
Private Source As String
Private Locked As Boolean
Private WithEvents PTimer As New Timer With {.Interval = 100, .Enabled = True}
Private Sub SetPrompt()
Dim Prompt As String = ""
Dim PromptSize As SizeF
If Not Locked Then
Prompt = "Not Locked"
Else
Prompt = "Locked"
End If
Using g As Graphics = TxtSource.CreateGraphics
PromptSize = g.MeasureString(Prompt, TxtSource.Font)
g.DrawString(Prompt, Me.Font, Brushes.Gray, New PointF(TxtSource.Width - PromptSize.Width, (TxtSource.Height - PromptSize.Height) / 2))
End Using
End Sub
Private Sub TestFolderName()
If IsProtected(Source) Then
TxtSource.ForeColor = Color.Red
Locked = True
Else
TxtSource.ForeColor = Color.Black
Locked = False
End If
End Sub
Private Function IsProtected(Foldername As String) As Boolean
Return Foldername.ToUpper = "C:\PROTECTED"
End Function
Private Sub TxtSource_TextChanged(sender As Object, e As EventArgs) Handles TxtSource.TextChanged
Source = TxtSource.Text
TestFolderName()
'TxtSource.Invalidate() 'Does not do anything
'TxtSource.Refresh() 'Does not do anything
'SetPrompt() 'Does not do anything
End Sub
Private Sub PTimer_Tick(sender As Object, e As EventArgs) Handles PTimer.Tick
SetPrompt() ' Kind of works
End Sub
Private Sub TxtSource_Paint(sender As Object, e As PaintEventArgs) Handles TxtSource.Paint
Dim Prompt As String = ""
Dim PromptSize As SizeF
If Not Locked Then
Prompt = "Not Locked"
Else
Prompt = "Locked"
End If
Using g As Graphics = e.Graphics
PromptSize = g.MeasureString(Prompt, TxtSource.Font)
g.DrawString(Prompt, Me.Font, Brushes.Gray, New PointF(TxtSource.Width - PromptSize.Width, (TxtSource.Height - PromptSize.Height) / 2))
End Using
End Sub
End Class
Continue reading...