繁体   English   中英

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

[英]Add the list of messagebox to a resource file in c#

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

这是我的代码:

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

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

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

您可以这样称呼它:

MessageBox.Show(Properties.Resources.ErrorNoFile);

而且,如果您修改了资源文件中的条目名称,它将被自动重构,至少使用我正在使用的VS2012而言。 实例化ResourceManager只是在您希望将这些消息保存在单独的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);
            }
       }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM