2015-06-20 106 views
-3

如何使我的標籤正確顯示NumericUpDown1/NumericUpDown2如何將字符串值除以另一個字符串值,每個字符串值均包含數字值

這裏是我有什麼

label6.Text = Convert.ToString(NumericUpDown1.Value/NumericUpDown2.Value); 

我也曾嘗試

label6.text = NumericUpDown1.value/NumericUpDown2.value 

,並得到一個錯誤說

不能隱Convery等等等等成字節

一個後我開始我的項目它崩潰(我所以只要把它當作我的項目打開它嘗試顯示NumUD1/NumUD2 ...

回答

1

在最簡單的術語:

decimal d = (NumericUpDown1.value/NumericUpDown2.value); 

stringVal = System.Convert.ToString(d); 

label6.text = stringVal; 

或者你可以創建的NumericUpDown的子類,並覆蓋ParseValue方法(如圖here):

public class MyNumericUpDown : NumericUpDown { 

    protected override double ParseValue(string text) 
    { 
    // Change text to whatever you want 
    string newText = FixText(text); 

    // Call base implementation. 
    base.ParseValue(newText); 
    } 

    private static string FixText(string inputText) { 
    // DO YOUR STUFF HERE. 
    } 
} 
+0

對不起對於具有誤導性的評論,我的意思是我想的標籤,以顯示NumericUpDown1中劃分BY NumericUpDown2結果:) –

+0

我的更新回答,我沒有IDE,所以它只是從內存中。我不是一個C#程序員... – Woodstock

1

值轉換爲int,然後進行分割,然後通過轉換爲string它分配給label6.Text

label6.Text = (Convert.ToInt32(NumericUpDown1.Value.ToString())/Convert.ToInt32(NumericUpDown1.Value.ToString())).ToString(); 
+0

得到這個錯誤 - http://gyazo.com/cc45199111e4455fec57a670692dc228 –

+0

@JacobReid:那是因爲我忘了在'Value'後面刪除'()'。現在我已經更新了我的答案。 –