i'm drawing rectangles in line, and every turn it's supposed to delete one rectangles every turn, until i wont be less than 1, but for some reason it's only deleting one rectangle and thats it, what am i doing wrong?
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
int i;
int j;
int h = 0;
int w = 0;
int x = 0;
int y = 0;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Graphics gr = panel1.CreateGraphics();
{
for (i = 5; i > 1; i--)
{
gr.DrawRectangle(Pens.Black, x, 70 + y, 60 + h, 60 + w);
x += 70;
}
x = 0;
y += 70;
}
}
}
}
form1
Related
The method genereer ("generate") draws a Bitmap image of a Mandelbrot. It has a variable schaal ("scale") which makes the Bitmap zoom in on the image with a certain factor. The standard value for this is 0.01, as declared above in the partial class Form1.
I have a textBox3 in the GUI, and I want to be able to type a new value in this textBox3, click on the button and draw a new image based on the scale.
I've written a method called Lees ("read"), which parses a double from textBox3. I've put this method in the Button1_MouseClick method, as well as the genereer method to draw a new image upon button click. I've also put the genereer method in the Form1_Shown method.
However, this doesn't seem to be working. When I click on the button, no matter what the value is, I get a white dot in the middle of the screen.
public partial class Form1 : Form
{
public double waardeX, waardeY, schaal = 0.01;
public int maxIteraties;
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
Genereer();
}
public void Lees()
{
schaal = double.Parse(textBox3.Text);
}
private void Genereer()
{
double max = 100;
Bitmap bitmap = new Bitmap(Figuur.Width, Figuur.Height);
for (int x = 0; x < Figuur.Width; x++)
{
for (int y = 0; y < Figuur.Height; y++)
{
//double a = (double)(x - (Figuur.Width / 2)) / (double)(Figuur.Width / 4);
//double b = (double)(y - (Figuur.Height / 2)) / (double)(Figuur.Height / 4);
double a = (double)(x - (Figuur.Width / 2)) * (double)(schaal);
double b = (double)(y - (Figuur.Height /2)) * (double)(schaal);
Mandelgetal getal = new Mandelgetal(a, b);
Mandelgetal waarde = new Mandelgetal(0, 0);
int i = 0;
while (i < max)
{
//i++; //v1
waarde.Vermenigvuldig();
waarde.Toevoegen(getal);
if (waarde.Wortel() > 2.0)
{
break;
}
else
{
if (i % 2.0 == 0.0)
{
bitmap.SetPixel(x, y, Color.White);
i++; //v2
}
else
{
bitmap.SetPixel(x, y, Color.Black);
i++; //v2
}
}
}
if((a*a + b*b) > 4)
{
bitmap.SetPixel(x, y, Color.Black);
}
Figuur.Image = bitmap;
}
}
}
private void Button1_MouseClick(object sender, MouseEventArgs e)
{
Lees();
Genereer();
this.Invalidate();
}
}
}
I am trying to make Chinese Checkers in C#. I'm having trouble moving a piece when it is clicked on. Right now I just want it to move where ever. What it is doing is creating a new instance of the piece object when it needs to just move. Any help would be great thank you. Here is the code.
namespace ChineseCheckers
{
public partial class mainForm : Form
{
private Board thisBoard = new Board();
public event PaintEventHandler paint;
public mainForm()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void mainForm_Paint(object sender, PaintEventArgs e)
{
int width = 15;
int del = 3;
int ystart = 50;
int xstart = ystart+4 * width;
SolidBrush whiteSB = new SolidBrush(Color.White);
for (int i = 0; i < 17; i++)
{
for (int j = 0; j < 17; j++)
{
if (Board.isSpace(i, j))
{
Space sp = thisBoard.getSpace(i, j);
int xPos = getXFromIndex(i, j, width, xstart, ystart);
int yPos = getYFromIndex(i, j, width, xstart, ystart);
pieceObject piece = new pieceObject(e, xPos - del, yPos - del, getColor(sp));
EventHandler myhandler = new EventHandler((a, b)=>myButton_Click(sender, e, piece));
piece.Click += myhandler;
piece.Size = new System.Drawing.Size(20, 20);
piece.Location = new System.Drawing.Point(xPos - del, yPos - del);
piece.BackColor = getColor(sp);
this.Controls.Add(piece);
}
}
}
}
private int getYFromIndex(int i, int j, int width, int xstart, int ystart)
{
return ystart + (2 * i) * width;
}
private int getIFromXY(int x, int y, int width, int xstart, int ystart)
{
return (y - ystart + width) / (2 * width);
}
private int getXFromIndex(int i, int j, int width, int xstart, int ystart)
{
return xstart + (2 * j - i) * width;
}
private int getJFromXY(int x, int y, int width, int xstart, int ystart)
{
int i = getIFromXY(x, y, width, xstart, ystart);
return (((x - xstart+ width/2) / width) + i) / 2;
}
private Color getColor(Space sp)
{
switch (sp)
{
case Space.Player1:
return Color.Orange;
case Space.Player2:
return Color.Yellow;
case Space.Player3:
return Color.Green;
case Space.Player4:
return Color.Blue;
case Space.Player5:
return Color.Purple;
case Space.Player6:
return Color.Red;
}
return Color.Gray;
}
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
Form currentForm = mainForm.ActiveForm;
currentForm.BackgroundImage = null;
currentForm.BackColor = Color.Black;
currentForm.Width = 600;
currentForm.Height = 650;
Button endTurn = new Button();
endTurn.Location = new Point(485, 575);
endTurn.Text = "End Turn";
endTurn.BackColor = Color.Wheat;
endTurn.Click += new EventHandler(endTurnEvent);
this.Controls.Add(endTurn);
}
public void endTurnEvent(object sender, EventArgs e)
{
System.Console.WriteLine("You have ended your turn!");
}
private void mainForm_MouseClick(object sender, MouseEventArgs e)
{
}
void myButton_Click(Object sender, System.EventArgs e, pieceObject piece)
{
System.Console.WriteLine("Position: " + piece.Location + "Color: " + piece.BackColor);
piece.Location = new Point(40, 40);
}
}
}
public class pieceObject : UserControl
{
// Draw the new button.
public pieceObject(PaintEventArgs e, int xPos, int yPos, Color color)
{
int width = 15;
SolidBrush whiteSB = new SolidBrush(color);
SolidBrush newPiece = new SolidBrush(color);
e.Graphics.FillEllipse(newPiece, xPos, yPos, width, width);
newPiece.Dispose();
}
}
Without a good, minimal, complete code example it is impossible to know for sure what the best advice would be. But given your problem statement:
What it is doing is creating a new instance of the piece object when it needs to just move
…it is at least possible to identify the specific bug that causes that and offer some suggestions.
The reason you are getting new instances of the pieceObject class is that you are creating them every time the form's Paint event is raised. It is important to understand that the Paint event is raised every time that Windows decides that the window needs to be redrawn. It's not just a one-time thing.
Based on the direction you currently appear to be heading in terms of design, it seems likely you really do only want the code you've put in the Paint event handler to be executed just once. If so, I'd recommend changing the code as follows:
First, fix the pieceObject so that it draws itself as needed:
public class pieceObject : UserControl
{
private readonly Color _color;
public pieceObject(Color color) { _color = color; }
// Draw the new button.
protected override void OnPaint(PaintEventArgs e)
{
int width = 15;
using (SolidBrush newPiece = new SolidBrush(_color))
{
e.Graphics.FillEllipse(newPiece, 3, 3, width, width);
}
}
}
Then, instead of creating your pieceObject instances every time the form's Paint event is raised, just do it once, when the form is created:
public mainForm()
{
InitializeComponent();
InitializePieceControls();
}
private void InitializePieceControls()
{
int width = 15;
int del = 3;
int ystart = 50;
int xstart = ystart+4 * width;
for (int i = 0; i < 17; i++)
{
for (int j = 0; j < 17; j++)
{
if (Board.isSpace(i, j))
{
Space sp = thisBoard.getSpace(i, j);
int xPos = getXFromIndex(i, j, width, xstart, ystart);
int yPos = getYFromIndex(i, j, width, xstart, ystart);
pieceObject piece = new pieceObject(getColor(sp));
piece.Click += myButton_Click;
piece.Size = new System.Drawing.Size(20, 20);
piece.Location = new System.Drawing.Point(xPos - del, yPos - del);
// It's not clear why you are doing this, as it will just make
// it impossible to see the ellipse that is drawn by the piece
// itself
piece.BackColor = getColor(sp);
this.Controls.Add(piece);
}
}
}
}
void myButton_Click(object sender, EventArgs e)
{
pieceObject piece = (pieceObject)sender;
System.Console.WriteLine("Position: " + piece.Location + "Color: " + piece.BackColor);
piece.Location = new Point(40, 40);
}
Notes:
In the above I've also removed a couple of places you created a white brush, didn't use it, and then failed to dispose of it.
Your use of the Click event on the pieceObject was wrong in a couple of ways: first, the lambda expression captured the sender and event args of the Paint event, not the a and b actually sent with the event; second, there was no need to wrap the event handler in the lambda in the first place, because you can just cast the actual sender of the Click event to the pieceObject that is in fact sending the event.
I have this code:
public partial class Form1 : Form
{
private int size;
public Form1()
{
InitializeComponent();
size = 10;
}
private void runAutomat_Click(object sender, EventArgs e)
{
var myMatrix = new int[size][];
for (int i = 0; i < size; i++)
{
myMatrix[i] = new int[size];
for (int j = 0; j < size; j++)
myMatrix[i][j] = 0;
}
var cw = new MyWebService();
var result = cw.FillMatrix(myMatrix, size);
}
}
Next I want to draw grid for result, but I don't have idea how to send it to method with PaintEventArgs. For example something like this:
private void PB_Paint(object sender, PaintEventArgs e)
{
int cellSize = 2;
for (int x = 0; x < size; ++x)
for (int y = 0; y < size; ++y)
{
if (result [y, x].state == 1)
e.Graphics.FillRectangle(new System.Drawing.SolidBrush(Color.Cyan), new Rectangle(y * cellSize, x * cellSize, cellSize, cellSize));
else if (result [y, x].state == 2)
e.Graphics.FillRectangle(new System.Drawing.SolidBrush(Color.Yellow), new Rectangle(y * cellSize, x * cellSize, cellSize, cellSize));
}
}
I know it is incorrect and I need better solution.
You could store the value of result as a form level variable and then call this.Refresh() to make the form redraw.
public partial class Form1 : Form
{
//Guessing what the data type is:
private int[,] _result;
private void runAutomat_Click(object sender, EventArgs e)
{
//snip
_result = cw.FillMatrix(myMatrix, size);
this.Refresh();
}
}
Ok, I think I found temporary solution.
using WFConsume.localhost;
public partial class Form1 : Form
{
private int size;
private localhost.Cell [][]cells;
public Form1()
{
InitializeComponent();
size = 10;
}
}
private void runAutomat_Click(object sender, EventArgs e)
{
var myMatrix = new int[size][];
for (int i = 0; i < size; i++)
{
myMatrix[i] = new int[size];
for (int j = 0; j < size; j++)
myMatrix[i][j] = 0;
}
MyWebService cw = new MyWebService();
cells = cw.FillMatrix(myMatrix, size);
}
It's working in form level. Thank you DavidG for a tips!
I am working on an application for WP8, in my code I want to assign indexes to buttons like in arrays. Reason is that I want to operate buttons with buttons (i.e one button that is pressed activate other buttons)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class LEDButton : Button
{
public const int LEDWidth = 50;
public const int LEDHeight = 30;
public LEDButton()
{
BackColor = Color.Tan;//inner color
//BackColor = Color.FromArgb(0, 64, 0);
ForeColor = Color.Yellow;//outline
FlatStyle = FlatStyle.Popup;//Button style
Size = new Size(LEDWidth, LEDHeight);
UseVisualStyleBackColor = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
LEDButton[,] b = new LEDButton[4, 4];
for (int y = 0; y < b.GetUpperBound(0); y++)
{
for (int x = 0; x < b.GetUpperBound(1); x++)
{
b[y, x] = new LEDButton()
{
//put button properties here
Name = "button" + y.ToString() + x.ToString(),//String.Format("Button{0}{1}", y, x),
TabIndex = 10 * y + x,
Text = y.ToString() + x.ToString(),
Location = new Point(LEDButton.LEDWidth * x + 20, LEDButton.LEDHeight * y + 20)
};
// b[y, x].Click += button_Click;
}
}
// add buttons to controls
for (int y = 0; y < b.GetUpperBound(0); y++)
for (int x = 0; x < b.GetUpperBound(1); x++)
this.Controls.Add(b[y, x]);
}
}
}
If I have understood your question properly, you should rely on an array of delegates. Here you have a correction of your code assigning dynamically 4 different methods to 4 different buttons:
namespace test2
{
public partial class Form1 : Form
{
public delegate void button_click(object sender, EventArgs e);
public static button_click[] clickMethods = new button_click[4];
public Form1()
{
InitializeComponent();
}
public class LEDButton : Button
{
public const int LEDWidth = 50;
public const int LEDHeight = 30;
public LEDButton()
{
BackColor = Color.Tan;//inner color
//BackColor = Color.FromArgb(0, 64, 0);
ForeColor = Color.Yellow;//outline
FlatStyle = FlatStyle.Popup;//Button style
Size = new Size(LEDWidth, LEDHeight);
UseVisualStyleBackColor = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
clickMethods[0] = buttonGeneric_Click_1;
clickMethods[1] = buttonGeneric_Click_2;
clickMethods[2] = buttonGeneric_Click_3;
clickMethods[3] = buttonGeneric_Click_4;
}
private void buttonGeneric_Click_1(object sender, EventArgs e)
{
}
private void buttonGeneric_Click_2(object sender, EventArgs e)
{
}
private void buttonGeneric_Click_3(object sender, EventArgs e)
{
}
private void buttonGeneric_Click_4(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
LEDButton[,] b = new LEDButton[4, 4];
for (int y = 0; y < b.GetUpperBound(0); y++)
{
for (int x = 0; x < b.GetUpperBound(1); x++)
{
b[y, x] = new LEDButton()
{
//put button properties here
Name = "button" + y.ToString() + x.ToString(),//String.Format("Button{0}{1}", y, x),
TabIndex = 10 * y + x,
Text = y.ToString() + x.ToString(),
Location = new Point(LEDButton.LEDWidth * x + 20, LEDButton.LEDHeight * y + 20)
};
if (y <= 3)
{
b[y, x].Click += new System.EventHandler(clickMethods[y]);
}
}
}
// add buttons to controls
for (int y = 0; y < b.GetUpperBound(0); y++)
for (int x = 0; x < b.GetUpperBound(1); x++)
this.Controls.Add(b[y, x]);
}
}
}
--- loop ---
Button abc = new Button();
abc.Name = loopCounter.ToString();
--- loop ---
This will help you to assigning indexes,
Don't use array of Button, it is useless!
I am recreating the classic game Reversi using c# in order to improve my skills at programming
but I have a problem when I check the colors. So far I have managed to revers colors from left and from top but it doesn't work correctly it only reverses the colors when I hit the last square on the board.
Any help would be much appreciated
Here is an image that may explain what I mean (the code is below) mean
The code that I have:
namespace praprevers
{
public partial class Form1 : Form
{
private Button[,] squares;
//private Button[,] r0;
public Form1()
{
InitializeComponent();
squares = new Button[4, 4];
squares = new Button[,] {
{btn_0_0, btn_0_1, btn_0_2, btn_0_3},
{btn_1_0, btn_1_1, btn_1_2, btn_1_3},
{btn_2_0, btn_2_1, btn_2_2, btn_2_3},
{btn_3_0, btn_3_1, btn_3_2, btn_3_3}
};
}
int _turn = 0;
private void DrawColor(object sender, EventArgs e)
{
Button b = sender as Button;
string[] btnData = b.Name.Split('_');
int x = int.Parse(btnData[1]);
int y = int.Parse(btnData[2]);
//check for possible combinations
int top = x - 3;
int botton = x +3;
int left = y - 3;
int right = y + 3;
for (int l = 0; l < 4; ++l)
{
if (top >= 0 && squares[top, y].BackColor == Color.Black)
{
squares[top + l, y].BackColor = Color.Black;
}
else if (left >= 0 && squares[x, left].BackColor == Color.Black)
{
squares[x, left + l].BackColor = Color.Black;
}
}
if (_turn == 0)
{
_turn = 1;
b.BackColor = Color.Black;
}
else
{
_turn = 0;
b.BackColor = Color.Red;
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Button sqrr in squares)
{
sqrr.Click += new System.EventHandler(this.DrawColor);
}
}
}
}