Label text display

G

Guest

How come when I put the label text setting here:

/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void LoadInfo()
{
label1.Text = DateTime.Now.ToString();
}

The form comes up but no text is displayed?

If I put the label text setting here:

namespace Viewer1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
label1.Text = DateTime.Now.ToString();

Then it works fine.

What do I need to learn?

Bill
 
A

Alan Pretre

BillZondlo said:
If I put the label text setting here:
InitializeComponent();
label1.Text = DateTime.Now.ToString();

Then it works fine.

What do I need to learn?

Hi. If you open up the region that VS.NET creates for you called "Windows
Form Designer generated code", then you will see all of the initialization
of the controls that needs to take place BEFORE your loading of the text.
InitializeComponent() in your Form contstructor will do this needed
initialization. You need to do your own property setting of the controls
AFTER InitializeComponent().

-- Alan
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


I recommend you two places where to put your controls customizations:

1- Right after the InitializateComponent () call in the constructor
2- the Form_Load event.

DO NOT MODIFY THE InitializateComponent () method, never touch it.

Pd:
In your first example when you call LoadInfo() ?

Cheers,
 
G

Guest

I understand what you say. I thought the same.
But, in an earlier program that I wrote, I have set labels to text values
right after the MAIN () {Application.Run(new form1())}

i.e.

[STAThread]
static void Main(string[] args)
{
Application.Run(new Form1());
}
private string folderName1, fileName1 ;

public void button1_Click(object sender, System.EventArgs e) //open folder
selection.
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result == DialogResult.OK ) //folder selected
{
folderName1 = folderBrowserDialog1.SelectedPath;
label1.Text = folderName1 ;

Or is this different in some way?

TIA

Bill
 
R

Rick Lones

BillZondlo said:
How come when I put the label text setting here:

/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void LoadInfo()
{
label1.Text = DateTime.Now.ToString();
}

The form comes up but no text is displayed?

If I put the label text setting here:

namespace Viewer1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
label1.Text = DateTime.Now.ToString();

Then it works fine.

What do I need to learn?

In the first example your function LoadInfo() would do what you want - but it is
never called! In the second your label initialization is done in the form's
constructor, which does get called when the app starts. A combination which
would also work would be:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
LoadInfo();
. . . etc.

HTH,
-rick-
 
G

Guest

Thank you for that information.

Good advice.

Bill


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


I recommend you two places where to put your controls customizations:

1- Right after the InitializateComponent () call in the constructor
2- the Form_Load event.

DO NOT MODIFY THE InitializateComponent () method, never touch it.

Pd:
In your first example when you call LoadInfo() ?

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

BillZondlo said:
How come when I put the label text setting here:

/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void LoadInfo()
{
label1.Text = DateTime.Now.ToString();
}

The form comes up but no text is displayed?

If I put the label text setting here:

namespace Viewer1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
label1.Text = DateTime.Now.ToString();

Then it works fine.

What do I need to learn?

Bill
 
G

Guest

Thank you.

What you added helped me understand more of what Ignacio said.

Now I'll know to call my methods. :)

I'm not real clear on my second example but I assume the button_click in the
"windows form designer generated code" does the calling after initialization.

Bill

Rick Lones said:
BillZondlo said:
How come when I put the label text setting here:

/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void LoadInfo()
{
label1.Text = DateTime.Now.ToString();
}

The form comes up but no text is displayed?

If I put the label text setting here:

namespace Viewer1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
label1.Text = DateTime.Now.ToString();

Then it works fine.

What do I need to learn?

In the first example your function LoadInfo() would do what you want - but it is
never called! In the second your label initialization is done in the form's
constructor, which does get called when the app starts. A combination which
would also work would be:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
LoadInfo();
. . . etc.

HTH,
-rick-
 
A

Alan Pretre

BillZondlo said:
Or is this different in some way?

Yes, this snippet that you show is setting the label text in a button click
event handler. The InitializeComponent() had already been called earlier in
your Form1 constructor. (The constructor is called in the Run(new
Form1())).

-- Alan
 
G

Guest

In your second example, the button_click is an event handler, and so is
called when a button is clicked on the form ...

you will have the following in the code for your second example ...

public class Form1 : System.Windows.Forms.Form
{
private Button button1;
/* other declarations omitted for berevity */

private void InitializeComponent()
{
button1 = new System.Windows.Forms.Button();
button1.Click += new System.EventHandler(this.button1_clicked);
/* other initialisations omitted for berevity */
}
/* other members omitted for berevity */
}

BillZondlo said:
Thank you.

What you added helped me understand more of what Ignacio said.

Now I'll know to call my methods. :)

I'm not real clear on my second example but I assume the button_click in the
"windows form designer generated code" does the calling after initialization.

Bill

Rick Lones said:
BillZondlo said:
How come when I put the label text setting here:

/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void LoadInfo()
{
label1.Text = DateTime.Now.ToString();
}

The form comes up but no text is displayed?

If I put the label text setting here:

namespace Viewer1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
label1.Text = DateTime.Now.ToString();

Then it works fine.

What do I need to learn?

In the first example your function LoadInfo() would do what you want - but it is
never called! In the second your label initialization is done in the form's
constructor, which does get called when the app starts. A combination which
would also work would be:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
LoadInfo();
. . . etc.

HTH,
-rick-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top