locked
How do I write count characters RRS feed

  • Question

  • How can I write a calculated field text box in C# to count the characters of another text box and display the count in a field using the label control type. Then can I use the counted characters as a number to add to the result of another count characters calculated field to display the total of those two fields.


    Question for the Imp-Ass input screen can I make a calculated field were
        a. result = count characters of general Imp-Ass property input text box
        b. display this count below the input text box
        c. make a another input box below for each special Imp-Ass property
        d. make a another calculated field were
            result = count characters of special Imp-Ass property plus characters of general Imp-Ass property
    Monday, July 14, 2014 4:09 AM

All replies

  • From your description above, I think you need count the string length in textbox first, such as:

    int count = 0;
    string st = "Hello world! ";
    bool prevIsLetter = false;
    foreach (char c in st)
    {
      bool isLetter = char.IsLetter(c);
      if (isLetter && !prevIsLetter)
      {
       count++;
      }
      prevIsLetter = isLetter;
    }
    Console.WriteLine(count);

    Happy coding.

    Thursday, July 24, 2014 11:34 AM
  • Do you need to do this as the user types (character by character) or just when they leave the text box?

    The String.length property should be good enough for most uses.

    If you only need the values when the user leaves the text boxes, add three calculated fields to the entity for the length of the two strings and the sum of the two lengths. Add code to their Calculate events to set the results, then add these calculated fields to your screen where you want to see them.

    If you need the character counts to change as the user types, you'd have to intercept the ControlAvailable events for the text boxes on the screen to add handlers for the KeyPress events. Let us know if you need more help with that.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Sunday, July 27, 2014 8:30 AM
  • Ideally it would count the characters before exiting the text box. The reason is the whole point is to allow the user to know if they have used up a combined number of characters that will need to fit into single box on the final form. Knowing that dynamically as they type would allow them to think through and edit the sentances they write.

    The Excel form would handle concatenating the two text strings based on a variable(Drug Route). Combining "General" Implementation and Assessment information with one other "Specific" Implementation and assessment string such as IV (intra venous) or Oral. It is however possible for there to be only a need for "General" information for a specific medication because it would take up less room that way allowing for more general or combined string length. I want to give the Administrative user a way to quickly gadge that use for each medication they add to the database.

    I see the code for counting characters is

    int count = 0;
    string st = "Hello world! "; 
    bool prevIsLetter = false;
    foreach (char c in st)
    {
      bool isLetter = char.IsLetter(c);
      if (isLetter && !prevIsLetter)
      {
       count++;
      }
      prevIsLetter = isLetter;
    }
    Console.WriteLine(count);

    What would be the code used for a sum of these two calculated fields. Also how do I handle what you said about the character counts to changing as the user types.
    "...intercept the ControlAvailable events for the text boxes on the screen to add handlers for the KeyPress events."
    Sunday, July 27, 2014 11:11 PM
  • As I said, in order to display the number of characters as they are typed, you will have to add a handler to handle key presses in the TextBox. In order to do this you add a handler to the ControlAvailable event of the TextBox and you do this in the Screen_Created event.

    You'll also need an Integer screen variable to put the number into so that it can be shown on screen.

    You need to handle two TextBox controls and add the results together so you need two sets of event handlers and three integer screen variables.

    The code you quote will count the words in a string, not the characters. It looks to see where a letter (A-Z, a-z) is at the start of the string or follows a non-letter character. You asked for a count of the characters. That is just the Length property of the string variable.

    The code you need, for one TextBox, is going to look something like this. (Not tested)

    You'd need two of these, one for each text box. You need to set the names in the code to match the controls on your screen and create the Integer data objects on your screen to hold the results and put them in the control tree to display on screen.

    private void ScreenName_Created()
    { this.FindControl("TextBoxOne").ControlAvailable += (object sender, ControlAvailableEventArgs e) =>
     {  ((TextBox)e.Control).KeyUp += (object sender1, KeyEventArgs e1) =>
      {
       this.TextOneLength = ((TextBox)sender).Text.Length;
       this.TotalLength = this.TextOneLength + this.TextTwoLength;
      };
     };
    }


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Monday, July 28, 2014 8:47 AM