focus on text boxes
-
hi, I have developed an application.i have to provide audio help of a software. when the user put the focus on any textbox.i have to provide adiou help for that particular text box.i have written following code: private void audiobtn_Click(object sender, EventArgs e) { if (txtbox1.Focus()) { System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(); myPlayer.SoundLocation = "C:\\WINNT\\Media\\recycle.wav"; myPlayer.Play(); } if(txtbox2.Focus()) { myPlayer.SoundLocation = "C:\\WINNT\\Media\\rcle.wav"; myPlayer.Play(); } if(txtbox3.Focus()) { myPlayer.SoundLocation = "C:\\WINNT\\Media\\re.wav"; myPlayer.Play(); } } problem is that when i click button,automaticaly focus set on first textbox an it plays its given sound.:( plzzzzz help me out y this is so?
-
hi, I have developed an application.i have to provide audio help of a software. when the user put the focus on any textbox.i have to provide adiou help for that particular text box.i have written following code: private void audiobtn_Click(object sender, EventArgs e) { if (txtbox1.Focus()) { System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(); myPlayer.SoundLocation = "C:\\WINNT\\Media\\recycle.wav"; myPlayer.Play(); } if(txtbox2.Focus()) { myPlayer.SoundLocation = "C:\\WINNT\\Media\\rcle.wav"; myPlayer.Play(); } if(txtbox3.Focus()) { myPlayer.SoundLocation = "C:\\WINNT\\Media\\re.wav"; myPlayer.Play(); } } problem is that when i click button,automaticaly focus set on first textbox an it plays its given sound.:( plzzzzz help me out y this is so?
-
hi, I have developed an application.i have to provide audio help of a software. when the user put the focus on any textbox.i have to provide adiou help for that particular text box.i have written following code: private void audiobtn_Click(object sender, EventArgs e) { if (txtbox1.Focus()) { System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(); myPlayer.SoundLocation = "C:\\WINNT\\Media\\recycle.wav"; myPlayer.Play(); } if(txtbox2.Focus()) { myPlayer.SoundLocation = "C:\\WINNT\\Media\\rcle.wav"; myPlayer.Play(); } if(txtbox3.Focus()) { myPlayer.SoundLocation = "C:\\WINNT\\Media\\re.wav"; myPlayer.Play(); } } problem is that when i click button,automaticaly focus set on first textbox an it plays its given sound.:( plzzzzz help me out y this is so?
Hello, YEP! Because you are calling the method Focus which setts the focus to the control. You have to use the "Focused" property which returns a bool!
if(txtbox1.Focused)
{
...Nicer would be to implement the "GotFocus" event for you textboxes. There you can store the sender, that you know which textbox has the focus. If you then inherit you own Textbox, which provides a string property "SoundLocation", which can be set during design or runtime, you wouldn't need a if statement (or switch) at all. All the best, Martin
-
Hello, YEP! Because you are calling the method Focus which setts the focus to the control. You have to use the "Focused" property which returns a bool!
if(txtbox1.Focused)
{
...Nicer would be to implement the "GotFocus" event for you textboxes. There you can store the sender, that you know which textbox has the focus. If you then inherit you own Textbox, which provides a string property "SoundLocation", which can be set during design or runtime, you wouldn't need a if statement (or switch) at all. All the best, Martin
-
Hello, I did a little test project to show you the possibilities! Created an inherited TextBox, like suggested with an additional property:
public class TextBoxSound : System.Windows.Forms.TextBox { private string \_soundlocation = ""; \[DefaultValue("")\] public string SoundLocation { get { return \_soundlocation; } set { if(value!=\_soundlocation) { \_soundlocation = value; //Here you could fire an event which is handled from the Form. //If It is the actuall TextBox it could interrupt the Sound and start the new one. } } }
In the Forms code (where I placed the TextBoxSound instances):
public Form1() { InitializeComponent(); foreach(Control c in this.Controls) //iterating threw all the Controls on the Form { TextBoxSound tbSound = c as TextBoxSound; //casting the Control to TextBoxSound if(tbSound!=null) //If ok { //Add GotFocus Event Handler tbSound.GotFocus+=new EventHandler(tbSound\_GotFocus); } } } /// /// Evnet Handler for all TextBoxSound instances on the Form /// /// /// private void tbSound\_GotFocus(object sender, EventArgs e) { TextBoxSound tbSound = sender as TextBoxSound; //casting the sender to TextBoxSound if(tbSound!=null) //If ok, which should allways be the case { ActTextBoxSound = tbSound; } } private TextBoxSound \_actTextBoxSound = null; /// /// TextBoxSound instance which is actually focused or was Focused Last /// public TextBoxSound ActTextBoxSound { get { return \_actTextBoxSound; } set { if(value!=\_actTextBoxSound) { \_actTextBoxSound = value; //Here you could place fancy code to interrupt the actual played sound or do something else. } } } /// /// On button click /// /// /// private void bPlaySound\_Click(object sender, System.EventArgs e) { //You only have to use the ActTextBoxSound property now. if(ActTextBoxSound!=null) { myPlayer.SoundLocation = ActTextBoxSound.SoundLocation; myPlayer.Play(); } else { //Show MessageBox "No TextBox selected" } }
Hope it helps! All the best, Martin