简体   繁体   English

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

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

I've already wrote my code with my try catch and extra message box but now i have to put the message box into a resource file how can i do it? 我已经用尝试捕获和额外的消息框编写了代码,但是现在我必须将消息框放入资源文件中,我该怎么办?

This is my code: 这是我的代码:

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

You can just add those messages as String in your main application Resource file using the designer ( Resources.resx ) and then access them using Properties namespace. 您可以使用设计器( Resources.resx )在主应用程序资源文件中将这些消息作为String添加,然后使用Properties命名空间访问它们。 Let's say you add this: 假设您添加了以下内容:

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

You can just call it like so: 您可以这样称呼它:

MessageBox.Show(Properties.Resources.ErrorNoFile);

And if you modify the entry name in the resource file, it will be automatically refactored, at least with VS2012 which is the one I'm using. 而且,如果您修改了资源文件中的条目名称,它将被自动重构,至少使用我正在使用的VS2012而言。 Instanciating a ResourceManager is only good if you want to keep those messages in a separate resource, otherwise it looks like an overkill to me. 实例化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