Jump to content
  • Advertisement
Sign in to follow this  
jwbowyer

Outdated code, or am I doing something wrong

This topic is 3735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I started to work through the tutorial posted at this link which has to do with game development in C sharp. It starts out with the all knowing Hello World program in a windows form. I created the window just fine, but after that nothing he posted worked. I didn't get any errors or anything of the sort, it just didn't show up in the form window. Am I doing something wrong or is it outdated? The code goes through the debugger fine? I even copied his code directly when nothing seemed to work and there was no change. Code is below
[source="c sharp"]
using System;
using System.Windows.Forms;
using System.Drawing;

namespace Tutorials.UsingAForm
{
    public class AForm : Form
    {
        static void Main()
        {
            Application.Run(new AForm());
        }

        //everything below here does not work
        public AForm()
        {
            //Is supposed to give the window a title
            Text = "A Brand New Window";
        }
        //is supposed to paint the Hello World string at the top left corner
        protected override void OnPaint(PaintEventArgs pea)
        {
            pea.Graphics.DrawString("Hello World", this.Font,
                     Brushes.Black, 0, 0);
        }
    }
}

Share this post


Link to post
Share on other sites
Advertisement
that should work try this

click the form goto the properties pannel

at the top you should see a lightning symbol click it

move down till you find Paint and select
double click it

copy the code from the function into that

change pea to e.Graphics.DrawString("Hello World", this.Font,
Brushes.Black, 0, 0);

The form is not black is it

Share this post


Link to post
Share on other sites
Dang, I guess that tutorial is outdated as it had none of those directions or all the code that now fills my screen.

Anyhow, I tried to do what you said and came up with this source code:

[source="C#"]
using System;
using System.Windows.Forms;
using System.Drawing;

namespace Tutorials.UsingAForm
{
public class AForm : Form
{
static void Main()
{
Application.Run(new AForm());
}

//everything below here does not work
public AForm()
{
//Is supposed to give the window a title
Text = "A Brand New Window";
}
//is supposed to paint the Hello World string at the top left corner

private void InitializeComponent()
{
this.SuspendLayout();
//
// AForm
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "AForm";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.AForm_Paint);
this.ResumeLayout(false);
}

private void AForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString("Hello World", this.Font,
Brushes.Black, 0, 0);
}
}
}



Apparently it still does not work. Nothing shows up in my windows form. =/

Share this post


Link to post
Share on other sites
Move the Paint += ... to the ctor instead:

//everything below here does not work
public AForm()
{
//Is supposed to give the window a title
Text = "A Brand New Window";

this.Paint += new System.Windows.Forms.PaintEventHandler(this.AForm_Paint);
}




EDIT: the problem is that InitializeComponent never gets called, and therefore the Paint-eventhandler never gets registered.

[Edited by - NisseBosseLasse on July 1, 2008 9:54:50 AM]

Share this post


Link to post
Share on other sites
Well that makes sense... somewhat... Ok, I have no clue what I'm doing. Thanks for the help everyone, but it seems that the tutorial I'm using is outdated and things have changed since he wrote it. My best bet is to get a book on C#, as I feel like I'm drowning at the moment with some of the tutorials I'm reading.

Share this post


Link to post
Share on other sites
Any tutorials you get your hands on are most likely going to be poorly written to the point of being useless. A flag of a bad tutorial is starting out with WinForms.

Quote:
My best bet is to get a book on C#
Yes, please do this. And stick to console applications while learning, so you can focus on learning the language properly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!