-4

I created a form with just 2 textboxes and a button. In the first one I type a temperature in Fahrenheit and when I press the button "Convert", the program calculates and puts the temperature in Celsius in the other TextBox. It's working fine.

Now I want the program to clear the second TextBox when I start typing in the first TextBox. Below I show just a part of the code, which didn't work. Can anybody help me?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Conv_Temp
{
   public partial class Frm_Principal : Form
   {
      public Frm_Principal()
      {
          InitializeComponent();
      }

      public event EventHandler Leave;

      private void Tb_Temp_Leave(object sender, EventArgs e)
      {
          MessageBox.Show("Leaving TB Tb_Temp");
          Tb_Result.Text="";
      }

   }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
Ismael
  • 11
  • 1
  • 4
  • Add a handler to the `KeyPressed` or `KeyDown` event of the text box. – D Stanley Oct 10 '16 at 21:26
  • 1
    why don't you try double clicking on the `Enter` Event of the first box, then from there you can do a simple check to see if the second TextBox.Length > 0 then clear the textbox of the second one and continue.. what seems to be the issue..this is not that that difficult – MethodMan Oct 10 '16 at 21:29
  • Possible duplicate of [Understanding events and event handlers in C#](http://stackoverflow.com/questions/803242/understanding-events-and-event-handlers-in-c-sharp) – Dante Oct 10 '16 at 21:51

2 Answers2

1

I think you are almost there.

Try adding this under InitializeComponent();

this.Tb_Temp.TextChanged += new System.EventHandler(this.Tb_Temp_Leave);

Nathan Kovac
  • 231
  • 2
  • 4
  • Rajput. Thanks for your sugestion. I think my code is better now. – Ismael Oct 10 '16 at 23:49
  • Nathan. Your sugestion worked fine. Thanks a lot. :-) – Ismael Oct 10 '16 at 23:49
  • @Ismael you should click the little checkmark next to an answer if you use it to solve your problem. It gives reputation points to the person who wrote the answer, incentivizes further contribution, and helps people know whether or not your problem was solved without needing to read through all the comments. – Lodestone6 Oct 11 '16 at 02:44
0

Add new Event handler in your form designer code.

this.textBox1.TextChanged += new System.EventHandler(this.ModifyTextBox1);

and implement this event in above form.cs file(Form_Principal )

private void ModifyTextBox1(object sender, EventArgs e) { textBox2.Text = String.Empty; }

Please follow good convention for writing codes this is just a demo.

Rajput
  • 2,597
  • 16
  • 29