Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. focus on text boxes

focus on text boxes

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    treah
    wrote on last edited by
    #1

    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?

    G M 2 Replies Last reply
    0
    • T treah

      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?

      G Offline
      G Offline
      ganti r
      wrote on last edited by
      #2

      Try doing this. Give the text box a higher tab order. (greater than 0). if you have a label or something like that give that a tab order 0. I hope this solves the issue.

      rAm i Think, i Wait, i Fast -- Siddartha

      1 Reply Last reply
      0
      • T treah

        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?

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        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

        T 1 Reply Last reply
        0
        • M Martin 0

          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

          T Offline
          T Offline
          treah
          wrote on last edited by
          #4

          hi, can u provide me some chunk of code.how i use GOTFOCUS.:doh: thanks :)

          M 1 Reply Last reply
          0
          • T treah

            hi, can u provide me some chunk of code.how i use GOTFOCUS.:doh: thanks :)

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups