当前位置:网站首页>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;
}
} 

边栏推荐
- What is linting
- 【软件测试】03 -- 软件测试概述
- Rehabilitation type force deduction brush question notes D3
- Initialization of global and static variables
- A brief introduction to heading/pitch/roll and omega/phi/kappa
- 【无标题】
- Volcano resource reservation feature
- Error: "mountvolume.setup failed for volume PVC fault handling
- An article was opened to test the real situation of outsourcing companies
- The problem of Chinese garbled code in the vscode output box can be solved once for life
猜你喜欢

并发编程 — 死锁排查及处理

SD_CMD_SEND_SHIFT_REGISTER

All English in the code

window navicat连接阿里云服务器mysql步骤及常见问题

LSA Type Explanation - detailed explanation of lsa-2 (type II LSA network LSA) and lsa-3 (type III LSA network Summary LSA)

Marvell 88e1515 PHY loopback mode test

C语言数组专题训练

MySQL (UDF authorization)

Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui

【软件测试】04 -- 软件测试与软件开发
随机推荐
[OBS] x264 Code: "buffer_size“
【软件测试】06 -- 软件测试的基本流程
Orin 两种刷机方式
Preemption of CFS scheduling
Mid 2022 documentary -- the experience of an ordinary person
Vscode editor
Ros2 - node (VII)
LSA Type Explanation - detailed explanation of lsa-2 (type II LSA network LSA) and lsa-3 (type III LSA network Summary LSA)
Log4qt usage of logbase in QT project
Unity UGUI不同的UI面板或者UI之间如何进行坐标匹配和变换
UTC, GPS time and Tai
Rehabilitation type force deduction brush question notes D3
Mutual transformation between two-dimensional array and sparse array (sparse matrix)
逻辑结构与物理结构
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
Orin installs CUDA environment
Volcano resource reservation feature
ROS2——功能包(六)
inux摄像头(mipi接口)简要说明
在本地搭建一个微服务集群环境,学习自动化部署