Click listener not firing - c#

I'm not able to get a basic click listener to work.
I've created a base button with the (Name) of button1.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("button1 was clicked");
}
When I run the application and click my button it doesn't fire. I'm struggling to figure this out because I'm following the guide outlined here: http://msdn.microsoft.com/en-us/library/dfty2w4e.aspx

It appears you're missing this line button1.Click += new EventHandler(button1_Click);, you should add this in the forms constructor.
If you go through the UI designer you can just drag the button onto the UI, then double click it and it will create the listener for you; everything you have there, plus the line above in the designer file, minus the MessageBox.Show(). This is the route I would recommend for creating all of your standard listeners.

If you click on the button on your form in the designer and view properties and select the events section, do you see a value next to the event "Click"? You should see the value "button1_click". If you don't see this, then that is your problem. You should be able to click the dropdown and select the button1_click event.

Related

Access a ListView located in a UserControl from the MainForm in Winforms

I have an app that uses a side menu, and for each button (there are 3) on the left side menu, it changes the pages shown.
I tried doing it with multiple panels, but it's a nightmare to maintain in designer, and it's probably not a very good programming habit, I expect.
So I search and found what seemed to be a great idea: UserControl.
But as usual, it's not that simple (for a badly self-taught guy like me)
The general flow of the program is as follows:
a Btn_uc1_Check button that gathers informations and displays them in a uc1_ListView,
a Btn_uc2_Seek button that gathers informations on the net based on the uc1_ListView , and displays them on uc2_ListView,
a Btn_uc3_compile that compiles the info from uc2_ListView into a file,
a Clear button that clears the ListView depending on the UserControl on screen.
Now to the problem:
How on earth do I gain access to a ListView located in a UserControl to be able to read, clear, and add items from the MainFrom or from another UserControl?
I searched and honestly found nothing corresponding to what I needed?
Quite many questions.
You can gain access to any controls in UC. Just change the property "Modifiers" of the ListView in your UC to "Public".
Set that method to public. Do not use keyword "static". Each control
in your form is an instance of a class, not a static class actually. In the main form, create a button and double click on it in VS designer. A method will automatically generated, something like private void button1_Click. When the button is clicked, all of the code lines in button1_Click will run.
Create a public event handler of your user control, then pass the method in main to the handler.
So the UC class will be similar to this:
public event EventHandler button_UC_Click_handler;
public UserControl1()
{
InitializeComponent();
}
private void button_UC_Click(object sender, EventArgs e)
{
button_UC_Click_handler.Invoke(sender, e);
}
In main form:
public MainForm()
{
InitializeComponent();
userControl11.button_UC_Click_handler += UserControl11_button_UC_Click_handler;
}
private void UserControl11_button_UC_Click_handler(object sender, EventArgs e)
{
MessageBox.Show("You have clicked it!");
}
Good luck!

Computer name program, visual C#

I'm very new to Visual C#
I've already made a calculator, following a tutorial.
Now, I'm trying to make a simple program, when run will bring up a box that shows what the computer name is. I have an idea on how to do it, but I'm still learning the C# syntax.
I have made the box that pops up and it says, "This Computer's Name is:"
then I made a blank label, and I want to be able to change the label to the computer name using System.Environment.MachineName
Here is what I have:
private void name_TextChanged(object sender, EventArgs e)
{
name.Text = System.Environment.MachineName;
//name being the name of the label
}
The labels text change event doesn't fire until something changes it's text.
Instead use the Forms Load event to do it, then use that line of code you have.
Firstly you want to create a form load event handler, you do this by double clicking on any blank space in the form, it will take you to the code window that is where you will want to write the following statement.
The form load event is executed before the form is displayed on screen, therefore your machine name will be displayed as soon as the window is visible.
Lets assume your form is called Form1
this is what your code will look like :
namespace Machine_Name
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// This is the form load event handler
private void Form1_Load(object sender, EventArgs e)
{
yourLabel.Text = Environment.MachineName;
}
}
}

C# - Method is not being called upon .Load or .Shown [duplicate]

I have a Windows Forms form where I am trying to show a user control when the form loads. Unfortunately, it is not showing anything. What am I doing wrong? Please see the code below:
AdministrationView wel = new AdministrationView();
public ProgramViwer()
{
InitializeComponent();
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
Please note I added the load event based on what I read in this article:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx
Three ways you can do this - from the form designer, select the form, and where you normally see the list of properties, just above it there should be a little lightning symbol - this shows you all the events of the form. Find the form load event in the list, and you should be able to pick ProgramViwer_Load from the dropdown.
A second way to do it is programmatically - somewhere (constructor maybe) you'd need to add it, something like: ProgramViwer.Load += new EventHandler(ProgramViwer_Load);
A third way using the designer (probably the quickest) - when you create a new form, double click on the middle of it on it in design mode. It'll create a Form load event for you, hook it in, and take you to the event handler code. Then you can just add your two lines and you're good to go!
You got half of the answer! Now that you created the event handler, you need to hook it to the form so that it actually gets called when the form is loading. You can achieve that by doing the following:
public class ProgramViwer : Form{
public ProgramViwer()
{
InitializeComponent();
Load += new EventHandler(ProgramViwer_Load);
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
}

Form.Closing not listed as event for form?

I'm using Visual Studio 2010; usually when I want to add events other than mouse clicks I go to to the events tab and find the event, click it, and it takes me to that spot in code.
However, I can't find any for the Closing event of the form.
I've tried manually adding it in but I think there's more behind the scenes stuff that VS is taking care of:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
What's the most effective way for having everything 'written' for the FormClosing event? Or how do I enter it all manually if I must?
This is form WinForms.
It's called FormClosing not Closing. You can add the event by going to the properties window and clicking on the "Events" button then double-clicking on the "FormClosing" item:
You're almost there with what you have. What you're missing is the code-behind that wires up the event to your handler, which is automatically generated for you when you follow the above procedure. Here's what it looks like in the InitializeComponent method in the "Form1.Designer.cs" file (click the "Show All Files" button on the top of the Solution Explorer):
this.FormClosing +=
new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
If you add the method Form1_FormClosing manually, You should add the following line of code in the constructor :
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
For the form close event:
Click on the form -> Properties -> Events -> Form Closing

How to add a form load event (currently not working)

I have a Windows Forms form where I am trying to show a user control when the form loads. Unfortunately, it is not showing anything. What am I doing wrong? Please see the code below:
AdministrationView wel = new AdministrationView();
public ProgramViwer()
{
InitializeComponent();
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
Please note I added the load event based on what I read in this article:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx
Three ways you can do this - from the form designer, select the form, and where you normally see the list of properties, just above it there should be a little lightning symbol - this shows you all the events of the form. Find the form load event in the list, and you should be able to pick ProgramViwer_Load from the dropdown.
A second way to do it is programmatically - somewhere (constructor maybe) you'd need to add it, something like: ProgramViwer.Load += new EventHandler(ProgramViwer_Load);
A third way using the designer (probably the quickest) - when you create a new form, double click on the middle of it on it in design mode. It'll create a Form load event for you, hook it in, and take you to the event handler code. Then you can just add your two lines and you're good to go!
You got half of the answer! Now that you created the event handler, you need to hook it to the form so that it actually gets called when the form is loading. You can achieve that by doing the following:
public class ProgramViwer : Form{
public ProgramViwer()
{
InitializeComponent();
Load += new EventHandler(ProgramViwer_Load);
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
}

Categories