http://msdn.microsoft.com/library/bb383962.aspx
1. 基于字符串数组创建一组单选按钮 (Visual C#)
private void button1_Click(object sender, EventArgs e)
{
string[] stringArray = new string[3];
stringArray[0] = "yes";
stringArray[1] = "no";
stringArray[2] = "maybe";
System.Windows.Forms.RadioButton[] radioButtons = new System.Windows.Forms.RadioButton[3];
for (int i = 0; i < 3; ++i)
{
radioButtons[i] = new RadioButton();
radioButtons[i].Text = stringArray[i];
radioButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
this.Controls.Add(radioButtons[i]);
}
}
public Form2()
{
//
// Required for Windows Form Designer support.
//
InitializeComponent();
// Initialize the user-defined button,
// including defining handler for Click message,
// location and size.
myButtonObject myButton = new myButtonObject();
EventHandler myHandler = new EventHandler(myButton_Click);
myButton.Click += myHandler;
myButton.Location = new System.Drawing.Point(20, 20);
myButton.Size = new System.Drawing.Size(101, 101);
this.Controls.Add(myButton);
}
public class myButtonObject : UserControl
{
// Draw the new button.
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Pen myPen = new Pen(Color.Black);
// Draw the button in the form of a circle
graphics.DrawEllipse(myPen, 0, 0, 100, 100);
myPen.Dispose();
}
}
// Handler for the click message.
void myButton_Click(Object sender, System.EventArgs e)
{
MessageBox.Show("Click");
}
this.label1.Text = "Time " + DateTime.Now.ToLongTimeString();
Random randomColor = new Random();
this.label1.ForeColor = Color.FromArgb(randomColor.Next(0, 256),
randomColor.Next(0, 256), randomColor.Next(0, 256));
int anInteger; anInteger = Convert.ToInt32(textBox1.Text); anInteger = int.Parse(textBox1.Text);
以下情况可能会导致异常:
-
文本转换为一个太大或太小的数字以致不能存储为 int。
-
该文本不能表示数字。
private void button1_Click(object sender, EventArgs e) { textBox1.Text = "Hello World"; textBox1.Select(6, 5); MessageBox.Show(textBox1.SelectedText); }
此示例将一个由三个使用不同字体样式(粗体、斜体和带下划线)的单词组成的句子写入到现有 RichTextBox 控件中。
richTextBox1.Rtf = @"{\rtf1\ansi This text is in \b bold\b0, " + @"this is in \i italics\i0, " + @"and this is \ul underlined\ul0.}";
向 RichTextBox 控件加载文本
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.Filter = "Text Files|*.txt";
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
richTextBox1.LoadFile(openFile1.FileName,
RichTextBoxStreamType.PlainText);