I had a form made up with buttons, textboxes, and labels. Then I decided to see if I could use tab control because I wanted to add other buttons but I didn't want them to be on the same page. So I cut all my buttons and textboxes and labels and I paste the tabcontrol. Then I paste all of the buttons and stuff back on in tab1. So before I moved on to making buttons I decided to test run the program. I got a page that came up and said nullreferenceexeption was unhandled. When I tried clicking on one of my button, I saw that there was no code inside it and it went from
private void Button1_Click(object sender, EventArgs e)
to
private void Button1_Click_1(object sender, EventArgs e)
Then the errors kept coming up. Now I just want to make my form the way it was before but even though I deleted the tabcontrol, when I click my buttons they still have the _Click_1 thing. Please tell me how I can fix this. I am new to C# so try to explain it simply.
And if you can, tell me how I can add the tabcontrol thing without messing up my entire program.
Also is there a way I could like roll back changes for a day or 2 in visual studio ultimate? Please get back as soon as possible I'm ripping my hair out I can't lose the program I've been working on for 2 weeks.
You can fix most of these problems, but it's going to take a bit of work. For each button, open the properties tab and go to the events section, find the event Click and click once in the text field. You should see a drop down arrow, click that arrow and look at the list. Find the event you expect the button to fire and click on it (Hint : it doesn't have _1 at the end)
Related
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
Let me start from a simple example to reproduce my problem:
Create WinForms project.
Drop a ToolStrip on it.
Make a ToolstripDropdownButton in it (with no children).
Add a click event-handler to the button, with something like this:
MessageBox.Show("text", "Caption");
(the point is to make the main form loose it's focus).
To make the problem more obvious, let's make a few unnecessary additions:
Set RenderMode of our ToolStip to ManagerRenderMode.
Set it's Autosize to false and make it a lil bigger.
And that's should be enough to reproduce it. Run the app and click the Button. You'll get the popup. Close the popup and voila ... the button now looks like someone is holding it pressed.
Any ideas on how to reset it to it's default state?
I've tried to call Buttons ResetBackColor, Refresh ... also as ToolStrips. Doesn't seem to work. Maybe I'm missing something?
The MessageBox or a modal form will interfere with that. The drop down is expecting children menu items.
Sample work around:
private void toolStripDropDownButton1_Click(object sender, EventArgs e) {
this.BeginInvoke(new Action(() => MessageBox.Show("Help")));
}
I started with my first WinForms project, and bumped into a small problem on the way.
I have a Form with a TabControl and some buttons outside it.
The problem is when i have a button focused and press Ctrl-Tab - nothing happens. But if you open, for instance, properties window of a file in explorer, you can cycle through tabs using Ctrl-Tab no matter which element has the focus.
So what is the right way to make such behavior? I mean, i can do Form.KeyPreview = true and write handlers myself, but is there a better way to do this?
As far as I know, the correct way is the one you mentioned yourself i.e. set Form.KeyPreview = true and write private Form1_KeyDown(object sender, KeyEventArgs e) handler to switch tabs.
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 :)
I have a C# winform app that has a tab control, I have a picturebox on tab A and would like the very same picturebox to appear on tab B. Can this be done?
Thank you
Alison
Simply add the picture box to the second tab..
tabControl1.TabPages["tabB"].Controls.Add(pictureBox1);
This will remove the picture box from tab A and put it, as is, inside tab B.
Edit: you can have this code in the SelectedIndexChanged of the tab control to have the picture box jump between the tabs:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(pictureBox1);
}
I'm affraid you can't
I though I could do it once by overriding the designer.cs file to add the control to both tabs and only got an error.
You can either make two pictureboxes, and keep them synchronized, or...
You can get the event to the tab control, OnTabChanged or something like that. If the tab is Tab A or Tab B, just move the picture box to that tab. I'm not exactly sure how you would do that, but I assume you would just remove it from whereever it is currently, and put it in the right place.