Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Can anyone help me to create dynamically, multidimensional array of textboxes on button click.Thanks!
Try this
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int ROW_TEXTBOX = 5;
const int COL_TEXTBOX = 6;
const int TEXTBOX_WIDTH = 100;
const int TEXTBOX_HEIGHT = 30;
const int SPACING = 20;
List<List<TextBox>> textboxes = new List<List<TextBox>>();
private void button1_Click(object sender, EventArgs e)
{
for (int row = 0; row < ROW_TEXTBOX; row++)
{
List<TextBox> newRow = new List<TextBox>();
textboxes.Add(newRow);
for (int col = 0; col < COL_TEXTBOX; col++)
{
TextBox newbox = new TextBox();
newbox.Width = TEXTBOX_WIDTH;
newbox.Height = TEXTBOX_HEIGHT;
newbox.Top = (row * (TEXTBOX_HEIGHT + SPACING)) + SPACING;
newbox.Left = (col * (TEXTBOX_WIDTH + SPACING)) + SPACING;
newRow.Add(newbox);
this.Controls.Add(newbox);
}
}
}
}
}
Related
I have a class project to create a tic tac toe simulations (game is not played by people) where O's and X's automatically generate when the New Game button is clicked. I am having trouble with the code to get the labels to show the output.
Using a 2D array type INT to simulate the game board, it should store a 0 or 1 in each of the 9 elements and produce a O or X. There also needs to be a label to display if X or O wins.
Here is my code so far ( I know there isn't much, I'm completely lost):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void StartButton_Click(object sender, EventArgs e)
{
Random rand = new Random();
const int ROWS = 3;
const int COLS = 3;
int[,] gameBoard = new int[ROWS, COLS];
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{ gameBoard[row, col]= rand.Next(2); }
}
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Win Forms labels get populated unless the current event process finishes completely. If you want to update the labels with X and O while you may use the control property InvokeRequired (boolean) and after assigning the value to label call label.Refresh() function. I will suggest fork a thread on hitting start button and do the for loop -> random.next() inside the thread. Try with these changes. All the Best!!
Try following :
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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
const int ROWS = 3;
const int COLS = 3;
const int WIDTH = 100;
const int HEIGHT = 100;
const int SPACE = 20;
static TextBox[,] gameBoard;
public Form1()
{
InitializeComponent();
gameBoard = new TextBox[ROWS, COLS];
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
TextBox newTextBox = new TextBox();
newTextBox.Multiline = true;
this.Controls.Add(newTextBox);
newTextBox.Height = HEIGHT;
newTextBox.Width = WIDTH;
newTextBox.Top = SPACE + (row * (HEIGHT + SPACE));
newTextBox.Left = SPACE + (col * (WIDTH + SPACE));
gameBoard[row, col] = newTextBox;
}
}
}
}
}
I've been following the instructions for an assignment and I can't see any issues with the code (no error messages and the program runs without crashing), but the program does not draw the graphics. It's supposed to random out 200 random numbers between 1-100, and then sort them into a graph with a bubble sort
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 WindowsFormsApp6
{
public partial class Form1 : Form
{
int[] number = new int[200];
Random generator = new Random();
public Form1()
{
InitializeComponent();
}
private void btnGenerate_Click(object sender, EventArgs e)
{
for (int i = 0; i < number.Length; i++)
{
number[i] = generator.Next(1, 101);
}
Invalidate();
}
private void btnSort_Click(object sender, EventArgs e)
{
BubbleSort(number);
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//Assigns origo
Point origo = new Point(40,150);
//Draw x and y axes
e.Graphics.DrawLine(Pens.Black, origo.X, origo.Y, origo.X, origo.Y - 100);
e.Graphics.DrawLine(Pens.Black, origo.X, origo.Y, origo.X + 200, origo.Y);
//Draw all points
for(int i = 0; i < number.Length; i++)
{
e.Graphics.FillEllipse(Brushes.Red, origo.X + i, origo.Y - number[i], 2, 2);
}
}
public void BubbleSort(int[] list)
{
for (int m = list.Length - 1; m > 0; m--)
{
for (int n = 0; n < m; n++)
{
if (list[n] > list[n + 1])
{
int temp = list[n];
list[n] = list[n + 1];
list[n + 1] = temp;
}
}
}
}
}
}
The program should draw 200 points when you press the button "Generate" and then sort them into a line/graph when you press "Sort", but the program isn't drawing anything when I press the buttons.
I am creating a number of comboBoxes based on user input. I create the boxes just fine, but when it comes to wanting to check the text within them I am struggling.
I thought of maybe storing them in a IList but that hasn't seemed to work so far. The goal is to change the text of all of them on a button click, but after several attempts I am becoming frustrated.
IList<ComboBox> comboBoxes = new List<ComboBox>();
private void AddComboBox(int i)
{
var comboBoxStudentAttendance = new ComboBox();
comboBoxStudentAttendance.Top = TopMarginDistance(i);
comboBoxStudentAttendance.Items.Add("");
comboBoxStudentAttendance.Items.Add("Present");
comboBoxStudentAttendance.Items.Add("Absent");
comboBoxStudentAttendance.Items.Add("Late");
comboBoxStudentAttendance.Items.Add("Sick");
comboBoxStudentAttendance.Items.Add("Excused");
comboBoxes.Add(comboBoxStudentAttendance);
this.Controls.Add(comboBoxStudentAttendance);
}
I tried the following but with no success.
private void DistributeAttendanceButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < sampleNum; i++)
{
switch (MasterComboBox.Text)
{
case "Present":
comboBoxes.ElementAt(i).Text = "Present";
break;
}
}
}
Try this
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
const int TOP_MARGIN = 10;
const int LEFT_MARGIN = 10;
const int WIDTH = 200;
const int HEIGHT = 10;
const int SPACE = 15;
const int NUMBER_OF_BOXES = 10;
public Form1()
{
InitializeComponent();
MasterComboBox.Text = "Present";
for (int i = 0; i < NUMBER_OF_BOXES; i++)
{
AddComboBox(i);
}
}
List<ComboBox> comboBoxes = new List<ComboBox>();
private void AddComboBox(int i)
{
var comboBoxStudentAttendance = new ComboBox();
comboBoxStudentAttendance.Top = TOP_MARGIN + i * (SPACE + HEIGHT);
comboBoxStudentAttendance.Left = LEFT_MARGIN;
comboBoxStudentAttendance.Width = WIDTH;
comboBoxStudentAttendance.Height = HEIGHT;
comboBoxStudentAttendance.Items.Add("");
comboBoxStudentAttendance.Items.Add("Present");
comboBoxStudentAttendance.Items.Add("Absent");
comboBoxStudentAttendance.Items.Add("Late");
comboBoxStudentAttendance.Items.Add("Sick");
comboBoxStudentAttendance.Items.Add("Excused");
comboBoxes.Add(comboBoxStudentAttendance);
this.Controls.Add(comboBoxStudentAttendance);
}
private void DistributeAttendanceButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < comboBoxes.Count; i++)
{
switch (MasterComboBox.Text)
{
case "Present":
comboBoxes[i].Text = "Present";
break;
}
}
}
}
}
I'm new to Unity and programming and I'm trying to make this game with moving cars on gameboard. My idea is to create an array and somehow store information about each element or tile in this array. I'd like these tiles to be able to be referenced to later, e.g. to detect if there is car GO on specific tile or not, etc. Unfortunately I¨m struggling how exactly should I save information to a tile so I can reference to it later, I mean later when I create a method which should be able to detect if that tile is occupied or not.
Thank you for all advices in advance!
The code below adds buttons to a panel control. You can replace the button with an image to use in your games.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
const int ROWS = 10;
const int COLS = 15;
const int WIDTH = 20;
const int HEIGHT = 20;
const int SPACE = 10;
List<List<MyButton>> buttons = new List<List<MyButton>>();
public Form1()
{
InitializeComponent();
for (int row = 0; row < ROWS; row++)
{
List<MyButton> newRow = new List<MyButton>();
buttons.Add(newRow);
for (int col = 0; col < COLS; col++)
{
MyButton newButton = new MyButton();
newRow.Add(newButton);
newButton.Width = WIDTH;
newButton.Height = HEIGHT;
newButton.Left = col * (WIDTH + SPACE);
newButton.Top = row * (HEIGHT + SPACE);
newButton.row = row;
newButton.col = col;
panel1.Controls.Add(newButton);
}
}
}
}
public class MyButton : Button
{
public int row { get; set; }
public int col { get; set; }
}
}
The string formats just fine in command line but in gui with a label it is all off. I think my formatting is correct
Example:
Code:
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 DisplayMultiplicationTableGUI
{
public partial class Form1 : Form
{
int i, j;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = String.Format("{0,3}", " ");
for (i = 1; i <= 10; i++)
label1.Text += String.Format(" {0,3}", (i).ToString());
for (i = 1; i <= 10; i++)
label2.Text += String.Format("\n{0,3} ", (i).ToString());
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++)
label3.Text += String.Format("{0,3} ", (i*j).ToString());
label3.Text += String.Format("\n");
}
}
}
}
}
The easiest way to solve this is to use a fixed-width font. You're never going to get this to line up correctly with a proportional font, unless you put each number in its own label or text box, or use a DataGridView.