newbie napisaĆ(a):
Not really,
Say they click on button1, it will always add the text "TEST" to the
textbox the cursor was on.
How do I know which textbox to add the word "TEST" too since there are
multiple textboxes on the form and it can be a different textbox
everytime depending on which text box they were working on.
Sonya wrote:
>newbie schrieb:
>>I want to create a form where when the users clicks on a particulat
textbox and then clicks on a button, a text is added to that textbox.
How do i go about doing this when a user selects on any textbox and
clicks on a buttont to insert text, it insert the text in the correct
textbox (the textbox that the user selected before clicking on the
button)?
Do you mean somethin like ?
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox2.SelectedText;
}
In your form declare programmaticaly TextBox (without declaring any
additional information like position, size, text etc.) and for all of
you textboxes add *the same* event on TextBoxClick and the same event on
ButtonClick. Here is the code.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
namespace test
{
public partial class Form1 : Form
{
private TextBox lastSelectedTextBox;
public Form1()
{
InitializeComponent();
foreach(Control c in this.Controls)
{
if(c is TextBox)
((TextBox) c).Click +=new EventHandler(TextBoxClick);
else if (c is Button)
((Button) c).Click +=new EventHandler(ButtonClick);
}
}
private void TextBoxClick(object sender, EventArgs e)
{
lastSelectedTextBox = (TextBox) sender;
}
private void ButtonClick(object sender, EventArgs e)
{
label1.Text = lastSelectedTextBox.Text + " from " +
((Button)sender).Text;
}
}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -