How to paste text from clipboard into selected textbox using a button - c#

I am relatively new to c# but I am currently creating a Windows Form that has an editor window. I'm struggling with the Paste button though as I have 2 textbox fields, one for the title of the note and one for the note itself. I'm wanting to be able to paste from the clipboard into either textbox.
I have tried using if statements based on noteText.Focused and titleText.Focused but obviously this doesn't work as the Paste button becomes focused as soon as you click it.
Any suggestions would be of great help.

Create a local variable and save last focused textBox in it.
//subscribe both textBoxes with same GotFocus event handler
textBox1.GotFocus += textBox_GotFocus;
textBox2.GotFocus += textBox_GotFocus;
//local variable
TextBox lastSelected;
//GotFocus
private void textBox_GotFocus(object sender, EventArgs e)
{
//save last Selected textBox
lastSelected = sender as TextBox;
}
private void button1_Click_1(object sender, EventArgs e)
{
//on click get value from clipboard
if(lastSelected != null)
lastSelected.Text = Clipboard.GetText();
}

Related

Dragging over the buttons to show a word in windows form C#

I have a windows form that includes a text box and 3 buttons
The text property of the first button is C.
The text property of the second button is A
Text property of the third button is R.
Is there a way to show a car word in the text box by dragging the mouse over the previous buttons?
I tried to write the code in the MouseMove event & DragEnter event but I did not get anything.
I need to click on the first button "C" then keep pressing and dragging over the other buttons to create the word "CAR" in the textBox
You can do this using Event MouseHover
private void BtnC_MouseHover(object sender, EventArgs e)
{
TxtCar.Text += BtnC.Text;
}
private void BtnA_MouseHover(object sender, EventArgs e)
{
TxtCar.Text += BtnA.Text;
}
private void BtnR_MouseHover(object sender, EventArgs e)
{
TxtCar.Text += BtnR.Text;
}

Route multiple buttons to the same routine

I have 10 buttons, 0-9 (button0, button1, button2...). When I click any of these buttons, I would like to perform the same routine on them. I would like to know how to, upon clicking of any of these buttons, direct them to the routine below.
private void button0_Click(object sender, EventArgs e)
{
int newValue;
newValue = Convert.ToInt32(Button.text);
}
I have already gone into the properties of each button, then events, and changed the click event to button0_Click (I would have thought this would add "handles button1.click, button2.click, etc." after "private void button0_Click(object sender, EventArgs e)" but if it does that in the background, that's ok as long as it works.)
I also need to know how to identify the button that has been pressed, which is where I'm at with "Convert.ToInt32(Button.text)" (e.g. button2.text = "2").
You can select the same event handler for all the buttons in the designer (in the event tab of the properties window, select the event and there'll be a drop down with all your defined event handlers).
To get which button has been clicked on, cast the sender argument to a Button and you'll have it.
Button button = (Button)sender;
int value = int.Parse( button.Text );
Edit: Also, the "Handles control.event" syntax only exists in Visual Basic.
Edit: Check out the generated code (Form1.Designer.cs, for example) to see how the events are hooked up.
The C# language doesn't use handles to bind events (as VB does). The code for the actual binding is in the generated code for the form, i.e. in the background as you put it.
The sender property is a reference to the control where the event happened. You just need to cast it to the actual type of the control:
private void button0_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int newValue = Convert.ToInt32(button.text);
}
As an alternative to using the text of the button (for example if you want to translate the application to different languages, or simply don't want to rely on the text), you can put whatever you like in the Tag property of each button, and retrieve it in the event handler.
You could wire them all up to the same event handler an extract the button from sender e.g.
private void button0_Click(object sender, EventArgs e)
{
var button = sender as Button
if (button != null)
{
int newValue = Convert.ToInt32(Button.text);
}
}

issue with textbox event handlers

I am still learning how to do with event handler. What I want is: When I click the txtMonday to get focused, then I click the remove button to clear this selected textbox. Problem is: when I click the remove button for the selected textbox, all the unselected textboxes are clear. I only want to remove the selected textbox. How to solve this problem? Your code example much appreciated. Thanks! I am using WPF and C#.
private void btnRemoveClick(object sender, RoutedEventArgs e)
{
TextBox text = new TextBox();
text.GotFocus += new RoutedEventHandler(txtMonday_GotFocus);
txtMonday.Clear();
text.GotFocus += new RoutedEventHandler(txtTuesday_GotFocus);
txtTuesday.Clear();
}
private void txtMonday_GotFocus(object sender, RoutedEventArgs e)
{
}
private void txtTuesday_GotFocus(object sender, RoutedEventArgs e)
{
}
This should do what you want. I suggest you do some more studying about C# though, as your code shows some fundamental misunderstandings.
//you'll need a variable to store the last focused textbox.
TextBox txtLast;
public MainWindow()
{
InitializeComponent();
//add an event for all the textboxes so that you can track when one of them gets focus.
txtSunday.GotFocus += txt_GotFocus;
txtMonday.GotFocus += txt_GotFocus;
txtTuesday.GotFocus += txt_GotFocus;
txtWednesday.GotFocus += txt_GotFocus;
txtThursday.GotFocus += txt_GotFocus;
txtFriday.GotFocus += txt_GotFocus;
txtSaturday.GotFocus += txt_GotFocus;
//default to clearing sunday to avoid exception
//you could also let it clear a new TextBox(), but this is wasteful. Ideally,
//you would handle this case gracefully with an if statement, but I will leave that
//as an exercise to the reader.
txtLast = txtSunday;
}
private void txt_GotFocus(object sender, RoutedEventArgs e)
{
//whenever you click a textbox, this event gets called.
//e.source is the textbox, but since it is is just an "Object" we need to cast it to a TextBox
txtLast = e.Source as TextBox;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//this will clear the textbox which last had focus. If you click a button, the current textbox loses focus.
txtLast.Clear();
}

Problem with button when text entered into the textbox

I am trying to make the save button visible when text is entered into the text box by using the following code:
if (tbName.TextModified == true)
{
btnCTimetablesOk.Visible = true;
}
else
{
btnCTimetablesOk.Visible = false;
}
but it gives error at tbname.textmodified
is there any other way to visible the button when we enter the text in text box
this is error i am getting "the event textbox.textmodified can only appear on the left hand side of += or -="
Try using the textbox's Enter and Leave events to show/hide your button:
private void textBox1_Enter(object sender, System.EventArgs e)
{
btnCTimetablesOk.Visible = true;
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
btnCTimetablesOk.Visible = false;
}
Then modify your textbox to use these new methods.
If I'm reading your text correctly, you want the save button to be visible when the textbox has text in it and invisible when the text box is blank. If that's the case, you can use the Leave event (which occurs when the textbox loses focus) and a simple if statement:
private void textBox1_Leave(object sender, System.EventArgs e)
{
if(textBox1.Text != "")
btnCTimetablesOk.Visible = true;
else
btnCTimetablesOk.Visible = false;
}
You can also put this conditional block in any other methods kicked off by events that change the text of the box.
Also, you might want to consider using Enabled instead of Visible, it'll leave the button on the form but will gray out the text and clicking will do nothing.
I'm going to take a stab in the dark here and assume that the button is related to the textbox and you probably want someone to be able to type something in the textbox then click the button. You probably don't want the user to have to type something, then tab out or click somewhere else to make the button visible then click the button.
tbName_TextChanged(object sender, EventArgs e)
{
btnCTimetablesOk.Visible = !String.IsNullOrEmpty(tbName.Text)
}
Btw you're getting that error because TextModified isn't a boolean property, it's an event, like TextChanged or Leave or Enter. You can assign an event handler to it but you can't just check it like that.
As an aside I personally hate systems hungarian for winforms controls. I'd much rather have a timetablesOkButton than a btnCTimeablesOK button. That way if you also have a timetablesNameTextBox you can see at a glance that the button and the textbox match. Of course it may not be up to you.

Dropped down in combo after focused

I have a combo box. It must display its content, when focused and its value changed as well.
I wrote this code in its Value Change event:
if(combo1.Focused)
combo1.DroppedDown=true;
But it doesn't work!
what's your solution?
What Event handler are you putting that code in? Assuming that you want to show the drop down when the user types in the edit box part of the combo just handle the TextChanged event and put that code inside there and it should work.
If I understand your requirement correctly, when the combobox gets focus you want the drop down list to show. That can be achieved as follows
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.GotFocus += new EventHandler(comboBox1_GotFocus);
}
void comboBox1_GotFocus(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}

Categories

Resources