Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to make a qGame puzzle for my assingment, but i cant find any help online,
below is one of my forms,
i need have a color selector so when i click on one of my block its takes that color then im suppose to save it and open it in a different form with justthe colored blocks..
i need help.

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;
using System.IO;
namespace QGame_New
{
    public partial class Design_Maze : Form
    {
         public Design_Maze()
        {
            InitializeComponent();
        }
         protected void generateButton_Click(object sender, EventArgs e)
         {
             int numberOfColumns = int.Parse(columnTextBox.Text);
             int numberOfRows = int.Parse(rowsTextBox.Text);
             int locX = 0, locY = 56;

             for (int i = 1; i <= numberOfColumns * numberOfRows; i++)
             {


                 Button x = new Button();
                 x.Name = "x" + i.ToString();
                 x.Size = new Size(30, 30);
                 x.Location = new Point(locX, locY);
                 x.Click += new EventHandler(x_Click);
                 if (((i + 1) % numberOfColumns) == 1)
                 {
                     locX = 0;
                     locY += x.Size.Height + 0;
                 }
                 else
                 {
                     locX += x.Size.Width + 0;
                 }
                 pictureBox1.Controls.Add(x);
                 x.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

                 if ((x.Location.X + x.Width) > this.ClientSize.Width)
                 {
                     this.Width += ((x.Location.X + x.Width) - this.ClientSize.Width) + 500;
                 }
                 if ((x.Location.Y + x.Height) > this.ClientSize.Height)
                 {
                     this.Height += ((x.Location.Y + x.Height) - this.ClientSize.Height) + 5;

                 }
             }
         }
void  x_Click(object sender, EventArgs e)
{
 	Button x = (Button)sender;
    Button color = redBox();
}
        private Color Color(string p)
        {
            throw new NotImplementedException();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    // Code to write the stream goes here.
                    myStream.Close();
                    SaveFileDialog dialog = new SaveFileDialog();
                    //

                }
            }
        }
        private void redBox_Click(object sender, EventArgs e)
        {
            button2.BackColor = SystemColors.ControlLightLight;
        }
        private void flieToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
        private void emptyBoxButton_Click(object sender, EventArgs e)
        {
          
        }
        private void Design_Maze_Load(object sender, EventArgs e)
        {
        }
       
    
    }
}


[edit]added code block[/edit]
Posted
Updated 4-Apr-11 16:15pm
v2
Comments
Sergey Alexandrovich Kryukov 4-Apr-11 22:31pm    
How can you hope for making a game if your coding is so pure (so many immediate constants hard-coded, for example)? What's the problems?
--SA
jim lahey 5-Apr-11 2:49am    
I know it's still early, but I don't see what this has to do with inheritance..

1 solution

I don't see anything here which has to do with inheritance, and I am not quite sure what you problem is: there is nothing about colour in your code, except a few disconnected bits an pieces.

However, you have complicated your code quite a bit unnecessarily. Instead of a single loop to create a row and column matrix, it is a lot clearer if you use a pair of nested loops:
Point buttonLocn = new Point(0, 56);
Size buttonSize = new Size(30, 30);
int locX = buttonLocn.X, locY = buttonLocn.Y;
int i = 1;
for (int xButton = 0; xButton < numberOfColumns; xButton++)
    {
    locY = buttonLocn.Y;
    for (int yButton = 0; yButton < numberOfRows; yButton++)
        {
        Button x = new Button();
        x.Name = "x" + i.ToString();
        i++;
        x.Size = new Size(buttonSize.Width, buttonSize.Height);
        x.Location = new Point(locX, locY);
        x.Click += new EventHandler(x_Click);
        x.Location = new Point(locX, locY);
        locY += buttonSize.Height;
        pictureBox1.Controls.Add(x);
        }
    locX += buttonSize.Width;
    }
Note that I have replaced your "magic numbers" with more readable versions.

I'm not sure what you need, but this is simple code to change the colour when you click a button:
private void x_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        b.BackColor = GetTurnColor();
        }
    }
int turn = 0;
private Color GetTurnColor()
    {
    // Since I don't know what your game is supposed to do,
    // I'll just dummy something in...
    turn++;
    if ((turn & 1) == 0)
        {
        return Color.Red;
        }
    return Color.Blue;
    }
Each time you click, the button colour will be set red or blue, alternating.
 
Share this answer
 
Comments
SwitcherSoft 5-Apr-11 4:52am    
Agree, 5 from me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900