Programmatically display text

F

Femi

Hello,

I'm trying to programmatically add text to textbox control to no avail.

I created a method under form1{

public void DisplayText(){
textbox1.text = "Test";
}

In the main method in program.cs I created a new instance of form1 frmNew. I
then call the displaytext method [frmnew.DisplayText();] however no text is
displayed.

Can anyone point me in the right direction?

Regards

Femi
 
J

Jon Skeet [C# MVP]

Femi said:
I'm trying to programmatically add text to textbox control to no avail.

I created a method under form1{

public void DisplayText(){
textbox1.text = "Test";
}

In the main method in program.cs I created a new instance of form1 frmNew. I
then call the displaytext method [frmnew.DisplayText();] however no text is
displayed.

Can anyone point me in the right direction?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

You need to actually *show* the form (e.g. with Application.Run) - it's
not clear whether or not that's the problem though.
 
F

Femi

Thank you for replying to my post, below is the content of the program.cs
file:


using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace MPLLoginscreen
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Commented out the Application.Run line 'cause I couldn't see a
way
//to use the text property of textbox1 or call a Form1 method
//Application.Run(new Form1());
Form1 frmNew = new Form1();
Application.Run(frmNew);
frmNew.displaytext();
}
}
}

and here is the content of the Form1.cs file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MPLLoginscreen
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void displaytext()
{
textBox1.Text = "test";
}
}
}
Basically when the application runs I want the text 'test' to be displayed
in the textbox.
Thank you for your help.

Jon Skeet said:
Femi said:
I'm trying to programmatically add text to textbox control to no avail.

I created a method under form1{

public void DisplayText(){
textbox1.text = "Test";
}

In the main method in program.cs I created a new instance of form1
frmNew. I
then call the displaytext method [frmnew.DisplayText();] however no text
is
displayed.

Can anyone point me in the right direction?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

You need to actually *show* the form (e.g. with Application.Run) - it's
not clear whether or not that's the problem though.
 
J

Jon Skeet [C# MVP]

Femi said:
Thank you for replying to my post, below is the content of the program.cs
file:

Form1 frmNew = new Form1();
Application.Run(frmNew);
frmNew.displaytext();

This is the problem. Application.Run doesn't return until the form has
been closed - so your call to displaytext doesn't occur until it's far
too late.

If you want the method to be called when the form has loaded, you could
add a handler for the Load event.
 
F

Femi

Thank you, however I want to able to display text as and when.

I'm still perservering.
 
J

Jon Skeet [C# MVP]

Femi said:
Thank you, however I want to able to display text as and when.

Calling the method whenever you need to *while the form is displayed*
should work fine - it's just that with your original code you're
waiting until the form has been closed.
 

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