text box saying instructions

Glen

Member
Joined
Oct 15, 2012
Messages
12
Programming Experience
Beginner
Im currently teaching myself to be familiar with this language
Right now im familiarizing some techniques and I am stuck at this dilemma.

Can anyone help me or suggest some tips? I am looking through the net but somehow I cant manage to find any tips so Im hoping that anyone here can help me...

As a practice, Im creating a simple program that can add and subtract two integer. But Im putting some user constraints.


My dilemmas are these, I want to have a text in the text box saying instructions to the user.
The other is I want to disable the two operating buttons which are ADD and SUBTRACT if there is no data input.

Like this, see how the text in the text boxes are not in regular color... So as the Add and Subtract buttons.
NOTE: I just edited this pic but this is how my app should be

1.jpeg


Then, when a data has been inserted, the text will be what the user had entered on the textbox and the
two operating buttons shall be enabled...

2.jpeg

Any Tips would help... Thanks...
 
The buttons are fairly easy, you just set the buttons .Enabled property to true or false...

The shadowed text in the textboxes require some coding...
 
The instructional text can actually be done relatively easily, although it's a bit of a hack. You can set the Text of your TextBox to the instruction by default and set the ForeColor so it's greyed. You can then handle the Enter and Leave events of the TextBox. On the Enter event, you test whether the ForeColor is set for the instruction and, if it is, you set it for normal input and clear the Text. On the Leave event, if the TextBox is empty then you reinstate the appropriate instruction text and colour. The one thing it won't do that such ghost text does when it's implemented at a lower level is remain in the control when it has focus until the user types something. That's probably more trouble than it's worth though.
 
The buttons are fairly easy, you just set the buttons .Enabled property to true or false...

The shadowed text in the textboxes require some coding...

Thanks for the tip sir powerade:D.

I've tried the tips for the buttons and they where disabled, but what would be the algorithm or command if I want the buttons to be enabled if you have entered an integer value on the textboxes?
 
The instructional text can actually be done relatively easily, although it's a bit of a hack. You can set the Text of your TextBox to the instruction by default and set the ForeColor so it's greyed. You can then handle the Enter and Leave events of the TextBox. On the Enter event, you test whether the ForeColor is set for the instruction and, if it is, you set it for normal input and clear the Text. On the Leave event, if the TextBox is empty then you reinstate the appropriate instruction text and colour. The one thing it won't do that such ghost text does when it's implemented at a lower level is remain in the control when it has focus until the user types something. That's probably more trouble than it's worth though.

Your tip is very interesting sir but Its hard for me as a newbie to understand...:D Do you have any simpler way to elaborate this? although I find it very interesting...
 
Thanks for the tip sir powerade:D.

I've tried the tips for the buttons and they where disabled, but what would be the algorithm or command if I want the buttons to be enabled if you have entered an integer value on the textboxes?

You should check in your textchanged event if the inserted value is numeric. If it is, then enable the button, if it's not, then disable it...
 
I found the following code here for a watermarked textbox. It is somewhat advanced, but it does what you want.

Option Strict On

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class WaterMarkTextBox
    Inherits TextBox

    Private oldFont As Font = Nothing
    Private waterMarkTextEnabled As Boolean = False

#Region "Attributes"
    Private _waterMarkText As String = "Watermark"

    <Category("Appearance")> _
    <Description("The watermarked text associated with the control.")> _
    Public Property WaterMarkText() As String
        Get
            Return _waterMarkText
        End Get
        Set(ByVal value As String)
            _waterMarkText = value
            Me.Invalidate()
        End Set
    End Property

    Private _waterMarkColor As Color = Drawing.Color.Gray

    <Category("Appearance")> _
    <Description("The color of the watermarked text.")> _
    Public Property WaterMarkColor() As Color
        Get
            Return _waterMarkColor
        End Get
        Set(ByVal value As Color)
            _waterMarkColor = value
            Me.Invalidate()
        End Set
    End Property

#End Region

    ' Default constructor
    Public Sub New()
        JoinEvents(True)
    End Sub

    Private Sub JoinEvents(ByVal join As Boolean)
        If join Then
            AddHandler (TextChanged), AddressOf WaterMark_Toggle
            AddHandler (LostFocus), AddressOf WaterMark_Toggle
            AddHandler (FontChanged), AddressOf WaterMark_FontChanged
            'No one of the above events will start immeddiatlly 
            'TextBox control still in constructing, so,
            'Font object (for example) couldn't be catched from within WaterMark_Toggle
            'So, call WaterMark_Toggel through OnCreateControl after TextBox is totally created
            'No doupt, it will be only one time call

            'Old solution uses Timer.Tick event to check Create property
        End If
    End Sub

    Private Sub WaterMark_Toggle(ByVal sender As Object, ByVal args As EventArgs)
        If Me.Text.Length <= 0 Then
            EnableWaterMark()
        Else
            DisableWaterMark()
        End If
    End Sub

    Private Sub WaterMark_FontChanged(ByVal sender As Object, ByVal args As EventArgs)
        If waterMarkTextEnabled Then
            oldFont = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
            Refresh()
        End If
    End Sub

    Private Sub EnableWaterMark()
        'Save current font until returning the UserPaint style to false (NOTE: It is a try and error advice)
        oldFont = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)

        'Enable OnPaint Event Handler
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.waterMarkTextEnabled = True

        'Trigger OnPaint immediatly
        Refresh()

    End Sub

    Private Sub DisableWaterMark()
        'Disbale OnPaint event handler
        Me.waterMarkTextEnabled = False
        Me.SetStyle(ControlStyles.UserPaint, False)

        'Return oldFont if existed
        If Not oldFont Is Nothing Then
            Me.Font = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
        End If
    End Sub

    ' Override OnCreateControl 
    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        WaterMark_Toggle(Nothing, Nothing)
    End Sub

    ' Override OnPaint
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        ' Use the same font that was defined in base class
        Dim drawFont As Font = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
        ' Create new brush with gray color or 
        Dim drawBrush As SolidBrush = New SolidBrush(Me.WaterMarkColor) 'use WaterMarkColor
        ' Draw Test or WaterMark
        e.Graphics.DrawString(IIf(waterMarkTextEnabled, WaterMarkText, Text).ToString(), drawFont, drawBrush, New Point(0, 0))
        MyBase.OnPaint(e)
    End Sub

End Class
 
I found the following code here for a watermarked textbox. It is somewhat advanced, but it does what you want.

Thanks again...!

But as a beginner I think its too much for me:D... Maybe later though.

I wanna focus on enable-ling and disable-ling the buttons... can you give me some codes for that?

I wanna use If else and then statement...
This is my logic/psudo-code but I don't know how to put it in codes. :(




If
{
Textbox1.Text and Textbox2.Text 'has integer values'
}
Then
{
Button1.enable = true
Button2.enable = true
}

else
{
Button1.enable = false
Button2.enable = false

Messagebox.show = "Wrong Input: Please try again""
}


end IF
 
This, I think, is the easiest way for a beginner to do this.

If IsNumeric(TextBox1.Text) Then
    'Textbox1 contains a numeric value
    Button1.Enabled = True

Else
    'Textbox1 does not contain a numeric value
    Button1.Enabled = False

End If


That code should be put in a sub that handles the Textchanged events of the textboxes.


Sidenote:
To write 'true' .Net-code, the TryParse method would probably be more correct to use...
 
Last edited:
This, I think, is the easiest way for a beginner to do this.

If IsNumeric(TextBox1.Text) Then
    'Textbox1 contains a numeric value
    Button1.Enabled = True

Else
    'Textbox1 does not contain a numeric value
    Button1.Enabled = False

End If


That code should be put in a sub that handles the Textchanged events of the textboxes.


Sidenote:
To write 'true' .Net-code, the TryParse method would probably be more correct to use...

Interestingly, IsNumeric calls Double.TryParse internally. In fact, it was to improve the performance of IsNumeric that the Double.TryParse method was created in the first place. It was so successful that many other TryParse methods were added later.

In this case, the OP did say that they wanted to add integers. That means that IsNumeric is not appropriate and Integer.TryParse should be used.
 
This, I think, is the easiest way for a beginner to do this.

If IsNumeric(TextBox1.Text) Then
    'Textbox1 contains a numeric value
    Button1.Enabled = True

Else
    'Textbox1 does not contain a numeric value
    Button1.Enabled = False

End If


That code should be put in a sub that handles the Textchanged events of the textboxes.


Sidenote:
To write 'true' .Net-code, the TryParse method would probably be more correct to use...

Thanks again... Ill try that...I will research on TryParse method also... and will post some results after I tried to code it on my program
 
How do you know all this?? :) I thought IsNumeric was a leftover from the old VB, but I guess all those have been rewritten...

Anyway, I recomended IsNumeric because I didn't see that he actually wrote integers. I guess, since you write that it uses Double.TryParse internally, Double can parse an integer, but I assume an Integer.TryParse wont parse a decimal value..? I haven't checked this tho... I'm not talking about this thread especially now, but if a program accept both decimals and integers, would that mean we must check for both those values (Double and Integer TryParse)?
 
How do you know all this?? :) I thought IsNumeric was a leftover from the old VB, but I guess all those have been rewritten...

Anyway, I recomended IsNumeric because I didn't see that he actually wrote integers. I guess, since you write that it uses Double.TryParse internally, Double can parse an integer, but I assume an Integer.TryParse wont parse a decimal value..? I haven't checked this tho... I'm not talking about this thread especially now, but if a program accept both decimals and integers, would that mean we must check for both those values?
IsNumeric is a hold-over from VB6 but all those old VB6 functions are not actually executing VB6 code. They have all been rewritten in VB.NET in such a way as to provide identical behaviour to the old VB6 functions.

Double.TryParse will accept any String that represents a valid Double value. Any String that contains a representation of a valid Integer also contains a representation of a valid Double, so Double.TryParse will accept a superset of what Integer.TryParse will. You can actually specify integers only when calling Double.TryParse but that option is not available when calling IsNumeric.
 
Back
Top