当前位置:网站首页>C#学习笔记
C#学习笔记
2022-07-05 07:05:00 【mQ_1848148016】
目录
改变窗体大小
//通过button1_click点击改变
private void button1_Click(object sender, EventArgs e)
{
//this.ClientSize = new System.Drawing.Size(300, 400);
//this.ClientSize = new Size(300, 400);//用户区大小
this.Size = new Size(300, 400);//整个窗口大小
this.button1.Size = new System.Drawing.Size(160, 150);//button1大小
}
获取窗口大小
private void button1_Click(object sender, EventArgs e)
{
int w = this.Size.Width;
int h = this.Size.Height;
MessageBox.Show("宽:" + w + " 高:" + h);
}
获取按钮位置
private void button1_Click(object sender, EventArgs e)
{
//int loc_x = this.button1.Location.X; //也可提前获取
//int loc_y = this.button1.Location.Y;
this.button1.Location = new Point(150,50);//指定按钮位置
MessageBox.Show("Button1_X:"+ button1.Location.X + " Button1_Y:"+ button1.Location.Y);
}
随机更新标签位置
private void label1_Click(object sender, EventArgs e)
{
Random random = new Random();
int n =random.Next(0,300); //随机范围0-300
this.label1.Location = new Point(n,n);//随机指定位置
}
checkBox的使用
private void button1_Click(object sender, EventArgs e)
{
String str = "您购买了:";
int sum = 0;
if (this.checkBox1.Checked == true)
{
str = str + "苹果";
sum += 3;
}
if (this.checkBox2.Checked == true)
{
str = str + ",火龙果";
sum += 5;
}
if (this.checkBox3.Checked == true)
{
str = str + ",西瓜";
sum += 10;
}
MessageBox.Show(str + sum + "元");
}
radiobutton的使用
private void button2_Click(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
Console.WriteLine("男");
else
Console.WriteLine("女");
}
ComboBox的使用
private void button1_Click(object sender, EventArgs e)
{
String str = this.comboBox1.SelectedItem.ToString();
Console.WriteLine(str);//输出选中项的内容
int a = this.comboBox1.SelectedIndex;
MessageBox.Show(this.comboBox1.Items[a].ToString());//输出选中项的内容
}
SerialPort的使用
发送数据
private void button1_Click(object sender, EventArgs e)
{
// 发送数据
if(serialPort1.IsOpen == true)//检测端口是否打开
{
String str = textBox1.Text; //将文本内容发送到串口端口
serialPort1.WriteLine(str);
}
}
接收数据
// 当串口接收到数据内容后,该事件触发并执行下面方法
// 该方法是系统另外开的子线程运行
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
String str = serialPort1.ReadLine(); //读取每行数据
serialPort1.DiscardInBuffer();// 拿到数据清除缓存
textBox2.Text = str + textBox2.Text;
}
具体串口通信文章:https://blog.csdn.net/m0_65592409/article/details/125213063?spm=1001.2014.3001.5501
PictureBox的使用
添加图片资源
导入图片
private int a = 0;
//点击button切换图片
private void button1_Click(object sender, EventArgs e)
{
if(a == 0)
{
this.pictureBox1.Image = global::WinForms_image.Properties.Resources.p2;
a = 1;
}
else if(a == 1)
{
this.pictureBox1.Image = global::WinForms_image.Properties.Resources.p3;
a=2;
}
else if(a ==2)
{
this.pictureBox1.Image = global::WinForms_image.Properties.Resources.p1;
a = 0;
}
}
边栏推荐
- [untitled]
- PHY驱动调试之 --- PHY控制器驱动(二)
- U-Boot初始化及工作流程分析
- 1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
- ROS2——常用命令行(四)
- 在本地搭建一个微服务集群环境,学习自动化部署
- Ros2 - configuration development environment (V)
- The problem of Chinese garbled code in the vscode output box can be solved once for life
- npm install -g/--save/--save-dev的区别
- Ros2 - ros2 vs. ros1 (II)
猜你喜欢
随机推荐
在本地搭建一个微服务集群环境,学习自动化部署
ROS2——功能包(六)
Binary search (half search)
Ros2 - workspace (V)
ROS2——初识ROS2(一)
Vscode editor
Technical conference arrangement
Unity ugui how to match and transform coordinates between different UI panels or uis
Vscode configures the typera editor for MD
Cookie、Session、JWT、token四者间的区别与联系
Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
Utf8 encoding
NVM Downloading npm version 6.7.0... Error
new和malloc的区别
ROS2——node节点(七)
vim
【软件测试】03 -- 软件测试概述
三体目标管理笔记
Ros2 - common command line (IV)
Ret2xx---- common CTF template proposition in PWN