Kyle

Kyle

  • NA
  • 6
  • 0

Novice Question--How do I put text into the form titlebar?

Jun 17 2007 9:42 AM

Hi. I am a very new C#/.NET programmer. After taking some tutorials on C# from www.csharp-station.com and a Windows forms tutorial on www.csharphelp.com, I decided to see what I could come up with on my own. I wrote a simple calculator. Obviously it's pointless to have because Windows already has one, but it was a good learning experience. Anyway, everything went well, but I can't seem to find how to put a title into the titlebar of the form. I've done some web searching and the few results I've gotten show something like this in the Main section after all the other code:

public static void Main()

{

        Form form = new Form();

        form.Text = "This is C#";

        form.Show();

        Application.Run();

}

However, the problem here is that I don't declare my form in the Main section.  I declare it at the very beginning of the program, like this:

public class SimpleCalculator:Form
{
 
 private Label lblResult;
 
 private Button btn1;
 private Button btn2;
 private Button btn3;
 private Button btn4;

-----------
And so on and so forth there are many other buttons declared, eventually we get here:
-----------
       public SimpleCalculator()
       {         
                //Window initialization
  
  ClientSize = new System.Drawing.Size(240,350);
  StartPosition = FormStartPosition.CenterScreen;
  
  //Label initialization
  lblResult = new Label();
  lblResult.Size = new Size (190,30);
  lblResult.Location = new Point(20,20);
  lblResult.Text = " ";
  Controls.Add(lblResult);
  
  //Button initialization
  handler = new EventHandler(CalculatorFunc);

----------
A lot of initialization stuff follows, afterwards we get to the event handler which I will not post because it is very long.  Eventually we get down to the Main section:
----------

 public static void Main()
 {
  Application.EnableVisualStyles();
  //Enables XP/Vista visual styles
  Application.Run(new SimpleCalculator());
  //Runs all the above code.
 }

---------



I have tried simply putting form.Text="Title" just above both "Application" functions, but then I get told this by the compiler: "The name 'form' does not exist in the current context." I know this is a simple error... I tried the name of the program in place of "form," like this:

SimpleCalculator.Text="Title" but that doesn't compile either. I feel rather stupid because setting the form title seems like it should be simple. What do I need to do to make it work? I did not post all the code of the program because I did not want to waste a lot of space.

Edit: There... That's better.  Geeze, how embarrassing that my text appeared without breaks like that, code and all.  Alan, you are one nice guy to actually go through and dissect all that.


Answers (2)