0

I'm attempting to create a TimeCard application for employees to be able track their hours through an application instead of having to write down their hours manually. I have a decent amount of textboxes that i need to check.

First, I want to check if the textbox is clicked it will clear the value that is currently in that textbox.

Second, I want to check the textboxes again, if the user clicks out of the textbox and didn't insert any values(hours) in the textbox(blank textbox) it will automatically return the text back to 0(hours).

I am using the 'MouseClick' property to assign all these textboxes. The first part of mode code works correctly.When a user clicks on the box it clears the 0 text that was there before, but I can't figure out how to return that 0 value. Once the textbox is clicked it clears the textbox and leave it blank.I've seen ways that you can do it one at a time, but i'm trying to learn how to code efficiently. Any guidance and help on this situation would be greatly appreciated. Thank You.

Tools: C# / Visual Studio 2012 / Microsoft SQL 2012

    private void MainForm_Load(object sender, EventArgs e)
    {
     foreach (Control control1 in this.Controls)
          {
           if (control1 is TextBox)
             {
              control1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.AllTextBoxes_Click);
             }  
          }
    }

//Mouse Click Clear
     private void AllTextBoxes_Click(object sender, MouseEventArgs e)
     {
       if ((sender is TextBox))
       {
        ((TextBox)(sender)).Text = "";
       }
     }
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • I suggest to use a button to read and submit the values in the textboxes to a database (or whatever permanent storage you require), and only after data has been stored correctly, clear the textboxes. – Steve Aug 28 '14 at 14:08
  • I'm not sure to understand your requirements, but the event to check when the control loses focus is... [LostFocus](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus(v=vs.110).aspx). If you do use it, consider using [GotFocus](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.gotfocus(v=vs.110).aspx) instead of Click for a better user experience (if the user uses tab to focus your textbox uh? :p) – Kilazur Aug 28 '14 at 14:10
  • Please clarify this `Second, I want to check again if the user leaves the textbox and insert any values in for their hours it returns back to 0.` – djv Aug 28 '14 at 14:14

1 Answers1

0

If I understand your requirements, what you actually want is a Watermark/Cue banner implementation.

But since I could be monstruosly wrong, here is an implementation of what you're trying to accomplish.

private void MainForm_Load(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
        {
        if (c is TextBox)
            {
                c.GotFocus += deleteContent_GotFocus; // No need for more than that ;)
                c.LostFocus += restoreContent_LostFocus;
            }  
        }
    }

private void deleteContent_GotFocus(object sender, EventArgs e)
{
    if ((sender is TextBox))
    {
        // Using "as" instead of parenthesis cast is good practice
        (sender as TextBox).Text = String.Empty; // hey, use Microsoft's heavily verbosy stuff already! :o
    }
}

private void restoreContent_LostFocus(object sender, EventArgs e)
{
    if ((sender is TextBox))
    {
        TextBox tb = sender as TextBox;
        if (!String.IsNullOrEmpty(tb.text)) // or String.IsNullOrWhiteSpace
            tb.Text = "0";
    }
}
Community
  • 1
  • 1
Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • Thank you! I got what I needed. I just had to change f (!String.IsNullOrEmpty(tb.text)) to if (tb.Text == "") – Nick Graham Aug 28 '14 at 16:34
  • That looks weird. Basically, String.IsNullOr[Empty/WhiteSpace] does that check already; moreover, it's good practice to use myString.Equals("") instead of == "". Consider accepting my answer if it did solve your problem. – Kilazur Aug 28 '14 at 17:01