button控件和文本控件

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]);
            }
        }


2. 创建非矩形按钮 (Visual C#)

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");
}

在 Windows 窗体上显示文本

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));

将 TextBox 控件中的文本转换成整数

int anInteger;
anInteger = Convert.ToInt32(textBox1.Text);
anInteger = int.Parse(textBox1.Text);

以下情况可能会导致异常:

  • 文本转换为一个太大或太小的数字以致不能存储为 int

  • 该文本不能表示数字。

在 TextBox 控件中设置选定文本

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = "Hello World";
    textBox1.Select(6, 5);
    MessageBox.Show(textBox1.SelectedText);
}

设置 RichTextBox 控件中的字符格式

此示例将一个由三个使用不同字体样式(粗体、斜体和带下划线)的单词组成的句子写入到现有 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值