C# 单选按钮事件
这是我的问题 我有几个单选按钮。如果我在表单创建文本框上单击第一个单选按钮,如果单击第二个-创建第二个文本框,如果我再次单击第一个单选按钮,再次单击一个文本框,是否可能 请给我个主意 如果没有属性可见请尝试以下方法(这只是其中之一):C# 单选按钮事件,c#,winforms,C#,Winforms,这是我的问题 我有几个单选按钮。如果我在表单创建文本框上单击第一个单选按钮,如果单击第二个-创建第二个文本框,如果我再次单击第一个单选按钮,再次单击一个文本框,是否可能 请给我个主意 如果没有属性可见请尝试以下方法(这只是其中之一): 这里动态创建和销毁控件没有多大意义。确保位置、大小和标签顺序正确,这只是一件令人头疼的事情。如果您喜欢以下选项,只需使文本框可见即可: private void radioButton2_CheckedChanged(object sender, Even
这里动态创建和销毁控件没有多大意义。确保位置、大小和标签顺序正确,这只是一件令人头疼的事情。如果您喜欢以下选项,只需使文本框可见即可:
private void radioButton2_CheckedChanged(object sender, EventArgs e) {
textBox1.Visible = radioButton2.Checked;
}
在设计器中将textbox的Visible属性设置为False。请仅查看
foreach
和voidtextbox
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
class MyForm : Form
{
private const int
HeightTextBox = 40, WidthTextBox = 25, //размер textboxes
DistanceBetweenTexBoxHeight = 25, DistanceBetweenTexboxWigth = 25; //растояние между ними
private int DimentionalTextBox = 3;
private const int
RadioButtonNumbers = 3, // количество радио кнопок
DistanceBetweenRadiobutton = 50,
RadioButtonFirstGroupStartPositionX = 5,
RadioButtonSecondGroupStartPositionX = 0,
RadioButtonFirstGroupStartPositionY = 0,
RadioButtonSecondGroupStartPositionY = 0,
RadioButtonSize = 25;
public MyForm()
{
//Size of window
ClientSize = new System.Drawing.Size(7 * HeightTextBox + 8 * DistanceBetweenTexBoxHeight,
7 * WidthTextBox + 8 * DistanceBetweenTexboxWigth);
//Create RaioButton
int x = RadioButtonFirstGroupStartPositionX;
int y;
RadioButton[] DimRadioButtons = new RadioButton[RadioButtonNumbers];
for (int i = 0; i < RadioButtonNumbers; i++)
{
DimRadioButtons[i] = new RadioButton();
DimRadioButtons[i].Name = "RadioButton" + (i + 2);
DimRadioButtons[i].Text = Convert.ToString(i + 2);
DimRadioButtons[i].SetBounds(x, RadioButtonFirstGroupStartPositionY, RadioButtonSize, RadioButtonSize);
x += DistanceBetweenRadiobutton;
Controls.Add(DimRadioButtons[i]);
}
//Watch dimention
// And catch even click on RadioButton
foreach (var a in this.Controls)
{
if (a is RadioButton)
{
if (((RadioButton)a).Checked)
{
DimentionalTextBox = Convert.ToInt16(((RadioButton)a).Text);
((RadioButton)a).Click += new EventHandler(this.TextBoxes);
}
}
}
}
// Create-Delete TextBoxes
private void TextBoxes(object sender, EventArgs e)
{
RadioButton rb_click = (RadioButton)sender;
int x = RadioButtonFirstGroupStartPositionX;
int y = 30;
int dim = Convert.ToInt16(rb_click.Text);
TextBox[,] MatrixTextBoxes = new TextBox[dim, dim];
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
MatrixTextBoxes[i, j] = new TextBox();
MatrixTextBoxes[i, j].Top = rb_click.Top;
MatrixTextBoxes[i, j].Name = "MatrixTextBox" + i + j;
MatrixTextBoxes[i, j].Text = i + " " + j;
MatrixTextBoxes[i, j].SetBounds(x, y, WidthTextBox, HeightTextBox);
x += DistanceBetweenTexboxWigth;
this.Controls.Add(MatrixTextBoxes[i, j]);
MatrixTextBoxes[i, j].Show();
}
y += DistanceBetweenTexBoxHeight;
x = RadioButtonFirstGroupStartPositionX;
}
}
}
class MyClassMain : MyForm
{
public static void Main()
{
Application.Run(new MyClassMain());
}
}
使用系统;
使用系统文本;
使用System.Windows.Forms;
使用系统图;
类MyForm:Form
{
私人常数
高度文本框=40,宽度文本框=25,//
距离介于nTexBoxHeight=25,距离介于nTexBoxWigth=25;//аажжжжж
私有int-dimensionaltextbox=3;
私人常数
RadioButtonNumber=3,//
RadioButton之间的距离=50,
RadioButtonFirstGroupStartPositionX=5,
RadioButtonSecondGroupStartPositionX=0,
RadioButtonFirstGroupStartPositionY=0,
RadioButtonSecondGroupStartPositionY=0,
RadioButtonSize=25;
公共MyForm()
{
//窗口大小
ClientSize=新系统.Drawing.Size(7*高度文本框+8*之间的距离X框高度,
7*宽度文本框+8*文本框之间的距离);
//创建RaioButton
int x=RadioButtonFirstGroupStartPositionX;
int-y;
RadioButton[]DimRadioButtons=新的RadioButton[RadioButtonNumber];
对于(int i=0;i
是否需要一个在单击单选按钮时移动并刷新的文本框?否。。。如果我点击RadioButton(第二个),我想添加新的文本框,如果我点击RadioButton(第一个),我想删除他。如果这是帮助的话。我可以写我所有的代码。我同意,但他确实要求回答,但没有使用Visible Property It的交易。但问题是我有一个文本框数组,如果我使用“可见”。。。我会头痛的位置。。。但是如果没有“可见”,在“for”中所有位置、大小和标签都处于良好状态。为什么改变可见性会改变您改变位置和大小的能力?请参阅。它的边框与每个单选按钮共享文本框。我无法想象一个循环会描述它的位置。你说它不工作是什么意思?它怎么不起作用?是否创建了单选按钮并将checkedchanged事件附加到该单选按钮?如果默认选中该按钮,并且用户单击另一个按钮,则Dispose()可能会出现问题。Check changed会被调用,因为它没有被选中,所以它会尝试调用t.Dispose(),此时它为null,并抛出一个异常。当我创建一个新项目时,它会工作。但是如果我用这个方法。我对文本框的名称会有很多问题,因为它们是数组的一部分。但是,尽管如此,谢谢=)看起来你做得很糟糕,而且正在承受后果……这里要吸取的教训
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
class MyForm : Form
{
private const int
HeightTextBox = 40, WidthTextBox = 25, //размер textboxes
DistanceBetweenTexBoxHeight = 25, DistanceBetweenTexboxWigth = 25; //растояние между ними
private int DimentionalTextBox = 3;
private const int
RadioButtonNumbers = 3, // количество радио кнопок
DistanceBetweenRadiobutton = 50,
RadioButtonFirstGroupStartPositionX = 5,
RadioButtonSecondGroupStartPositionX = 0,
RadioButtonFirstGroupStartPositionY = 0,
RadioButtonSecondGroupStartPositionY = 0,
RadioButtonSize = 25;
public MyForm()
{
//Size of window
ClientSize = new System.Drawing.Size(7 * HeightTextBox + 8 * DistanceBetweenTexBoxHeight,
7 * WidthTextBox + 8 * DistanceBetweenTexboxWigth);
//Create RaioButton
int x = RadioButtonFirstGroupStartPositionX;
int y;
RadioButton[] DimRadioButtons = new RadioButton[RadioButtonNumbers];
for (int i = 0; i < RadioButtonNumbers; i++)
{
DimRadioButtons[i] = new RadioButton();
DimRadioButtons[i].Name = "RadioButton" + (i + 2);
DimRadioButtons[i].Text = Convert.ToString(i + 2);
DimRadioButtons[i].SetBounds(x, RadioButtonFirstGroupStartPositionY, RadioButtonSize, RadioButtonSize);
x += DistanceBetweenRadiobutton;
Controls.Add(DimRadioButtons[i]);
}
//Watch dimention
// And catch even click on RadioButton
foreach (var a in this.Controls)
{
if (a is RadioButton)
{
if (((RadioButton)a).Checked)
{
DimentionalTextBox = Convert.ToInt16(((RadioButton)a).Text);
((RadioButton)a).Click += new EventHandler(this.TextBoxes);
}
}
}
}
// Create-Delete TextBoxes
private void TextBoxes(object sender, EventArgs e)
{
RadioButton rb_click = (RadioButton)sender;
int x = RadioButtonFirstGroupStartPositionX;
int y = 30;
int dim = Convert.ToInt16(rb_click.Text);
TextBox[,] MatrixTextBoxes = new TextBox[dim, dim];
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
MatrixTextBoxes[i, j] = new TextBox();
MatrixTextBoxes[i, j].Top = rb_click.Top;
MatrixTextBoxes[i, j].Name = "MatrixTextBox" + i + j;
MatrixTextBoxes[i, j].Text = i + " " + j;
MatrixTextBoxes[i, j].SetBounds(x, y, WidthTextBox, HeightTextBox);
x += DistanceBetweenTexboxWigth;
this.Controls.Add(MatrixTextBoxes[i, j]);
MatrixTextBoxes[i, j].Show();
}
y += DistanceBetweenTexBoxHeight;
x = RadioButtonFirstGroupStartPositionX;
}
}
}
class MyClassMain : MyForm
{
public static void Main()
{
Application.Run(new MyClassMain());
}
}