2013-01-16 14 views
0

我已經用我的try catch和額外的消息框編寫了我的代碼,但現在我必須將消息框放入資源文件中,我該怎麼做?將消息框的列表添加到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."); 
      } 
     } 
+0

您是指消息框或您正在顯示的文本? – ScruffyDuck

+0

@ScruffyDuck正在顯示的文字 – Amanda

回答

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); 
      } 
     } 
1

您可以使用設計器(Resources.resx)只需添加這些消息作爲字符串在主應用程序的資源文件,然後使用屬性命名空間訪問它們。比方說,你補充一點:

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

你可以叫它像這樣:

MessageBox.Show(Properties.Resources.ErrorNoFile); 

如果你在資源文件修改條目名稱,它會自動重構,至少在VS2012這是我正在使用的一個。如果你想把這些消息保存在一個單獨的資源中,那麼實例化一個ResourceManager就好了,否則它看起來對我來說太過矯枉過正。

+0

Thank Youu @Zarathos – Amanda

相關問題