Issue outputting text to textBox (WinForms) - c#

It's my first time using winforms. I'm having some issues.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "FORM1";
textBox1.AppendText("SOME TEXT");
}
}
I've tried this. My textbox is set to fill so it fills up the whole form. I set the multi-line propery to true and dock property to fill.
From what I can try as a first time WinForms user, the Form1_Load should run as soon as the form is created. I've tried some various ways to print text, nothing works. I noticed visual studios says "0 references" next to my function. I'm not sure what this means, maybe part of my issue? Please help.

Having 0 references to method means that it's never used in code. So your issue is that you defined method, which looks like should be executed on form load event, but it's just the definition. I suggest that you copy body of your method to clipboard, go to design page of your form, on the right side, you can browse events, that form generates, find Load event, double-click it, Visual Studio should generate code with empty method definition, paste there your code.
When you right-click your method name and click Find all references you should get something like this at the bottom:

Have you checked following things are done in properties:
Go to Form1->Properties window and check for events.
Now check for Load event and it should be attached to your Form1_Load method.

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);
}
}

Visual C# button name not changing text

Day 1 coder here:
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string firstName;
}
In Visual C# Express, I changed my button name (through properties) from button1 to btnString, but as you can see it is not adapting in the code.
Did I do something wrong ?
private void button1_Click(object sender, EventArgs e)
{
}
This is the event handler, and by changing the control's name it won't automatically change the event handler name (because it was created prior to changing the control's name), you can manually do it though. You can also change it through the properties window, change the tab to "events" instead of the default selected "properties" tab
Changing the button name seemingly hasn't updated the Click event handler.
Either go to the button properties event tab, and update the button1_Click to btnString_Click and the same to the event function, or delete the button1_Click function and double click the button again, so visual studio will generate the correct handler name.
When you change the name of a control, the designer will search your code and replace instances where you have used that variable name, but it will not change the name of event handlers.
Why? What if instead of the "default name" (for a button click it would be <buttonName>_Click) you had your own custom name, like MyCoolEventHandler_Click? The designer would not know how to rename that. The same thing applies if you have coincidentally used a variable name in a totally unrelated function. Would you want it changing the name on you?
You have to do these changes manually. My best advice is to name the controls before you create event handlers. But you can always go into the Properties panel and change the links.

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