开发者

C# - Adding two numbers from a RichTextBox and a TextBox

开发者 https://www.devze.com 2023-03-22 20:46 出处:网络
I have a textbox that the user can input into. Right now the user can enter in anything, but I would like to limit the user.

I have a textbox that the user can input into. Right now the user can enter in anything, but I would like to limit the user.

  • I first need to only开发者_如何学Go allow numbers from the user that are negative or positive, and/or decimal (up to 3 digits).

In the RichTextBox the data looks like this:

227.905
227.905
242.210
-236.135
5.610
29.665
269.665

SO, what I am trying to do is add the string value from the TextBox to each one of these lines in the RichTextBox.

EXAMPLE: If the user entered in "25.305" into the TextBox it would add that value to each one of the values in the RichTextBox and then replacing that value in the RichTextBox with the new value making the updated RichTextBox look like this:

253.210
253.210
267.515
-210.830
30.915
54.970
294.970
  • Does anyone know how to do this?


Try this

 string[] Values = richTextBox1.Text.Split(new char[]{'\r','\n'});
            richTextBox1.Clear();
            foreach (string Value in Values)
            {
                richTextBox1.Text += (Convert.ToDouble(Value) + Convert.ToDouble(textBox1.Text))+"\r\n";

        }


Rather than maintaining your array of numbers in a rich edit control you should create a separate list collection. Then when the user enters data, add the entered number to each entry in the collection, build an updated string and set it as your rich text data.

Something like this:

using System;
using System.Collections.Generic;
using System.Text;

public class SomeClass
{
    public static void Main()
    {
        // your collection - make this a class member
        List<double> nums = new List<double>();

        // populate array
        nums.Add(227.905);
        nums.Add(227.905);
        nums.Add(242.210);
        nums.Add(-236.135);
        // etc.

        // faux user data entry - collect it and validate it a button click handler
        double userNum = 25.305;

        // add new value to items in array
        for (int i=0; i<nums.Count; i++) nums[i] += userNum;

        // declare string builder for fast string concatenation
        StringBuilder sb = new StringBuilder();

        // build output string from nums array
        foreach (double d in nums)
            sb.Append(d.ToString() + System.Environment.NewLine);

        // writing to console here but you would do something like:
        //    _myRichEditControlInstance.Text = sb.ToString();
        Console.WriteLine(sb.ToString());
    }
}


IF RichTextBox.Text returns a string which has the data. (sorry can't test).

string[] Lines = richTextBox.Text.Split(new char[] {'\r','\n'});
StringBuilder sb = new StringBuilder();
double d = double.Parse(textBox1.Text);
for(int i = 0; i < Lines.Lenght; ++i)
     sb.AppendLine((double.Parse(Lines[i]) + d).ToString());
richTextBox.Text = sb.ToString();



it should work

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号