Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
This is a C# application that has a RichTextBox named (rtbInfo)
The code works great in a VB.Net project with same rtbInfo
In VB.Net this line of code
ElseIf chr = vbLf Then

Was rewritten in C# as
else if (chr == '\n')


I will post the complete code with the declaration of variables
the rtbInfo has WordWrap set to true and a limit of 127 Characters
and a Size of 347 by 120 Font 10.8 If anyone wants to test

What I have tried:

public partial class frmADE : Form
{

    int chrPermited = 32;
    int total = 0;
    int spaceCount = 0;
    int letterCount = 0;
    int carReturn = 0;
    int punCount = 0;
    int syCount = 0;
    int lines = 0;
    public frmADE()
    {
        InitializeComponent();
    }
    private void rtbInfo_TextChanged(object sender, EventArgs e)

    {
        //int spaceCount, letterCount, carReturn, punCount, syCount;
        // Is this permitted in C# I did it in VB.Net ? ? ? Asking for a friend
        lines = 0;
        spaceCount = 0;
        letterCount = 0;
        carReturn = 0;
        punCount = 0;
        syCount = 0;

        foreach (char chr in rtbInfo.Text)
        {
            if (char.IsLetterOrDigit(chr))
                letterCount += 1;
            else if (chr == '\n') // In VB.net written as else if (chr == Constants.vbLf)
           //================================================================
            {
                carReturn += 1;
                lines += 1;

            }
            else if (char.IsLetterOrDigit(chr))
                letterCount += 1;
            else if (char.IsWhiteSpace(chr))
                spaceCount += 1;
            else if (char.IsPunctuation(chr))
                punCount += 1;
            else if (char.IsSymbol(chr))
                syCount += 1;

            if (lines == 4)
            {
                tbMessage.Text =("NO MORE ENTRY");
                rtbInfo.BackColor = Color.White;
                rtbInfo.ReadOnly = true;
                rtbInfo.Select();
                tbMessage.Text = "Max Lines = " + lines;
                return;
            }

            if (total == 1)
            {
                tbMessage.Text = ("Press Return");
                total -= 1;
                tbMessage.Text = "Press RETURN";
                return;
            }

            if (chr == '\n')
            {
                chrPermited = 32;
                letterCount = 0;
                spaceCount = 0;
                carReturn = 0;
                punCount = 0;
                syCount = 0;
                total = 0;
            }
        }

        total = chrPermited - letterCount - spaceCount - carReturn - punCount - syCount;
        tbMessage.Text = total.ToString() + "  More Characters on this Line";
    }
Posted
Updated 5-Feb-23 13:37pm
Comments
OriginalGriff 21-Jan-23 4:47am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

What have you tried?
Where are you stuck?
What help do you need?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 21-Jan-23 5:32am    
You can use System.Environment.NewLine for the C# equivalent to vblf.
Richard Deeming 23-Jan-23 4:32am    
Depending on the environment, Environment.NewLine could be equivalent to vbLf, vbCr, or vbCrLf.
Richard MacCutchan 23-Jan-23 4:49am    
Yes, I should have clarified that the term "NewLine" has more than one actual meaning.
Member 15627495 21-Jan-23 8:04am    
all your functions exist to count characters ?

or the details of each type of chars are needed constraints ?

int rtb_size = rtbinfo.length ;

First THANKS to all who responded. My What I Tried Code was VB.Net and way over complicated.
So I decided to start kind of from scratch. Looked at a few Code Project articles.
The suggestion to use
var lineCount = rtbInfo.Lines.Count();

was less work but I wanted to know how to capture this "\n" in the RichTextBox.
While this code works it presented problems of how to deal with setting the
RTB to Read Only = true. This led to learning how to work with ProcessCmdKey.
The two variables tot and totE the first is use when entering NEW data the other when doing an EDIT
The value 127 is the max number of characters permitted in the RTB.
here is the code suggestions welcomed this is my first C# application.
I do NOT understand what the "c" does in this line of code.
Quote:
foreach (char c in rtbInfo.Text)

private void rtbInfo_TextChanged(object sender, EventArgs e)
{

    vbCr = 0;

    if (rtbInfo.Text.Contains("\n"))
    {
        char[] carReturn = {'\n'};

        foreach (char c in rtbInfo.Text)
        {
            if (carReturn.Contains(c))
            {
                vbCr += 1;
            }
        }
    }

     //var lineCount = rtbInfo.Lines.Count();

     if (frmStart.doWhat == 3)
        {
        totE = rtbInfo.TextLength;
        tbMessage.Text = "Enter " + (127 - totE) + " more characters & " + (4 - vbCr) + " Lines";

    }
     if (frmStart.doWhat == 1)
        {
        tot = rtbInfo.TextLength;
        tbMessage.Text = "Enter " + (127 - tot) + " more characters & " + (4 - vbCr) + " Lines";
        }

    if (tot == 127 | totE == 127)
     {
        rtbInfo.ReadOnly = true;
        tbMessage.Text = "UP Arrow Key to Edit NOTES";
     }
    if (vbCr == 4)
    {
        rtbInfo.ReadOnly = true;
        tbMessage.Text = "Only 4 Lines UP Arrow Key to Edit";
    }
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Up)
        //|| (keyData == Keys.Left) || (keyData == Keys.Right) || (keyData == Keys.Down))
    {
        rtbInfo.ReadOnly = false;
        tbMessage.Text = "Edit Additional Notes";
        SendKeys.Send("{BACKSPACE}");
        rtbInfo.Focus();
        return true;
    }
    else
    {
        if(vbCr ==4 | tot == 127)
        {
            tbMessage.Text = "Press UP Arrow Key to Edit";
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}
 
Share this answer
 
This has been a REAL learning experience. I went a complete different route
for monitoring the characters as they were entered and deleted in the RichTextBox
Removed the TextChanged and decided to use KeyUp and use the Backspace and
Enter keys to preform function. Speaking of function I learned to write a function!
NOT sure this was good design. Please Comment.
Took 3 days to discover this it goes in the InitializeComponent();
THIS is terrible MS rtbInfo.KeyUp += rtbInfo_KeyUp; WHY I need to register things!
The code is still way too much like spaghetti code.
I will post the code and politely ask for suggestion to improve on the design.
public partial class frmTest : Form
{
    int Count = 0;
   static int Lines = 0;
    int total;
    static int cT;
    static int res;
    static int Z;
    static bool FT;

    public frmTest()
    {
        InitializeComponent();
        rtbInfo.KeyUp += rtbInfo_KeyUp;
        /*== Took 3 days to discover this ==*/
    }

    public static string shoMsg()
    {
        if(Z == 1 & FT == true)
        {
            Z = 2;
        }
        if(FT == true)
        {
            res = res + 1;
        }
        if(FT == false)
        {
            if(Lines == 1 | Lines == 2 | Lines == 3 | Lines ==4)
            {
               res = 33 - cT;
            }
        }
        string retMsg = "Total of " + res + " more char can be entered T or F " + FT;//For Testing
        // Production tbMessage.Text = "Total of " + res + " more char can be entered on line " + Lines;
        return retMsg;
    }

    private void rtbInfo_KeyUp(object sender, KeyEventArgs e)
    {
        Count = rtbInfo.TextLength;
        Lines = rtbInfo.Lines.Length;
        cT = cT + 1;
        FT = false; // Start false because it is for adding new text

        txtBoxMsg.Text = "Total of " + (135 - Count) + " can be entered & " + (5 - Lines) + " Lines";

            if (e.KeyCode == Keys.Back)
            {
                FT = true;// Set to true because your delete text

            tbCount.Text = "Back R " + res + "  cT "+cT + " FT " + FT;// for testing

            rtbInfo.Focus();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
            }

            if(FT == true)// Counts while deleting
            {
            string getMsg = shoMsg();
            tbMessage.Text = getMsg;
            cT = cT - 1;
            Z = 1;
                if(Z != 1)
                {
                cT = 0;
                }
            }

            if(FT == false)// Counts while adding
            {

            string getMsg = shoMsg();
            tbMessage.Text = getMsg;
            }

        if (rtbInfo.Text.EndsWith("\n"))
        {
            cT = 0;
            res = 0;
            Count = 0;
            res = 33;
            tbCount.Text = "Issued New Line";

        }

        total = cT;
        if (total == 33)
        {
            SendKeys.Send("{ENTER}");
            rtbInfo.ScrollToCaret();
            rtbInfo.Focus();
            total = 0;
            cT = 0;
            res = 0;
        }
 
Share this answer
 
Comments
CHill60 6-Feb-23 3:59am    
Please do not post multiple solutions to questions - especially your own. At best it is confusing, at worst it makes to appear to be a rep-point hunter. You can use the "Improve solution" link on your original solution to add extra information.
You can do something similar with your question too - look for the "Improve Question" link
Choroid 6-Feb-23 14:20pm    
CHill60 My apologizes I assure you I am not a rep-point hunter
I will follow your advice and use the Improve Solution link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900