蓝色魔法

蓝色的魔法,永恒的世界.

C#开发PPC软件的一些常用代码.

八月5

动态添加按钮,并且添加按钮点击事件.

Button btn = new Button();  //创建一个新按钮并付赋予变量btn.
btn.Location = new Point(20,20); //给新按钮创建一个新位置.
btn.Size = new Size(20, 20); //给按钮创建一个新大小.
this.Controls.Add(btn); //把btn显示出来. 到这里为止动态添加按钮成功.
btn.Click += new System.EventHandler(btn_click);  //添加按钮点击事件      
private void btn_click(object sender, System.EventArgs e)
{
     Button b1 = (Button)sender; //将触发此事件的对象转换为该Button对象
     MessageBox.Show(“” + b1.Name); //点击后用信息框显示出被点击按钮的名字.
}

 

获取绝对路径(String)

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

(注意此段代码最后不带”/”请自行添加.)

 

打开图片,并用PictureBox显示(在这之前需要创建一个名为pictureBox1的PictureBox控件)
string path; //声明path变量为字符串
OpenFileDialog openFileDialog1 = new OpenFileDialog();  //新建一个打开文件
openFileDialog1.InitialDirectory = “My Pictures”; //默认文件夹为My Picture
if (openFileDialog1.ShowDialog() == DialogResult.OK) //如果打开文件成功.
{
  path = openFileDialog1.FileName; //path=打开文件的地址.
  Bitmap bmptest = new Bitmap(path); //新建一个Bitmap.
  pictureBox1.Image = bmptest; //把新建的Bitmap设置为PictureBox的图像.
}

Email will not be published

Website example

Your Comment:

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word