2

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 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?
1
  • 3
    I reviewed your question history and comments and it seems that are using this site and a code-writing service. Questions get answered and the code snippets get copied and pasted into your code with minimal tweaking. This is an easy assumption based on your comments below that ask for code tweaks and bug fixes that should be trivial even for beginners. Please try a little harder to figure out your own code. You will learn a lot more and SO will become an even more valuable resource. Commented Jul 26, 2011 at 17:06

3 Answers 3

1

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";

        }
Sign up to request clarification or add additional context in comments.

3 Comments

An error occurs stating: "Input string was not in a correct format." referring to the TextBox
what value you input in textbox?
put one value at a time in text box
1

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

3 Comments

How can I go about this if the RichTextBox and the TextBox will not always be the same? This seems to be hardcoded when the data can change in both of the textboxes.
This is a working sample that you will need to rewrite for your own solution. And if the data in the rich text box can change then the part where I populate the array needs to do parsing rather than hard coding. I would suggest using a parsing snippet from the other samples there but do add validation before entering the number into an array.
Thank you I will rewrite it to properly parse the data.
1

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

7 Comments

There seems to be an error on the line sb.AppendLine(double.Parse(Lines[i]) + d);. It states "The best overloaded method match 'System.Text.Stringbuilder.AppendLine(string)' has some invalid arguments".
While this approach might eventually work I wouldn't recommend the parsing approach since it might create a lot of problems with user interaction. You should store your data in a separate array and you would never need to parse.
@Quantic Programming: Still the same error. I believe it has to do with the Lines[i] + d?
@Colton c'mon you can't figure out .ToString()?
Don't you have any programming sense ?(double.Parse(Lines[i]) + d).ToString()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.