当前位置:网站首页>C#学习笔记

C#学习笔记

2022-07-05 07:05:00 mQ_1848148016

目录

改变窗体大小

获取窗口大小

获取按钮位置

随机更新标签位置

checkBox的使用

radiobutton的使用

SerialPort的使用

PictureBox的使用


改变窗体大小

//通过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("女");
}

yH5BAAAAAAALAAAAAAOAA4AAAIMhI+py+0Po5y02qsKADs=wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

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;
    }
                
}

 

 

 

原网站

版权声明
本文为[mQ_1848148016]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_65592409/article/details/125590861