2022-12-07 14:23:24 +08:00

178 lines
3.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Winform
date: 2022-11-18 14:48:31
author: 文永达
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/B951AE18-D431-417F-B3FE-A382403FF21B.jpeg
---
# Winform
## Net Framework
### 控件属性
#### Name
表示控件名
```c#
this.button1.Name = "button1";
```
#### Text
表示控件文本显示
```c#
this.button.Text = "button1";
```
### 控件事件
#### button按钮
##### click
#### comboBox
##### SelectedIndexChanged
问题SelectedIndexChanged控件初始加载的时候总会进去两次SelectedValue 值总为System.Data.DataRowView。
原因最后才发现自己是先绑定数据源后设置控件ValueMember和DisplayMember属性。
解决办法:正确的做法是先设置这两个属性,后绑定数据源。
##### 绑定数据源
```c#
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("NAME", typeof(string));
DataRow dr = dt.NewRow();
dr["ID"] = "1";
dr["NAME"] = "NAME1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "2";
dr["NAME"] = "NAME2";
dt.Rows.Add(dr);
this.comboBox1.DisplayMember = "NAME";
this.comboBox1.ValueMember= "ID";
this.comboBox1.DataSource = dt;
```
### 控件文本显示国际化
#### 使用资源文件方式
在解决方案根目录新建`App_GlobalResources`文件夹
新建 `Resource.en-US.resx` 资源文件
放置英文文本
新建 `Resource.resx`资源文件
放置默认简体中文文本
根目录新建 `ResourceCulture.cs`类
```c#
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading;
namespace WindowsFormsApp1
{
public class ResourceCulture
{
// 设置需要的语言文本资源文件
public static void SetCurrentCulture(string name)
{
if (string.IsNullOrEmpty(name))
{
name = "en-US";
}
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name);
}
// 获取资源文件中的文本
public static string GetString(string id)
{
string strCurLanguage = "";
try
{
ResourceManager rm = new ResourceManager("WindowsFormsApp1.App_GlobalResources.Resource",
System.Reflection.Assembly.GetExecutingAssembly());
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
strCurLanguage = rm.GetString(id, ci);
}
catch
{
strCurLanguage = "No id" + id + ", please add.";
}
return strCurLanguage;
}
}
}
```
控件里调用
新建initRes()方法
```c#
private void initRes()
{
// 设置 窗体form 名称
this.Text = ResourceCulture.GetString("Form1_frmText");
// 设置 分组框 groupbox 名称
this.gbLanguageView.Text = ResourceCulture.GetString("gbLanguageView_frmText");
this.gbLanguageSelection.Text = ResourceCulture.GetString("gbLanguageSelection_frmText");
}
```
可以在窗体初始化调用加载
```c#
private void Form1_Load(object sender, EventArgs e)
{
this.initRes();
}
```
如果是一个切换语言的窗体
新建可以切换的控件,这里使用`RadioButton`,因为是中英文切换,所以需要建两个`RadioButton`
使用`click`事件
```c#
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
ResourceCulture.SetCurrentCulture("en-US");
this.SetResourceCulture();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
ResourceCulture.SetCurrentCulture("zh-CN");
this.SetResourceCulture();
}
```