如何在C#Windows窗体应用程序中动态创建网格
我有一个项目,需要在Windows窗体应用程序上动态创建一个网格,该应用程序在每个单元格上都有一个单击侦听器 网格需要为12 X 12 我对此进行了反复研究,但找不到向Windows窗体应用程序添加网格的方法。我看到了WPF的解决方案,但没有看到windows窗体的解决方案 如果你能给我指出正确的方向,我将不胜感激如何在C#Windows窗体应用程序中动态创建网格,c#,grid,C#,Grid,我有一个项目,需要在Windows窗体应用程序上动态创建一个网格,该应用程序在每个单元格上都有一个单击侦听器 网格需要为12 X 12 我对此进行了反复研究,但找不到向Windows窗体应用程序添加网格的方法。我看到了WPF的解决方案,但没有看到windows窗体的解决方案 如果你能给我指出正确的方向,我将不胜感激 网格将需要与战舰游戏一起使用对于赢的形式,您正在寻找DataGridView。这里有多种方法,这取决于您是想通过js还是代码隐藏来实现 对于Win表单,您正在寻找DataGrid
网格将需要与战舰游戏一起使用对于赢的形式,您正在寻找DataGridView。这里有多种方法,这取决于您是想通过js还是代码隐藏来实现
对于Win表单,您正在寻找DataGridView。这里有多种方法,这取决于您是想通过js还是代码隐藏来实现
以您想要的表格形式尝试以下代码: 编辑:我添加了一种通过新的BattleShipRow类将12行12列放入网格的方法。可以通过多种方式将数据添加到网格,但这会将这些对象的列表绑定到网格 您可能需要调整大小以使显示符合您的要求 最后,我将LoadGridData中定义的battleShipGrid列表作为公共成员。当点击发生时,您可以根据游戏需要修改战列舰行对象列表中的数据
private System.Windows.Forms.DataGridView myNewGrid; // Declare a grid for this form
private List<BattleShipRow> battleShipGrid; // Declare this here so that you can use it later to manipulate the cell contents
private void Form1_Load(object sender, EventArgs e)
{
myNewGrid = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(myNewGrid)).BeginInit();
this.SuspendLayout();
myNewGrid.Parent = this; // You have to set the parent manually so that the grid is displayed on the form
myNewGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
myNewGrid.Location = new System.Drawing.Point(10, 10); // You will need to calculate this postion based on your other controls.
myNewGrid.Name = "myNewGrid";
myNewGrid.Size = new System.Drawing.Size(400, 400); // You said you need the grid to be 12x12. You can change the size here.
myNewGrid.TabIndex = 0;
myNewGrid.ColumnHeadersVisible = false; // You could turn this back on if you wanted, but this hides the headers that would say, "Cell1, Cell2...."
myNewGrid.RowHeadersVisible = false;
myNewGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
myNewGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
myNewGrid.CellClick += MyNewGrid_CellClick; // Set up an event handler for CellClick. You handle this in the MyNewGrid_CellClick method, below
((System.ComponentModel.ISupportInitialize)(myNewGrid)).EndInit();
this.ResumeLayout(false);
myNewGrid.Visible = true;
LoadGridData();
}
public class BattleShipRow
{
public string Cell1 { get; set; }
public string Cell2 { get; set; }
public string Cell3 { get; set; }
public string Cell4 { get; set; }
public string Cell5 { get; set; }
public string Cell6 { get; set; }
public string Cell7 { get; set; }
public string Cell8 { get; set; }
public string Cell9 { get; set; }
public string Cell10 { get; set; }
public string Cell11 { get; set; }
public string Cell12 { get; set; }
}
private void LoadGridData()
{
battleShipGrid = new List<BattleShipRow>();
for (var i = 0; i < 12; i++)
{
battleShipGrid.Add(new BattleShipRow());
}
myNewGrid.DataSource = battleShipGrid;
}
private void MyNewGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
throw new NotImplementedException();
}
private System.Windows.Forms.DataGridView myNewGrid;//为此窗体声明一个网格
私有列表战舰网格;//在这里声明,以便以后可以使用它来操作单元格内容
私有void Form1\u加载(对象发送方、事件参数e)
{
myNewGrid=new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(myNewGrid)).BeginInit();
这个.SuspendLayout();
myNewGrid.Parent=this;//必须手动设置父项,以便在表单上显示网格
myNewGrid.ColumnHeadersHeightSizeMode=System.Windows.Forms.DataGridViews ColumnHeadersHeightSizeMode.AutoSize;
myNewGrid.Location=new System.Drawing.Point(10,10);//您需要根据其他控件计算此位置。
myNewGrid.Name=“myNewGrid”;
myNewGrid.Size=new System.Drawing.Size(400400);//您说过需要将网格设置为12x12。您可以在此处更改大小。
myNewGrid.TabIndex=0;
myNewGrid.ColumnHeadersVisible=false;//如果需要,可以重新启用此选项,但这会隐藏会显示“Cell1,Cell2…”的标题
myNewGrid.RowHeadersVisible=false;
myNewGrid.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.Fill;
myNewGrid.AutoSizeRowsMode=DataGridViewAutoSizeRowsMode.DisplayedCells;
myNewGrid.CellClick+=myNewGrid\u CellClick;//为CellClick设置事件处理程序。您可以在下面的myNewGrid\u CellClick方法中处理此问题
((System.ComponentModel.ISupportInitialize)(myNewGrid)).EndInit();
此选项为.resume布局(false);
myNewGrid.Visible=true;
LoadGridData();
}
公共级战列舰
{
公共字符串Cell1{get;set;}
公共字符串Cell2{get;set;}
公共字符串Cell3{get;set;}
公共字符串Cell4{get;set;}
公共字符串Cell5{get;set;}
公共字符串Cell6{get;set;}
公共字符串Cell7{get;set;}
公共字符串Cell8{get;set;}
公共字符串Cell9{get;set;}
公共字符串Cell10{get;set;}
公共字符串Cell11{get;set;}
公共字符串Cell12{get;set;}
}
私有void LoadGridData()
{
战列舰网格=新列表();
对于(变量i=0;i<12;i++)
{
battleShipGrid.Add(新的BattleShipRow());
}
myNewGrid.DataSource=battleShipGrid;
}
私有void MyNewGrid_CellClick(对象发送者,DataGridViewCellEventArgs e)
{
抛出新的NotImplementedException();
}
以您想要的表格形式尝试以下代码:
编辑:我添加了一种通过新的BattleShipRow类将12行12列放入网格的方法。可以通过多种方式将数据添加到网格,但这会将这些对象的列表绑定到网格
您可能需要调整大小以使显示符合您的要求
最后,我将LoadGridData中定义的battleShipGrid列表作为公共成员。当点击发生时,您可以根据游戏需要修改战列舰行对象列表中的数据
private System.Windows.Forms.DataGridView myNewGrid; // Declare a grid for this form
private List<BattleShipRow> battleShipGrid; // Declare this here so that you can use it later to manipulate the cell contents
private void Form1_Load(object sender, EventArgs e)
{
myNewGrid = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(myNewGrid)).BeginInit();
this.SuspendLayout();
myNewGrid.Parent = this; // You have to set the parent manually so that the grid is displayed on the form
myNewGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
myNewGrid.Location = new System.Drawing.Point(10, 10); // You will need to calculate this postion based on your other controls.
myNewGrid.Name = "myNewGrid";
myNewGrid.Size = new System.Drawing.Size(400, 400); // You said you need the grid to be 12x12. You can change the size here.
myNewGrid.TabIndex = 0;
myNewGrid.ColumnHeadersVisible = false; // You could turn this back on if you wanted, but this hides the headers that would say, "Cell1, Cell2...."
myNewGrid.RowHeadersVisible = false;
myNewGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
myNewGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
myNewGrid.CellClick += MyNewGrid_CellClick; // Set up an event handler for CellClick. You handle this in the MyNewGrid_CellClick method, below
((System.ComponentModel.ISupportInitialize)(myNewGrid)).EndInit();
this.ResumeLayout(false);
myNewGrid.Visible = true;
LoadGridData();
}
public class BattleShipRow
{
public string Cell1 { get; set; }
public string Cell2 { get; set; }
public string Cell3 { get; set; }
public string Cell4 { get; set; }
public string Cell5 { get; set; }
public string Cell6 { get; set; }
public string Cell7 { get; set; }
public string Cell8 { get; set; }
public string Cell9 { get; set; }
public string Cell10 { get; set; }
public string Cell11 { get; set; }
public string Cell12 { get; set; }
}
private void LoadGridData()
{
battleShipGrid = new List<BattleShipRow>();
for (var i = 0; i < 12; i++)
{
battleShipGrid.Add(new BattleShipRow());
}
myNewGrid.DataSource = battleShipGrid;
}
private void MyNewGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
throw new NotImplementedException();
}
private System.Windows.Forms.DataGridView myNewGrid;//为此窗体声明一个网格
私有列表战舰网格;//在这里声明,以便以后可以使用它来操作单元格内容
私有void Form1\u加载(对象发送方、事件参数e)
{
myNewGrid=new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(myNewGrid)).BeginInit();
这个.SuspendLayout();
myNewGrid.Parent=this;//必须手动设置父项,以便在表单上显示网格
myNewGrid.ColumnHeadersHeightSizeMode=System.Windows.Forms.DataGridViews ColumnHeadersHeightSizeMode.AutoSize;
myNewGrid.Location=new System.Drawing.Point(10,10);//您需要根据其他控件计算此位置。
myNewGrid.Name=“myNewGrid”;
myNewGrid.Size=new System.Drawing.Size(400400);//您说过需要将网格设置为12x12。您可以在此处更改大小。
myNewGrid.TabIndex=0;
myNewGrid.ColumnHeadersVisible=false;//如果需要,可以重新启用此选项,但这会隐藏会显示“Cell1,Cell2…”的标题
myNewGrid.RowHeadersVisible=false;
myNewGrid.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.Fill;
myNewGrid.AutoSizeRowsMode=DataGridViewAutoSize