Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In richTextBox1 I wrote these three lines:
line1
line2
line3

When I send this text to whatsapp as a message, the three lines become one line. I need to separate that single line to 3 separate lines. I am not able to do so.

If I could find the last character of each line (assume I found '@')
C#
richTextBox1.Text= richTextBox1.Text.Replace("@", "@" + System.Environment.NewLine);

I can insert new line.

How could I do that in each line?

What I have tried:

C#
char[] charArr = new char[50];
     string text = richTextBox1.Text;
     int len = richTextBox1.Text.Length;
     text.CopyTo(0, charArr, 0, len);

     byte[] bytes = Encoding.ASCII.GetBytes(text);
     string newText = Encoding.ASCII.GetString(bytes);

     newText.CopyTo(0, charArr, 0, len);
  for (int i = 0; i < len; i++)
  {
  //I am searching for '\0' or '\n'. It does not seems it is there
  MessageBox.Show(newText.Substring(i,1),"byte="+bytes[i].ToString()+"ch="+
  charArr[i].ToString());
  }
Posted
Updated 8-Sep-23 11:06am
v4

Start by finding out what exactly you are getting from the Rich text box: use the debugger to look at the string text.
If that contains newlines ("\n") - and it should do - then check the charArr content.
I see newline characters exactly where I expect them if I try your code here ... they are byte values, so you are looking for "10" which is a "Line Feed" character in ASCII, "\n" in Unicode.

It's possible that you need to replace the '\n' with '\r' or "\r\n" to work with whatsapp.
 
Share this answer
 
Comments
Engineer khalid 4-Sep-23 15:49pm    
i beleive whatsapp will not accept \r\n for new line it concacnate all 3 lines in one single line
i will post the whole program here may be somebody solve it
Assuming each line contains a newline character at the end, you can extract them as an array, as described at TextBoxBase.Lines Property (System.Windows.Forms) | Microsoft Learn[^]. To send them to Whatsapp you need to check the Whatsapp documentation to see which character(s) they use to separate the lines.
 
Share this answer
 
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MansoorBuildding
{
    public partial class WhatsAppForm : Form
    {
        char[] chary = new char[100];
        string[] CleanedString = new string[100];
        int len = 0;

        public WhatsAppForm()
        {
            InitializeComponent();
        }

        private void SendWhatUpMessage(string Message, string Number)
        {
            try
            {
                if (Number == "") MessageBox.Show("No number added");
                if (Number.Length <= 0)
                {
                    Number = "+966" + Number;
                }
                //Number = Number.Replace("","");
                System.Diagnostics.Process.Start("https://api.Whatsapp.com/send?phone=" + Number + "&text=" + Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnSendClick(object sender, EventArgs e)
        {
            int j, counter, Nline;
            string[] tempArray = richTextBox1.Lines;
            string str = "";
            Nline = richTextBox1.Lines.Count();
            Nline--;

            for (counter = 0; counter < Nline; counter++)
            {
                System.Diagnostics.Debug.WriteLine(tempArray[counter]);
                len = tempArray[counter].Length;

                tempArray[counter].CopyTo(0, chary, 0, len);

                CleanedString[counter] = "";
                for (j = 0; j < len; j++)
                    CleanedString[counter] += chary[j].ToString();
            }

            str = "";
            for (counter = 0; counter < Nline; counter++)
            {
                if (counter < Nline - 1) str += CleanedString[counter] + "%0a";//<=== add "%0a"  not \r\n
                else if (counter == Nline - 1) str += CleanedString[counter];
            }

            SendWhatUpMessage(str, textBoxMobileNo.Text);
        }
        public void ClearNumberAndMessage()
        {
            textBoxMobileNo.Text = "";
            richTextBox1.Text = "";
        }
    }
}
 
Share this answer
 
v3
Comments
Engineer khalid 4-Sep-23 15:59pm    
the above code send 3 lines to whatsapp but whatsapp receive it and accept it as one line
if somebody solve this issue ?
Richard Deeming 5-Sep-23 3:48am    
So this "solution" you have posted, and accepted as the solution to your question, is in fact NOT a solution at all?

Delete this non-solution. If you want to update your question, click the green "Improve question" link and edit your question.
Engineer khalid 5-Sep-23 6:42am    
u are right it was not the solution
i found the solution give me some time (about 30 minutes )to review it
Engineer khalid 5-Sep-23 7:00am    
whatsapp need "%0a" for new line .
to find number of lines in richBox1 use Nline = richTextBox1.Lines.Count();
empty line in richTextBox1 considerd as a line.Those things added more bug to code
Richard MacCutchan 5-Sep-23 7:12am    
"empty line in richTextBox1 considerd as a line."
Yes, as it would be in any document. You just need to iterate the array returned from richTextBox1.Lines (see my answer above), add the line end character (%0A*) to the end, and send it to Whatsapp.

*%0A represents the line-feed character which is the same as is used by '\n' in most programming languages.

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