2015-11-20 32 views
0

我正在寫一個圖形繪製代碼來呈現圖中所示的數學表達式。我不明白如何在文本框中輸入表達式來應用於進一步的計算?舉個例子:我如何取代y = x * x/128;在我的代碼中輸入在文本框中的表達式?像y = textbox.text;C#如何使用UI文本框中的數學表達式進行計算?

protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     e.Graphics.ScaleTransform(1.0f, -1.0f); //Invert coordinatesystem on screen 
     e.Graphics.TranslateTransform(420, -this.ClientRectangle.Height + 300); //PUT coordinate 0, 0 in the middle of screen 

     int min = 0; 
     int max = 250; 

     int x = 0; 
     int y = 0; 

     e.Graphics.DrawLine(Pens.LightSteelBlue, min-400, min, max+200, min); //X1,Y1,X2;Y2 
     e.Graphics.DrawLine(Pens.LightSteelBlue, min, min-250, min, max); //X1,Y1,X2;Y2 

     Brush aBrush = (Brush)Brushes.LightSteelBlue; 

     while (x <= max && y <= max) 
     { 

      y = x*x/128; 

      if (y > max) 
       break; 

      e.Graphics.FillRectangle(aBrush, x, y, 1, 1); //x,y,width,height 

      System.Threading.Thread.Sleep(10); 
      Application.DoEvents(); 

      x++; 
     } 

     INPUT_FORMULA.Focus(); // textbox to enter formula 
     INPUT_FORMULA.Select(INPUT_FORMULA.Text.Length, 0); 
    } 

enter image description here

回答

0

因爲它是一個字符串,你必須分析它。對於簡短的表達式,您可以手動操作。但對於更高級的你應該尋找一個圖書館。

+0

我該怎麼做? –

+0

我試過了:double.TryParse(INPUT_FORMULA.Text,out b); –

+0

int p = Convert.ToInt32(a); int q = Convert.ToInt32(b); –

相關問題