将消息框列表添加到c#中的资源文件中

本文关键字:资源 源文件 消息 列表 添加 | 更新日期: 2023-09-27 17:53:50

我已经写了我的代码与我的尝试捕获和额外的消息框,但现在我必须把消息框到一个资源文件我怎么能做到这一点?

这是我的代码:

  public void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            // in the filepath variable we are going to put the path file that we browsed.
            filepath = txtPath.Text;
            if (filepath == string.Empty)
            {
                MessageBox.Show("No file selected. Click browse and select your designated file.");
            }
       }

将消息框列表添加到c#中的资源文件中

您可以使用设计器(Resources.resx)将这些消息作为字符串添加到主应用程序资源文件中,然后使用Properties命名空间访问它们。假设你添加了这个:

ErrorNoFile | "No file selected. Click browse and select your designated file."

你可以这样称呼它:

MessageBox.Show(Properties.Resources.ErrorNoFile);

如果你修改资源文件中的条目名称,它将被自动重构,至少在我使用的VS2012中是这样。只有当您希望将这些消息保存在单独的资源中时,实例化ResourceManager才是好的,否则对我来说它看起来像是多余的。

// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("items", Assembly.GetExecutingAssembly());
// Retrieve the value of the string resource named "filepath".
// The resource manager will retrieve the value of the  
// localized resource using the caller's current culture setting.
public void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            // in the filepath variable we are going to put the path file that we browsed.
            filepath = txtPath.Text;
            if (filepath == string.Empty)
            {
                String str = rm.GetString("welcome");
                MessageBox.Show(str);
            }
       }