How to use event handlers in C# - c#

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="";
}
}
}

I think you are almost there.
Try adding this under InitializeComponent();
this.Tb_Temp.TextChanged += new System.EventHandler(this.Tb_Temp_Leave);

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.

Related

Awesomium "LoadingFrameComplete" is firing too many times

Just recently began tinkering with Awesomium, it's very cool and much better than the stock webBrowser for WinForms.
However, when I use the _LoadingFrameComplete method to determine if the page has loaded, it seems to be firing 10+ times (when used on Facebook, 2 times when navigating to google.com)
I am trying to get the comparable method of webBrowser1_DocumentCompleted (which only fires one time, after the document has completed).
Is this a 'me' problem, or am I using the wrong methods to check whether the website has finished loading completely.
I'm using Visual C# 2010 Edition
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Debugging_Problems
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string searchURL = textBox1.Text;
webControl1.Source = new Uri(searchURL);
}
private void Awesomium_Windows_Forms_WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
richTextBox1.AppendText("Completed.\n");
}
}
}
You need to use IsMainFrame
private void Awesomium_Windows_Forms_WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
if (e.IsMainFrame)
{
richTextBox1.AppendText("Completed.\n");
}
}
Try putting if(e.IsMainFrame) { .... } inside your LoadingFrameComplete event handler and only put your code in there. – Jon
That was the answer. Thank you.

c# mouse event sequence

Question... why does this code only produce a message box "Down"? I'm not getting the Up. If I block down code, the up works.
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 Mouse
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
MessageBox.Show("Up");
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show("Down");
}
}
}
Because you are showing a MessageBox
Whenever a mouse down event happened a MessageBox will popup. The MessageBox Will be in foreground and the up event will be on the MessageBox instead of the form. Thus the up event in the form doesn't fire
Just do a Console.WriteLine instead of MessageBox and it should work as expected
They will both fire if you remove the message box call. this is interferring with the mouse events because your loosing focus on the form. Instead of using message box try using...
Console.WriteLine("MouseUp");
This will display in the ouput window and not interfere with the events

How to structure speech recognition code in an executable c# WinForms App?

link to original code - please look at it first.
I'm not sure how to write this code so that it runs properly. So far my attempt:
The code in the website is using System Speech Recognition from Microsoft to record audio from microphone and turn it into text. Except, I don't know how to format that code on the website properly. The below certainly does not work. I get red underlines everywhere. I'm also not sure how 'event-handler' code is supposed to look like.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace SystemSpeechRecognition_winForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
SpeechRecognitionEngine _speechRecognitionEngine = new SpeechRecognitionEngine();
_speechRecognitionEngine.SetInputToDefaultAudioDevice();
DictationGrammar _dictationGrammar = new DictationGrammar();
_speechRecognitionEngine.LoadGrammar(_dictationGrammar);
_speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
ERROR for the four lines
This is the error:
.
"delegate System.EventHandler"
"Represents the method that will handle an event that has no event data"
"Error: No overload for 'SpeechRecognized' matches delegate 'System.EventHanndler'
_speechRecognitionEngine.SpeechRecognized -= new EventHandler(SpeechRecognized);
_speechRecognitionEngine.SpeechHypothesized -= new EventHandler(SpeechHypothesizing);
_speechRecognitionEngine.SpeechRecognized += new EventHandler(SpeechRecognized);
_speechRecognitionEngine.SpeechHypothesized += new EventHandler(SpeechHypothesizing);
}
private void SpeechHypothesizing(object sender, SpeechHypothesizedEventArgs e)
{
///real-time results from the engine
string realTimeResults = e.Result.Text;
}
private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
///final answer from the engine
string finalAnswer = e.Result.Text;
}
}
You're having compilation issues because - to take one of the events as an example - the SpeechRecognized event is of type EventHandler<SpeechRecognizedEventArgs> and you are attempting to assign it an instance of the non-generic EventHandler class.
_speechRecognitionEngine.SpeechRecognized -= new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognized);

C# Windows Forms code not working - Attach Event to button

Could someone explain the reason why the code below does not work?
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 Speaker
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("bravo you did it");
}
}
}
The window I have designed that corresponds to this code is a single window with a single button. I have the intention to do quite an extented program, but encountering problems, I decided to start with a small sample to see what's wrong, and I see neither this simple code works. Any suggestions? When I press the button1, nothing happens at all.
Make sure attached the event Click with your button. You can do it by going to designer, double click the button, it will create the event handler for you in the code. You can also attach the event handler in your Form Constructor like:
public Form1()
{
InitializeComponent();
button1.Click += button1_Click;
}
You can go to the designer, right click on Button1, click properties, Got to events and there you can attach the event handler:
You need to bind the button click to the event of button click! :)
button1.Click += button1_Click;

TextBox not showing in WinForms form

I am adding a TextBox to my form at runtime, and this is a brand new project, so this is the only code I have so far, so I am 100% positive that this is not my own doing:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
}
}
}
Why won't the TextBox display? There's nothing at all. I set breakpoints all over the place, but none of them how anything that could help me. All seems normal, but isn't.
The code is very simple, the only reason I can think of is you have some other control added before (wide enough to cover the added TextBox), try this:
private void button1_Click(object sender, EventArgs e)
{
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
box.BringToFront();
}
Also check the event handler ControlAdded, I guess the form has some code for this event handler and discard the control added if it's type of TextBox, something like this:
private void form_ControlAdded(object sender, ControlEventArgs e) {
if(e.Control is TextBox) Controls.Remove(e.Control);
}
The code that adds the textbox to the form is in button1_Click event handler. If you move it to the constructor, it will work just fine.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
}
}
}
I had a similar problem.
Looking at the code I found out that the textboxes that DO show were of type System.Windows.Forms.TextBox while those that DID NOT show were of type VisualJS.Web.TextBox. Perhaps your problem is similar.

Categories

Resources