您的位置:首页 > 编程语言 > C#

C#基础语法第二天

2018-12-25 18:50 155 查看

C#基础语法第二天

1,重点

所有的属性赋值代码全部写在事件中。

2,C#基础语法:

(一)预定义的15种属性类型

整数

int:32位有符号整数;uint:32位无符号整数;
short:16位有符号整数;ushort:16位无符号整数;
long:64位有符号整数;ulong:64位无符号整数;
byte:8位无符号整数;sbyte:8位有符号整数;

小数

double:双精度 double a=200.33;
float:单精度 float b=200.4f;
decimal:精度型 decimal c=202.3m;

字符

char 单引号中有且只有一个 char d=‘a’;

字符串

string 双引号里面可写可不写 string s="";

布尔类型

bool (true/false)

对象类型

object object o= ;等号后面以上类型都可装

(二)

排除第一种,等号后面试着敲空格,如果出现智能提示,直接敲小数点,选择一个合适的选项分号结束。
​ this.WindowState = FormWindowState.Maximized

(三)

遇到特殊类型(color),等号后面直接使用属性类型单词点,选择一个分号结束。

this.BackColor = Color.Plum;

(四)

排除前面的所有,符号后面先"new",敲个空格,出现智能提示直接一对小括号分号结束,删掉前半个括号,再添加上,针对括号中的每一个属性继续使用这四种方法。

注意

1,首先属性赋值的本质是让等号两边的属性类型一致即可;
2,属性赋值一般是给可以设置的属性赋值,只读属性不能赋值;
bool s = this.Focused;
3,方法:遇到方法,直接一对小括号分号结束,括号中的参数和第四种属性赋值方式一样。
this.PointToScreen(new Point(20,30));
ForeColor:前景色(文字颜色)
BackColor:背景色(盒子颜色)
TextLength:所有文本的长度
SelectionLength:选中文字的长度
4,类:是对象的抽象化,也是由属性、方法、事件构成;类不能直接使用其属性、方法、事件,如需要使用这个类,必须先将这个类进行实例化成对象
注意:实例化的本质就是将这个类转换成一个新的对象。

3,滚动字幕案例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
//设置大小
this.Size = new Size(400,500);
//设置位置居中
this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.Bounds.Height / 2 -this.Height / 2;
//修改文字信息
this.Text = "滚动字幕";
//设置背景颜色
this.BackColor = Color.Black;
//设置textbox的里面的文字大小
textBox1.Width = this.Width;
textBox1.Font = new Font("宋体",20);
//设置textbox的文字居中对齐
textBox1.TextAlign = HorizontalAlignment.Center;
//设置textbox的位置在form的0,0点
textBox1.Location = new Point(0,0);
//设置textbox的背景是黑色
textBox1.BackColor = Color.Black;
//设置textbox的里面文字颜色为白色
textBox1.ForeColor = Color.White;
//设置textbox的光标位置在所有文字的索引位置
//0代表所有文字的最前面
//textbox1.TextLength代表文本框中的文本的长度
textBox1.SelectionStart = textBox1.TextLength;
//设置文本框的位置在最下面
textBox1.Top = this.Height;
//将textbox1设置成只读的
textBox1.ReadOnly = true;
//去掉边框
textBox1.BorderStyle = BorderStyle.None;
//导入音乐工具
//实例化一个音乐播放者对象
SoundPlayer sound = new SoundPlayer();
sound.SoundLocation = "../../music/shengpizi.wav";
//音乐播放
//sound.Play();
//音乐循环播放,可把上一行代码省略
sound.PlayLooping();
}

private void timer1_Tick(object sender, EventArgs e)
{
//设置文本框的top,每隔0.1s,减少2
textBox1.Top = textBox1.Top - 2;
//判断textbox完全移出了form
if (textBox1.Top<=-textBox1.Height) {
textBox1.Top = this.Height;
}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: