Replacing the content of textBox1 with the contents of textBox2 - c#

I'm quite sure I'm missing some sort of really obvious solution to this. It might be my state of rank newbie, but I am completely lost at this point.
Essentially, I need to create a WinForm that will allow the user to input text in textBox1 and textBox2. Then, at the click of button1, the text in textBox2 replaces that in textBox1. From what I can tell, this needs to go into the button1_Click event.
Here's a few examples of what I've tried so far:
private void button1_Click(object sender, EventArgs e)
{
string output;
output = textBox2.Text;
textBox1.Text = output;
}
And the simplest solution I could find:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox2.Text;
}
As well as:
this.textBox1.Text = this.textBox2.Text;
None of these work. I can enter the text just fine in either textbox, but nothing happens when I actually click the button. My mother would turn me over her knee if she could hear my response to that lack of... well, response.
Like I said, I'm sure I'm missing something really obvious. But I've now consulted my textbook, Google, Bing and at least a half-dozen forums with no luck in finding anything that has let me solve this on my own. Could someone be kind enough to tell me what it is I'm doing wrong?

It sounds like your event handler is not wired in.
Go to your form designer and click on Button1, then look at the Click event and see if it says button1_Click. (The list of events is in the Properties window. Click the yellow lightning bold icon to view the events).
Alternatively, if you want to just check that the event handler is wired up properly you can open (assuming your form is called MyForm) MyForm.Designer.cs and look for this line:
this.Button1.Click += new System.EventHandler(this.button1_Click);
or something real close to that.

Did you hook the event properly? I'm betting your button1_Click doesn't get called. Put a breakpoint in it to make sure.

Related

c# how do i detect when a user clicks anywhere inside a form

I need to have something happen when a user clicks anywhere inside the form. I don't know much about c# and I have tried looking it up but I just can't find an answer to this exact question.
try this
1 --> go to form properties.
2 --> find Click event.
3 --> double Click it.
code here
There's things called events
so when the event is happen the code will work
for ex:
Click : when I 'click', the code will happen and that's what are you looking for
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("hello world!");
}
All you have to do is select the event you want from the properties tab and double click on it and write the code you want

When combobox changes do something ( C# )

I've been trying to make a program with 3 comboboxes where depending on what you pick different things happen.
Here is a screenshot of what I'm stuck with.
The only thing missing in the screenshot is the following which is in the private void Form1_Load event
cBxColor1.Items.Add("Black");
cBxColor2.Items.Add("Black");
cBxTest.Items.Add("Something");
In the screenshot above I try two methods to write something in the textbox. One whenever the text changes and then checks for the choosen item. In this case Something, Black and Black. I'm planning on adding more later but so far I'm trying to get this to work with one.
The original plan was to have while(the selected texts in the comboboxes are Something, Black and Black) then add some text to the textbox if that is true.
Screenshot of the error I get when trying the other method, I'm not sure what this means.
I've googled and searched around for a solution but I truly couldn't find anything that would help to solve my problem. I would appreciate if the 1337 hax0rz on here would help me out.
TextChanged is a Event. Use it in a methode like this:
private void ComboBox_TextUpdate(Object sender, EventArgs e)
{
//Your code here
MessageBox.Show("You are in the ComboBox.TextUpdate event.");
}
Add the event with += to your combobox in your initialisation:
ComboBox.TextUpdate += ComboBox_TextUpdate;
So at every TextUpdate your Methode ComboBox_TextUpdate gets called and you can code there.
Instead of using the if condition to see if the text has changed, you should use the ComboBox event SelectedValueChanged.
To create that event right-click on your ComboBox and select properties. Select "Events" and double-click the textbox next to the SelectedValueChanged event.
Then you want to check the values of each ComboBox like you did.
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (cBxColor1.SelectedText.Equals("Black") || cBxColor2.SelectedText.Equals("Black") || cBxTest.SelectedText.Equals("Something"))
{
tbxTest.Text = "TEST";
}
}
Also, that while statement is almost a death threat because once it enters that condition, it will not leave.
You won't be able to change the ComboBox value due to the while being executed.

Control is not getting focused on Keyup event

Hi all I have created a dynamic combo box with a Textbox and a button to appear as dropdown style, every thing works fine but I handled keyup event for the textbox so that when user enter some text I will search for the results and display them
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
//Some code to filter my data
textBox1.Focus();
}
But I am unable to set the focus Immediately back to the textbox after results getting displayed so can some one help me
Code I used is from here
http://www.planetsourcecode.com/vb/scripts/showcode.asp?txtCodeId=8554&lngWid=10
I have found that the Focus() method is a bit flaky.
Other options:
textBox1.Select(textBox1.Text.Length - button1, 1);
...or simply:
textBox1.Select();
If you can verify that something else is going wrong, then this might be off base, otherwise you might simply be fighting weirdness.

Execute method On-Load of an object on a form in C#

E.g. instead of having a button to initiate the method, the method automatically happens without any user interaction - automatically.
private void button13_Click(object sender, EventArgs e)
{
try
{
ServiceController sc = new ServiceController();
sc.ServiceName = "Spooler";
if (sc.Status.ToString().ToLower() == "stopped")
{
serviceStatusLabel.Text = "Installed but stopped";
}
if (sc.Status.ToString().ToLower() == "running")
{
serviceStatusLabel.Text = "Installed and started";
}
}
catch
{
serviceStatusLabel.Text = "Service not installed";
}
}
I just want the Label object to show the service status when the form is loaded up, without using a button
EDIT: Given your comment, are you actually after the Form.Load event? It sounds like it. Any event handlers subscribed to that event will be executed "when the form is displayed for the first time".
(The confusing thing is that your title talks about "On-Load" of an object whereas it sounds like you really want the method to be called when the form is loaded.)
It's not really clear what you mean by "when its output on the form" but you might want to look at the TextChanged and VisibleChanged events. That's if you want something to happen when the label is altered.
If you're looking for when the service status is altered, it doesn't look like there's an event raised for that, I'm afraid. Note that it would be much cleaner to switch on the enum value rather than to convert it to a string, lower it, and then compare that with hard-coded constants.
... Do I get your question correctly?
You want a piece of code to be executed when an object or the form is loaded?
Well that's easy :p
Click on your object (or form) in the designer, in the properties dock, click the lightning bolt icon, go to the Load or Show event, and double-click the box.
A new piece of code should now be created in the code view, something like this:
private void Object_Load(blabla) handles Object.Load
{
}
Whatever code is in that event will be executed when the object is loaded or shown.
If you create a handler for the Load event, it will run when the form gets loaded.

Do I have to define an event in this case?

I've got a form with several controls (to make things simple, say it's a couple of textboxes), which I need to be updated by clicking on two buttons - a Forward button and a Backwards button.
Now, the general idea is that the information to be displayed is stored in some kind of array of objects, and is shown according to the value of some counter. That is, a user clicks on the Forward button -> the counter is incremented -> the corresponding array item is shown on the form. Same goes for the Backwards button.
So, the question is - should I define any specific event in this case? Or is it sufficient to use the standard
private void button1_Click(Object sender, EventArgs e) {}
event which is provided when double-clicking on a button control? I mean, what would be the right thing to do?
I think it's a pretty dumb question, still I appreciate your advice here, thanks in advance!
The click event is an ok place for that logic, however, it's a good practice to extract that forward/backward logic in a separate method (maybe you'll want to go forward by pressing the right arrow?) to something like this:
private void btnForward_Click(Object sender, EventArgs e)
{
GoForward();
}
private void GoForward()
{
// the forwarding code here
}
And, make a habit of naming controls as early as possible, because VS uses the control name to name the event handler, and button1_Click is not very descriptive :)

Categories