Help with Visual C# 2008 program

Started by
7 comments, last by jpetrie 15 years, 7 months ago
Hi everyone, I am attempting to start a maze program in my computer class. This is a short description of part of my assignment. For this assignment, im to create a program that will allow the user to construct a maze, and then will find a shortest path from a given internal cell to an exit. Goals To give you experience in constructing a GUI programmatically. To give you experience in working with 2-dimensional arrays. To give you experience in working with a queue. To give you an introduction to the breadth-first search algorithm. Functionality The program should initially display a GUI that looks like: This GUI consists of a 10 x 10 grid of square buttons representing the cells of the maze. Surrounding each square button are 4 smaller rectangular buttons, representing locations where walls can be placed. Clicking on one of the rectangular buttons should cause its color to change to black (System.Drawing.Color.Black). Subsequent clicks should cause its color to alternate between black and the default background color for controls (System.Windows.Forms.Control.DefaultBackColor). Thus, the user can construct as maze The visual editor should not be used except perhaps to put the name "Maze" on the title of the Form. (You may, of course, use the visual editor to create buttons in order to see what code it creates, but these buttons must be removed before you create the final version of your project.) Each of the "wall" buttons should have its Flatstyle property set to Flatstyle.Flat, while each of the "cell" buttons should have its Flatstyle property set to Flatstyle.Popup. Set the TabIndex properties of all the buttons, so that the GUI can be controlled by either the mouse or the keyboard. Only two event handlers should be written. One event handler should be associated with all of the "wall" buttons; this method should change the color of the associated button. The first parameter of the event handler will give you the Button that was clicked. You will need to cast this value to Button before you can use any of its members. The other event handler should be associated with all the "cell" buttons. This event handler will be responsible for all of the functionality of these buttons, as described above. Im confused about is creating the GUI without the visual editor. For the first 2 assignments we used the visual editor. Im lost on how to start this. It sounds like we are still using Visual C# 2008 but not using it to create the GUI. Please help! Thanks everyone, Andrew
Advertisement
I would highly recommend looking at the first two projects you created. Open them up in VS2k8 and examine the code that was created by the IDE. The code that the designer created is held in a file called formname.Designer.cs under formname.cs in the solution explorer. Example, it will be called Form1.Designer.cs under Form1.cs by default.

Open up that file and then expand the 'Windows Forms Designer generated code' to see what code was created when you were using the designer interface. Then play with it to get a feel for how it works before finally working on your new assignment.
Ok I have this code in my visual editor Maze.cs

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace practice{    public partial class Maze : Form    {        public Maze()        {            InitializeComponent();            button();        }        public void button()        {            this.button1 = new System.Windows.Forms.Button();            this.SuspendLayout();            //             // button1            //             this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;            this.button1.Location = new System.Drawing.Point(13, 13);            this.button1.Name = "button1";            this.button1.Size = new System.Drawing.Size(91, 23);            this.button1.TabIndex = 0;            this.button1.Text = "HELL YEAH";            this.button1.UseVisualStyleBackColor = true;            this.button1.Click += new System.EventHandler(this.button1_Click);        }        private void button1_Click(object sender, EventArgs e)        {        }    }}


Im just trying to insert one button without using the editor but still no luck. I first put the button in with the editor to get all the code. Then I deleted the button from the designer. And now even if I change the numbers in the code the button shows up in the same spot with the same settings everytime even if I change the numbers. And it keeps telling me there is no definition for button1?
I have no clue what im doing wrong please help!

Thanks Andrew
You have to declare the button1 variable:
public partial class Maze : Form{  Button button1;  // ....


You might have missed the declaration in the designer-generated components. You also should add button1 to the form's (this's) list of Controls. this.Controls.Add(button1) or some such.

If another button is still showing up somewhere, you probably haven't actually deleted it from the designer. However I suspect since the undeclared button1 is a compiler error, when you're running the program you're running the older version, before you deleted the designer-generated button.
Ok thanks ill try that. I have another question though. If im wanting my button to change from white to black when I click it how would I do this? And if I click it again how would I change it back?

Thanks Andrew
Buttons have an event you can register for , simply register your function that sets the color with the button's event. Heres some pseudocode i'll leave the rest for you to figure out.

load function()
{
create firstbutton;
register changebuttoncolor function with firstbutton's click event handler here;
add your button to the form here (add it to the form's control collection);
}

ChangeButtonColor function(normal event parameters here)
{
create tempbuton = (button)sender;
if tempbutton is white make it black
else make it white
}

Since you registered the function with the click event handler it gets called everytime the button gets clicked , so it's in there you should swap colors.

Some links to help you figure out all of the steps in the pseudocode:
using the button class and creating a button and adding it to a control or a form's collection
how to register your functions with events
scroll down to the C# sample for an example implementation of a function that manipulates a button's data when the button is clicked (this function is already assumed to be registered with the click event in this sample) , note the changing of the backcolor
What sort of class is this that expects you to write such a complex program in C# when you don't even have the basic knowledge of how to create buttons at runtime?
This is all pretty basic stuff that is covered in any basic C# book like "Head first C#".
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
ok this works for changing the color of my button.

        private void button1_Click(object sender, EventArgs e)        {            Color c = Color.Black;            ((Button)sender).BackColor = c;        }


How would I do this for 320 buttons if I dont want to type this out 320 times. Ive tried to do a for loop but it keeps giving me errors?

Thanks Andrew
What errors? Your buttons will need to be in an array or some kind of container for a loop to be viable. Review your textbooks section on for loops for their examples, it should become clear.

Why do you have 320 buttons? That's usually a bad sign.

This topic is closed to new replies.

Advertisement