Windows Develop Bookmark and Share   
 index > Windows Forms General > c# Insert Text into Textbox
 

c# Insert Text into Textbox

I have a series of textboxes on a Windows Form and a Series of buttons aswell. What I am trying to achieve sounds quite simple but I cannot figure out how to do it. Basically all Im trying to achieve is each button has some predefined text associated with it and when the user clicks the button, it inserts that text into the textbox which has the focus. The problem with this is that when you click on the button you lose the focus from the Textbox. The only way I have figured out is to save which control has focus before the button is clicked. This works but when you have 32 textboxes on a form, surely there is a better way than creating an On_Enter Method or similar to set the value of the Control which needs the text.

Any help would be muchly appreciated

Daniel Kelly  Monday, August 14, 2006 12:41 AM

Daniel Kelly wrote:
This works but when you have 32 textboxes on a form, surely there is a better way than creating an On_Enter Method or similar to set the value of the Control which needs the text.

Off hand I can't think of an existing data item that tells you the previous control that had focus. What is it about using the On_Enter method to set a reference that you don't like? That is, how would we measure 'better'?

Frank Boyne  Monday, August 14, 2006 2:19 AM
Saving the reference is one way, but while investigating the matter using Spy++, I saw the line "WM_SETFOCUS hwndLoseFocus:000B0764".  So I looked in MSDN in the Platform SDK documentation, which stated that the wParam of WM_SETFOCUS gives the window handle of the last selected control.  So the following subclassed control should work exactly how you want it to.  Note that the Tag property determines what text to add.  Let me know if you need help using this.

    public class TextWriterButton : System.Windows.Forms.Button
    {
        private TextBox lastTextBox = null;

        protected override void WndProc(ref Message m)
        {
            const int WM_SETFOCUS = 0x0007;
            if (m.Msg == WM_SETFOCUS)
            {
                IntPtr handle = m.WParam;
                if (handle.ToInt32() != 0)
                {
                    Control lastSelected = Control.FromHandle(handle);
                    if (lastSelected is TextBox)
                        lastTextBox = (TextBox)lastSelected;
                    else
                        lastTextBox = null;
                }
            }
            base.WndProc (ref m);
        }

        protected override void OnClick(EventArgs e)
        {
            if (lastTextBox != null)
                lastTextBox.Text += this.Tag;
            base.OnClick (e);
        }
    }

Hope it helps.
sirjis  Monday, August 14, 2006 3:50 AM
Since you may have more controls on the page why don't you handle the GetFocus event on the TextBoxes to set a field that contains the 'active' text box, then when the button is pressed you use the field instead of trying to find the textbox.
Steve Mitcham  Monday, August 14, 2006 12:13 PM
That was the way I thought of doing it but with 30 or so text boxes, it creates 30 events. I wasnt sure if this was the "correct way to do it". I thought there may have been another way.
Daniel Kelly  Tuesday, August 15, 2006 1:49 AM

Hi,

I think its very simple( I think). But you have to do a little trick. You said you have 32 text boxes on the form and some buttons. Now you want the text be inserted in the txt boxes. You just make an array of 32 textboxes and initialize them with your 32 textboxes. Also set the tag property of each textbox with an index value that it appears on the form. You need to initialize the array of text boxes in the same order. Now create a singleOnFocus event handler for all the textboxes. Also create an a variable index of type int. In the event handler, write the following code:

public void OnFocus(Object sender,EventArgs e)

{

index=int.Parse(((TextBox)sender).Tag);

}

Now create the handler for Click event of the buttons. (Remember that you can create single event handler for the buttons if you the associated text is placed in the Tag property. In that case, retrieve that text from the tag property.

public void OnClick(Object sender,EventArgs e)

{

TextBox txt=ArrayOfTextBoxes[index];

txt.Text=<Associated text with the currently clickedbutton>;

}

wasif2  Tuesday, August 15, 2006 7:04 AM
would you even need the array? could you not just note the focused text box as a private member?
koder monkey  Tuesday, August 15, 2006 2:47 PM

You'r right, the array isn't really needed as a private member will suffice.


private TextBox theInsertTarget = null;

private void textBox_Enter (object sender, EventArgs e)
{
theInsertTarget = sender
as TextBox;
}

Use the design window to wire-up the Enter event (or the leave event) for each textbox to this generic Enter event handler and theInsertTarget will always point to the last textbox to fire the event, or at least the last textbox that has this event handler wired up tofire the event.

Frank Boyne  Tuesday, August 15, 2006 3:37 PM

You can use google to search for other answers

Custom Search

More Threads

• Verdana DrawString() problem
• Looping through controls
• Handling both mouse buttons being pressed
• how to control a javascript webpage on the C# ?
• hide the command window if a form is launched from a console application
• Largest Sever 2003 Extended Partition?
• Sending Email when Exception
• ICorRuntimehost, start method is failing
• Font is Combobox text?
• aaa DataGridView: Can't hide Cell highlight border