2013-10-31 37 views
1

這是作業(我總是試着指出它,所以它在前面)。該程序是一個轉換程序。用戶從組合框中選擇轉換選項,然後在用戶點擊計算程序計算轉換時輸入長度(即:英尺到米)。我有幾個問題,因爲陣列部分令我困惑。我想確保我正朝着正確的方向前進。轉換程序將信息存儲在數組中並使用組合框

我想我使用我的數組來填充我的組合框(我使用了一個提供的示例,雖然我沒有完全理解它)。當用戶點擊計算按鈕時,我應該將我的轉換值存儲在同一個數組中嗎?像這樣:

string [,] conversions = { {kilometers to miles, .11111}, {miles to kilometers, .11111}} 

或者我正在朝着正確的方向與我有什麼?要清楚,因爲它是編碼數組填充我的組合框,所以如果我添加額外的數據,那麼它會顯示這些數字在組合框,這不是我真正想要的。

我的下一個問題,當用戶點擊計算按鈕,它將如何知道用戶選擇了什麼選項?我認爲這與索引有關,但就實際聲明它而言,我感到困惑?

*****忽視這個問題。我想我找到了答案*********

Answer on Updating Labels

最後,我想我的最後一個問題是頁面有旁邊的文本框,標籤,這樣如果用戶選擇「萬里公里的」輸入文本框會說邁爾斯,然後答案文本框會說千米......這是什麼叫?我需要找到它在我的書,不能。我知道我失去了一些東西,但我試圖找到任何一個例如或者在書它被覆蓋,我只是沒有看到它。


下面是我的代碼,我現在有。

public partial class FrmConversions : Form 
{ 
    const double Miles_To_Kilometers = 1.6093; 
    const double Kilometers_To_Miles = 0.6214; 
    const double Feet_To_Meters = 0.3048; 
    const double Meters_To_Feet = 3.2808; 
    const double Inches_To_Centimeters = 2.54; 
    const double Centimeters_To_Inches = 0.3937; 

    public FrmConversions() 
    { 
     InitializeComponent(); 
    } 


    private void btnExit_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 


    private void FrmConversions_Load(object sender, EventArgs e) 
    { 
     cboConversions.Items.Clear(); //clear the combobox 

     string[,] conversions = 
      { 
       {"Kilometers to Miles" , "Miles to Kilometers"}, 
       {"Feet to Meters"  , "Meters to Feet"}, 
       {"Inches to Centimeters", "Centimeters to Inches"} 
      }; 

     foreach (string str in conversions) 
     { 
      cboConversions.Items.Add(str); 
     } 
    } 


    private void btnClear_Click(object sender, EventArgs e) 
    { 
     txtEntry.Clear(); 
     txtAnswer.Clear(); 
    } 


    public bool IsDecimal(TextBox txtEntry, string name) 
    { 
     try 
     { 
      Convert.ToDecimal(txtEntry.Text); 
      return true; 
     } 

     catch (FormatException) 
     { 
      MessageBox.Show(name + " must be a decimal value.", "Entry Error"); 
      txtEntry.Focus(); 
      return false; 
     } 
    } 


    private void btnCalculate_Click(object sender, EventArgs e) 
    { 
     int index = cboConversions.SelectedIndex; 
     if (index != -1) 
     { 
      try 
      { 
       if (IsDecimal()) 
       { 
        txtAnswer.Text = ToString; 
       } 

       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message + "\n\n" + 
        ex.GetType().ToString() + "\n" + 
        ex.StackTrace, "exception"); 
       } 
      } 
     } 
    } 
} 


} 

回答

0

有很多方法可以做到這一點,字典將是整潔?

在這個例子中,字典的鍵(我指定的字符串)被加載到組合框中。稍後,這些字符串可用於從字典中檢索轉換值。

// Declare at the top 
Dictionary<string, double> conversions = { 
    { "Kilometers to Miles", Kilometers_To_Miles}, 
    { "Miles to Kilometers", 1/Kilometers_To_Miles}, 
    { "Feet to Meters", Feet_To_Meters}, 
    { "Meters to Feet", 1/Feet_To_Meters}, 
etc 
}; 

// In Form Load 
foreach (string str in conversions.Keys) 
{ 
    cboConversions.Items.Add(str); 
} 

// In btnCalculate_Click 
var conversionValue = conversions[cboConversions.Text]; 
var convertedValue = (double)txtEntry.Text * conversionValue; // Need to validate is numeric 
txtAnswer.Text = convertedValue 

如果您在使用數組那麼這將工作太背水戰,但騙子通過在結束使用LINQ:

// Declare at the top 
object[,] conversions = { 
    { "Kilometers to Miles", Kilometers_To_Miles}, 
    { "Miles to Kilometers", 1/Kilometers_To_Miles}, 
    { "Feet to Meters", Feet_To_Meters}, 
    { "Meters to Feet", 1/Feet_To_Meters}, 
etc 
}; 

// In Form Load 
foreach (string str in conversions) 
{ 
    cboConversions.Items.Add(str[0]); 
} 

// In btnCalculate_Click 
var conversionValue = conversions.First(x => x[0] == cboConventions.Text)[1]; 
var convertedValue = (double)txtEntry.Text * conversionValue; // Need to validate is numeric 
txtAnswer.Text = convertedValue 
+0

謝謝主席先生。這很有幫助。 –

相關問題