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

边栏推荐
- UIO driven framework
- 【软件测试】06 -- 软件测试的基本流程
- Rehabilitation type force deduction brush question notes D3
- 解读最早的草图-图像翻译工作SketchyGAN
- ROS2——topic话题(八)
- Mutual transformation between two-dimensional array and sparse array (sparse matrix)
- 【obs】x264编码:“buffer_size“
- . Net core stepping on the pit practice
- 网易To B,柔外刚中
- An article was opened to test the real situation of outsourcing companies
猜你喜欢
![[algorithm post interview] interview questions of a small factory](/img/62/6e330b1eba38f2dc67b21a10f0e2a8.jpg)
[algorithm post interview] interview questions of a small factory

. Net core stepping on the pit practice

All English in the code

PHY drive commissioning - phy controller drive (II)

ROS2——常用命令行(四)

SRE核心体系了解

使用paping工具进行tcp端口连通性检测

Some classic recursion problems

Mipi interface, DVP interface and CSI interface of camera

Skywalking all
随机推荐
Instruction execution time
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
【无标题】
Architecture
Spinningup drawing curve
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
.net core踩坑实践
Marvell 88e1515 PHY loopback mode test
【软件测试】06 -- 软件测试的基本流程
【软件测试】05 -- 软件测试的原则
new和malloc的区别
cgroup_ memcg
Rehabilitation type force deduction brush question notes D3
能量守恒和打造能量缺口
PowerManagerService(一)— 初始化
Orin 安装CUDA环境
Preemption of CFS scheduling
IPage can display data normally, but total is always equal to 0
Huawei bracelet, how to add medicine reminder?
SOC_SD_DATA_FSM