Button next to the users in a loop - c#

I got a form with 7 users on it among each other. But I want to set 7 button next to it in a loop. Right now I've got this.
int x = 12;
int y = 30;
foreach (details dets1 in detailsList)
{
var label = new Label();
label.AutoSize = true;
label.Location = new Point(x, y);
label.Name = dets1.fname;
label.Text = dets1.fname;
this.Controls.Add(label);
y += label.Height + 5;
}
for (int i = 0; i < 7 ; i++)
{
Button button = new Button();
button.Location = new Point(200, 30);
button.Text = "Off";
button.Tag = i;
button.BackColor = Color.Red;
this.Controls.Add(button);
button.Click += button_Click;
}

Your problem is in the Location: you put all the buttons on the same place
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200, 30);
...
}
Let's organaize all the buttons vertically:
const int shift = 50;
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200, 30 + shift * i);
...
}
Or horizontally:
const int shift = 90;
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200 + shift * i, 30);
...
}

Related

How to create scrolling tiles in Visual Studio C# Forms

I want to make a tiles based scrolling game in Visual Studio C# using forms. I know its not the best platform for this but those are set parameters. I suppose the easiest way to think of the end program is like pokemon 2d top down world scrolling.
I can create a 2d array of picture boxes and allocated images to them based on a text 2d array of tile ids. I can scroll the picture boxes and when they reach a certain place, jump back to the original location and display a shifted tile.
My issue is adding controls, it only adds a column rather than the full grid. I have tried using tables but with the same problem.
Has anyone done this type of large world scroller using VSC# and forms? Are there any tutorials or suggestions? Thanks.
EDIT - Code added'
public Form1()
{
InitializeComponent();
TableLayoutPanel wholescreen = new TableLayoutPanel();
wholescreen.Location = new System.Drawing.Point(0,0);
wholescreen.Size = new System.Drawing.Size(200,200);
wholescreen.RowCount = 2;
wholescreen.ColumnCount = 2;
Controls.Add(wholescreen);
PictureBox item = new PictureBox();
item.Size = new System.Drawing.Size(50, 50);
item.ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";
for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 2; y++)
{
item.Left = x * 50;
item.Top = y * 50;
wholescreen.Controls.Add(item,x,y);
}
}
}
'
another way i tried....'
int WIDTH = 3;
int HEIGHT = 3;
PictureBox[] grid = new PictureBox[9];
//PictureBox[,] grid = new PictureBox[3, 3];
//int[,] level1 = new int[2, 2] { { 1, 2 }, { 3, 4 } };
int playerx = 0;
public Form1()
{
InitializeComponent();
int y = 0;
int x = 0;
for (int cntr = 0; cntr < HEIGHT*WIDTH; cntr++)
{
if ((cntr % HEIGHT) == 0)
{
x++;
y = 0;
}
grid[cntr] = new PictureBox();
grid[cntr].Left = x * 50;
grid[cntr].Top = y * 50;
grid[cntr].ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";
Controls.Add(grid[cntr]);
}
}
'
I think you are just creating one square and moving it around. Try...
private void Method2()
{
TableLayoutPanel wholescreen = new TableLayoutPanel();
wholescreen.BackColor = Color.AliceBlue;
wholescreen.Location = new System.Drawing.Point(0, 0);
wholescreen.Size = new System.Drawing.Size(200, 200);
wholescreen.RowCount = 2;
wholescreen.ColumnCount = 2;
Controls.Add(wholescreen);
PictureBox item;
// item.ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";
for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 2; y++)
{
item = new PictureBox();
item.Size = new System.Drawing.Size(50, 50);
item.BackColor = Color.Blue;
//item.Left = 0;
//item.Top = 0;
wholescreen.Controls.Add(item, x, y);
}
}
}

Can't take an instance using Controls.Add

I'm triyng to create buttons programmatically. I don't want to use Button btn = new Button();.. btn.height=.... control.Add(btn);. The code I'm using can add the buttons to the form but I can't take an instance so I can't create button.click event. Can anybody help me to solve it. The code I'm using below thanks.
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Controls.Add(
new Button()
{
Top = 50 + (50 * i),
Left = 50 + (50 * j),
Width = 50, Height = 50,
Text = (++k).ToString()
});
}
}
If you don't want to create a button, and subscribe directly to the event (i don't know why is this important), you can do this:
private void Method()
{
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Controls.Add(new Button() { Top = 50 + (50 * i), Left = 50 + (50 * j), Width = 50, Height = 50, Text = (++k).ToString() });
}
}
Controls.OfType<Button>().ToList().ForEach(x => x.Click += Button_Click);
}
private void Button_Click(object sender, System.EventArgs e)
{
MessageBox.Show((sender as Button).Text);
}

I have an array of colors and I want to assign the colors with the labels in the for loop in C#. How can I do so?

Color[] brickcolor = new Color[] { Color.Red, Color.Orange, Color.Pink, Color.Purple, Color.Blue, Color.Green, Color.Honeydew, Color.Lavender };
for (int c = 0; c <= 10; c++)
{
for (int r = 0; r <= 8; r++)
{
Label bricks = new Label();
bricks.Location = new Point(x, y);
bricks = brickcolor;
bricks.Width = 90;
bricks.Height = 25;
pnlGame.Controls.Add(bricks);
y += 30;
}
}
for (int r = 0; r < 8; r++)
{
Label bricks = new Label();
bricks.Location = new Point(x, y);
bricks.BackColor = brickcolor[r]; //or bricks.ForeColor
bricks.Width = 90;
bricks.Height = 25;
pnlGame.Controls.Add(bricks);
y += 30;
}

Aligning labels in array in C# to form a grid

I have created an array of black labels and have displayed them on a picture box. Unfortunately, I am not able to line them up directly at each intersection of the black lines. How can I do this?
InitializeComponent();
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
// create 361 labels, set their properties
for (int i = 0; i < 361; i++)
{
board[i] = new Label();
board[i].Parent = pictureBox1;
board[i].Location = new Point(x, y);
board[i].Name = "label" + i;
board[i].Text = "0";
board[i].BackColor = Color.Black;
//set size of labels
board[i].Size = new Size(31,31);
}
// set the position of the label
foreach (Label i in board)
{
//set distance between labels
if (x >= 1024)
{
x = pictureBox1.Location.X;
y += 52;
}
else
{
x += 52;
}
this.Controls.Add(i);
i.BringToFront();
i.Location = new Point(x, y);
}
as far as i have understand your question, and saw your code.
you are creating labels at same
Location(x,y) where x = 100 and y = 0
in the next loop
foreach (Label i in board)
{
if (x >= 1024)
{
x = 0;
y += i.Height + 55;
}
else if (y >= 1024)
{
y = 0;
x += i.Width + 55;
}
}
None of your condition will become true, because your x = 100 and y = 0
so location will remain same and all labels will be at same location
if you want to display a chess grid see this method
Chess Grid in Winforms
if you want to display labels on intersection of lines then lets modify your Code
x = PictureBox1.Location.X + 55;
y = pictureBox1.Location.Y + 55;
for (int i = 0; i < 361; i++)
{
board[i] = new Label();
board[i].Parent = pictureBox1;
board[i].Location = new Point(x,y);
board[i].Name = "label" + i;
board[i].Text = "0";
board[i].BackColor = Color.Black;
board[i].Size = new Size(55,55); //Define size of label according to your choice
if(x >= 1024)
{
x = PictureBox1.Location.X + 55; //Start position
y += 55; // Step to next line
}
else
x += 55; //jump to next horizontal box
}
I hope this helps

How to find canvas created by for loop in wpf

I've got function:
private string[] Letters = new string[11] {"", "A", "B", "C", "D", "E","F", "G","H", "I", "J"};
private void GenerateGameMap()
{
int LocA = 5;
int LocB = 5;
for(int i = 1; i < 12; i++)
{
for(int i2 = 1; i2 < 12; i2++)
{
Canvas canvas = new Canvas();
canvas.Width = 26;
canvas.Height = 26;
canvas.Margin = new Thickness(LocA + 1, LocB + 1, 0, 0);
Border border = new Border();
border.BorderThickness = new Thickness(2);
if (i == 1 || i2 == 1)
{
Label label = new Label();
label.FontFamily = new FontFamily("Arial");
label.FontSize = 20;
label.Foreground = Brushes.White;
label.Margin = new Thickness(-1, -2, 0, 0);
label.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
label.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
if(i == 1)
{
if(i2 > 1)
{
label.Content = Litery[i2 - 1];
}
}else
{
if(i2==1)
{
label.Content = (i - 1).ToString();
}
}
border.BorderBrush = Brushes.Gold;
canvas.Background = Brushes.Black;
canvas.Children.Add(label);
}else
{
border.BorderBrush = Brushes.CadetBlue;
canvas.Background = Brushes.BurlyWood;
}
if(i > 1 && i2 > 1 )
{
canvas.Name = Letters[i2 - 1] + (i - 1).ToString();
canvas.MouseLeftButtonUp +=canvas_MouseLeftButtonUp;
}
border.Width = 28;
border.Height = 28;
border.Margin = new Thickness(LocA, LocB, 0, 0);
LocA+=30;
MainGameCanvas.Children.Add(canvas);
MainGameCanvas.Children.Add(border);
if(i2 == 11)
{
LocA = 5;
}
}
LocB += 30;
}
}
Everything is fine with that function. But when I'am trying to find canvas:
private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
string canvasName = Text1.Text + Text2.Text;
var GameObject = MainGameCanvas.FindName(canvasName);
Canvas SelectedGameobject = GameObject as Canvas;
SelectedGameobject.Background = Brushes.YellowGreen;
}
I'am receiving error 'Object reference not set on instance of an object' in line "SelectedGameobject.Background = Brushes.YellowGreen;". Is there other way to find this control ?. As i said, I'am sure that function GenerateGameMap() is working corectly.

Categories

Resources