C#资源文件的使用实例

C#资源文件的使用实例

在写程序时, 可以把用到的 字符串,图标,图片,声音等外部资源,放在一个 .resx (资源文件)中。 这样的好处是不用考虑什么路径的问题.而且还对资源有保护的作用。

一、程序界面

二、新建一个项目Ky_Resx;

在Form1上放置2个按钮和1个picturebox。

三、创建一个 Resource1.resx 文件.

选中 项目Ky_Resx,右击 “添加”—“新建项”

四、向Resource1.resx 里添加字符串

双击Resource1.resx,

五、向Resource1.resx 里添加图像

下拉选中图像, 然后 拷贝图像后 粘贴到资源中,并改名为“图片1”和“图片2”

六、编写程序

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;

namespace Ky_Resx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           if (button2.Text=="图片1" )
           { button2.Text=Resource1.ButtonText2; }
           else
            button2.Text=Resource1.ButtonText1;
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text=="图片1" )
            {
                pictureBox1.Image = Resource1.图片1;
            }
            else
            {
                pictureBox1.Image = Resource1.图片2;
            }
        }
    }
}

七、源程序下载

1、dalong10的下载https://download.csdn.net/download/dalong10/13200217

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值