当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
window navicat连接阿里云服务器mysql步骤及常见问题
Special training of C language array
Rehabilitation type force deduction brush question notes D1
Ros2 - configuration development environment (V)
Database mysql all
Ros2 - Service Service (IX)
Configuration method and configuration file of SolidWorks GB profile library
Ros2 - workspace (V)
数学分析_笔记_第8章:重积分
Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"
随机推荐
ROS2——ROS2对比ROS1(二)
小米笔试真题一
Marvell 88E1515 PHY loopback模式测试
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
程序中的负数存储及类型转换
使用paping工具进行tcp端口连通性检测
代码中的英语全部
testing framework
Ros2 - configuration development environment (V)
Database mysql all
Huawei bracelet, how to add medicine reminder?
Ros2 - install ros2 (III)
ROS2——配置开发环境(五)
Build a microservice cluster environment locally and learn to deploy automatically
Lexin interview process
[algorithm post interview] interview questions of a small factory
IPage能正常显示数据,但是total一直等于0
Orin installs CUDA environment
Unity 之 ExecuteAlways正在取代ExecuteInEditMode
Sre core system understanding