1

我已经用我的 try catch 和额外的消息框编写了我的代码,但是现在我必须将消息框放入资源文件中,我该怎么做?

这是我的代码:

  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.");
            }
       }
4

2 回答 2

1

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

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

你可以这样称呼它:

MessageBox.Show(Properties.Resources.ErrorNoFile);

如果你修改了资源文件中的条目名称,它会自动重构,至少在我正在使用的 VS2012 中是这样。仅当您想将这些消息保存在单独的资源中时,实例化 aResourceManager才是好的,否则对我来说它看起来有点过头了。

于 2013-01-16T15:01:33.760 回答
0
// 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);
            }
       }
于 2013-01-16T14:58:46.750 回答