C#WinForm基础编程(二)

简介: C#WinForm基础编程

C#WinForm基础编程(一)https://developer.aliyun.com/article/1433699


四、数组的删除

示例4:从数组中删除给出的数字

int[] arr = new int[] {8,6,9,5,73,11,56,87 };
Console.WriteLine("请输入你要删除的元素");
int num = int.Parse(Console.ReadLine());
int index = -1;
for(int i = 0; i < arr.Length; i++)
{
    if (num == arr[i])//查找要删除的元素
    {
        index = i;
        break;
    }
}
if (index != -1)
{
    for(int j = index; j < arr.Length-1; j++)//注意j是要小于arr.length-1;
    {
        arr[j] = arr[j + 1];//将从删除位置开始的数组元素依次前移
    }
    arr[arr.Length - 1] = 0;//将数组最后一个元素置成0
}else
{
    Console.WriteLine("没有你要删除的元素");
}
 ///
for(int i = 0; i < arr.Length; i++)
{
    Console.Write(arr[i]+"\t");
}

示例5:在顺序数组中插入一个数,要求插入后仍然保持数组的升序排列。

int[] arr = new int[] { 10, 20, 30, 40, 50, 60 ,0};
Console.WriteLine("请输入要插入的数字");
int num = int.Parse(Console.ReadLine());
int index = arr.Length-1 ;
for(int i = 0; i < arr.Length; i++)
{
    if (arr[i] > num)
    {
         index = i;
         break;
    }
}
for(int j = arr.Length - 2; j >= index; j--)
{
   arr[j + 1] = arr[j];
}
arr[index] = num;
for (int i = 0; i < arr.Length; i++)
{
    Console.Write(arr[i] + "\t");
}

视频课:https://edu.51cto.com/course/20906.html

第五章:类和对象

什么是对象

描述一个对象:

姓名:欧阳锋

性别:男

家住:深圳

。。。。属性;

弹吉他;

跳街舞;

。。。。能力

欧阳锋外传——封装

对象:是组成世界万物的具体个体
类:是有相同特点的个体的抽象概念
第一节:创建类和对象
创建类:
class Person
{
    public string name;
    public string sex;
    public int age;
    public void intro()
    {
        Console.WriteLine("我是:"+name+"性别:"+sex+",今年"+age+"岁了");
    }
}
创建对象:
Person p = new Person();
p.name = "令狐冲";
p.sex = "男";
p.age = 8;
Console.WriteLine(p.name);
p.intro();

在图形界面造人

string name = txtName.Text;
string sex = txtSex.Text;
int age = int.Parse(txtAge.Text);
Person per = new Person();
per.name = name;
per.sex = sex;
per.age = age;
per.intro();
构造方法:

构造方法是创建该类对象的方法,如果类中没有构造方法,系统会给一个默认(无参数)的构造方法

构造方法的结构是:public 类名(){}

一个类中一般需要有两个构造方法,一个是空参数(默认)的构造方法,一个是带参数的构造方法。

public Person(){  }
   public Person(string name, string sex, int age)
   {
       this.name = name;//this代表当前类的对象
       this.sex = sex;
       this.age = age;
   }
Person p = new Person("张无忌","男",12);
 Console.WriteLine(p.name);
 p.intro();

第二节:定义一般方法

方法的种类

方法分类:

一、无参数无返回值
public void intro()
 {
       Console.WriteLine("我是:"+name+"性别:"+sex+",今年"+age+"岁了");
 }
二、有参数无返回值
public void playGame(int level)
{
    if (level > 10)
    {
        Console.WriteLine("打败boss");
    }else
    {
        Console.WriteLine("game over");
    }  
}
三、无参数有返回值
public int getRandom()
{
     Random ran = new Random();
     return ran.Next(0,100);            
}

调用:

Student stu = new Student("王宗月","男",20);
 stu.intro();
 stu.playGame(50);
 int num=stu.getRandom();
 Console.WriteLine("随机数是:"+num);
 Console.Read();
四、有参数有返回值
public double plus(double num1,double num2)
{
    return num1 + num2;
}

方法调用:

Person per = new Person();
 double n1 = double.Parse(txtNum1.Text);
 double n2 = double.Parse(txtNum2.Text);
 double result=per.plus(n1,n2);
 MessageBox.Show(""+result,"计算结果是:");

五、类的完整代码:

class Person
{
    public string name;
    public string sex;
    public int age;
    public void intro()//无参数无返回值
    {
        Console.WriteLine("我是:"+name+"性别:"+sex+",今年"+age+"岁了");
    }
    public void playGame(int level)//无参数有返回值
    {
        if (level > 10)
        {
            Console.WriteLine("打败boss");
        }else
        {
            Console.WriteLine("game over");
        }  
    }
    public double plus(double num1,double num2)//有参数有返回值
    {
        return num1 + num2;
    }
    public string say()//无参数有返回值
    {
        return "欢迎你来到我家";
    }
    public Person(){ }//默认构造方法
    public Person(string name, string sex, int age)//带参数的构造方法
    {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
}

练习:1)编写狗类的代码并创建对象进行测试;2)编写手机类代码并测试;

class Dog
{
    public string name;
  public string sex;
  public int age;
  public string color;
    public Dog() { }
    public Dog(string name,string sex,int age,string color)
    {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.color = color;
    }
    public void eat(string food)
    {
        Console.WriteLine("狗狗"+name+"在吃"+food);
    }
    public string seeDoor(string who)
    {
        if (who == "生人")
        {
            return "旺旺";
        }
        else
        {
            return "摇尾巴";
        }
    }
}

测试代码:

Dog dog = new Dog("花花","母",2,"白色");
dog.eat("肉骨头");
string res=dog.seeDoor("生人");
Console.WriteLine(res);

电话的类代码:

class Phone
{
  public string brand;
    public double price;
    public string type;
    public Phone() { }
    public Phone(string brand,double price,string type)
    {
        this.brand = brand;
        this.price = price;
        this.type = type;
    }
  public void listenMusic()
    {
        Console.WriteLine(brand+type+"手机在播放音乐");
    }
    public double comp(double num1,double num2,string opt)
    {
        if (opt == "+") return num1 + num2;
        if (opt == "-") return num1 - num2;
        if (opt == "*") return num1 * num2;
        if (opt == "/") return num1 / num2;
        return 0;
    }
    public void call(string phoneCode)
    {
        Console.WriteLine("给"+phoneCode+"号码打电话");
    }
    public string reportTime()
    {
        return DateTime.Now.ToString();
    }
}

手机测试代码:

Phone p = new Phone("华为",4560,"荣耀T-5");
p.listenMusic();
Console.WriteLine(p.reportTime());
p.call("13526985241");
Console.WriteLine(p.comp(6.5,5.9,"*"));

第三节:系统常用类ArrayList (集合)

ArrayList list = new ArrayList();
        list.Add(new Person("任我行", "男", 58));//向集合中添加元素
        list.Add(new Person("任盈盈", "女", 18));
        list.Add(new Person("令狐冲", "男", 18));
        list.Add(new Person("东方不败", "不男不女", 18));
        list.Add(new Person("岳不群", "不男不女", 48));
    Console.writeLine(list.Count);//打印集合中元素的数量
        list.RemoveAt(3);//删除索引位置为3 的元素
        list.Insert(2,new Person("鹿晗","女",23));//在指定索引位置插入元素
        foreach (Person per in list)
        {
            Console.WriteLine(per.name+":"+per.sex+":"+per.age);
        }
        list.Clear();//清空集合中所有的元素

第四节:ListBox和combobox控件的使用

两者都有集合items属性,是用来装内容的,其本质是ArrayList集合,添加ListBox组件将items添加名字等字符串,实现如下效果,点击按钮将ListBox中的所有项添加到comboBox中。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Eoxh0ier-1672665634021)(assets\1547022928672.png)]

1、单个添加的代码

欢迎按钮代码:

string name = txtName.Text;
MessageBox.Show("欢迎你:"+name,"提示");

将文本框内容单个添加到LIstBox中的按钮的代码:

string name = txtName.Text;
listNames.Items.Add(name);
txtName.Text = "";

将ListBox中内容单个添加到comboBox中的代码:

string name = (string)listNames.SelectedItem;
cobName.Items.Add(name);
cobName.SelectedIndex = 0;
listNames.Items.RemoveAt(listNames.SelectedIndex);
2、将ListBox中选中的所有项一起移动到comboBox中的代码
foreach(string item in list.Items)//将ListBox集合中所有的元素遍历
{
    cob.Items.Add(item);//将遍历的每一个集合元素添加到comboBox集合中
}
cob.SelectedIndex = 0;//让comboBox集合中默认选中第一个元素
list.Items.Clear();//清空ListBox集合中所有元素

改变ListBox中的selectionMode属性为multiSimple让ListBox中的选项可以多选。

foreach(string sel in list.SelectedItems)//遍历ListBox中选中元素的集合
{
    cob.Items.Add(sel);//将选中元素添加到comboBox中
}
cob.SelectedIndex = 0;
while (list.SelectedItems.Count > 0)
{
    list.Items.Remove(list.SelectedItems[0]);//从ListBox大集合Items中删除选中集合中的每一个元素,因为删除后集合会自动前移,所以只需要删除第一个元素即可。
}

第三节:类的静态属性和方法—static

Person per=new Person();

per.intro();

类的属性或方法一旦定义了静态:static,该变量就不能被实例对象访问,只能通过类名直接访问。

示例1:

class Student
    {
        public string name;
        public string sex;
        public int age;
        public static string className;
    public Student(string name,string sex,int age)
    {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public void intro()
    {
        Console.WriteLine("我是:"+name+",性别"+sex+",今年"+age+"岁"+"在"+className);
    }
    public static void testc()
    {
        Console.WriteLine("nihao helloworld");
    }
}

调用的时候

Student stu3 = new Student("周钦荣", "男", 20);
        //stu3.className = "java2班";//这样调用是不行的
        Student.className="java3班";
        Student.testc();
类的ToString()方法:
override
public string ToString()
{
     return "我是:"+name + ":性别" + sex + ":年龄" + age;
}

控制台输出:窗体程序需要在项目中右键—【属性】—【输出类型】—【控制台应用程序】

string name = txtName.Text;
int age = int.Parse(txtAge.Text);
string sex = radMan.Checked ? "男" : "女";
Person per = new Person(name, sex, age);
Console.WriteLine(per);


C#WinForm基础编程(三)https://developer.aliyun.com/article/1433703

目录
相关文章
|
1月前
|
C#
24. C# 编程:用户设定敌人初始血值的实现
24. C# 编程:用户设定敌人初始血值的实现
22 0
|
2月前
|
SQL 数据库连接 应用服务中间件
C#WinForm基础编程(三)
C#WinForm基础编程
79 0
|
6天前
|
存储 安全 网络安全
C#编程的安全性与加密技术
【4月更文挑战第21天】C#在.NET框架支持下,以其面向对象和高级特性成为安全软件开发的利器。本文探讨C#在安全加密领域的应用,包括使用System.Security.Cryptography库实现加密算法,利用SSL/TLS保障网络传输安全,进行身份验证,并强调编写安全代码的重要性。实际案例涵盖在线支付、企业应用和文件加密,展示了C#在应对安全挑战的同时,不断拓展其在该领域的潜力和未来前景。
|
6天前
|
程序员 C#
C#编程中的面向对象编程思想
【4月更文挑战第21天】本文探讨了C#中的面向对象编程,包括类、对象、封装、继承和多态。类是对象的抽象,定义属性和行为;对象是类的实例。封装隐藏内部细节,只暴露必要接口。继承允许类复用和扩展属性与行为,而多态使不同类的对象能通过相同接口调用方法。C#通过访问修饰符实现封装,使用虚方法和抽象方法实现多态。理解并应用这些概念,能提升代码的清晰度和可扩展性,助你成为更好的C#程序员。
|
7天前
|
IDE 程序员 C#
C#编程入门:从零开始的旅程
【4月更文挑战第20天】本文引导初学者入门C#编程,从环境搭建开始,推荐使用Visual Studio Community版作为IDE。接着,通过编写&quot;Hello, World!&quot;程序,介绍基本语法,包括数据类型、运算符和表达式。文章还涉及控制结构、函数和方法,以及面向对象编程概念。通过学习,读者将对C#有初步了解,并激发进一步探索编程世界的兴趣。
|
7天前
|
开发框架 .NET Java
探索 C#编程的奥秘与魅力
【4月更文挑战第20天】C#是微软开发的现代、面向对象的编程语言,以其简洁语法、强大功能和跨平台支持脱颖而出。它支持自动垃圾回收、泛型、委托、LINQ,并广泛应用于桌面、Web、移动和游戏开发。C#拥有活跃的开发者社区和丰富的资源,是Unity游戏开发的首选语言。随着.NET Core,C#可在多个操作系统上运行,持续创新,未来发展潜力巨大。
|
7天前
|
缓存 算法 测试技术
优化 C#编程性能的策略
【4月更文挑战第20天】优化C#性能策略包括:选择合适算法和数据结构,避免频繁对象创建,缓存常用数据,减少内存分配,使用异步编程,优化数据库操作(如合理查询和使用索引),利用多线程并行处理,精简代码,使用性能分析工具,硬件升级,以及进行性能测试。综合应用这些策略可提升程序性能和响应性。
|
1月前
|
C# 开发者
35.c#:winform窗口
35.c#:winform窗口
13 1
|
2月前
|
C#
C# Winform 选择文件夹和选择文件
C# Winform 选择文件夹和选择文件
47 0
|
2月前
|
C# 数据安全/隐私保护
C#WinForm基础编程(一)
C#WinForm基础编程
62 0