# How to calculate the number of characters per block size from a text content in a rcihTextBox?

516 Reputation points
2023-11-15T00:25:32.4833333+00:00

There are two richTextBoxes. The one on the right is the richTextBox I paste/type text inside. This is also the richTextBox that I want later to split the text from to the richTextbox on the left.

The richTextBox on the right name is: richTextBoxToSplit and the one on the left name is: richTextboxSplitted.

now for the logic/rules of what should be happening when I put text in the richTextBoxToSplit or/and in the textBoxSplitAmount.

First the most important thing to mention is that the value in the textboxSplitAmount meaning the length of characters that should be in each block when splitting later the text from the richTextBoxToSplit. the numbers the user type in the textBox are not the number of blocks that should be splitted but the number of characters in each block.

The splitting formula is automatically doing in the btnSplit click event code.

so the logic behind the application: the user enter a number of characters for each block in the textBox then when he click the SPLIT button the application will create blocks with the amount of entered characters for each block and will display it on the left richTextBoxSplitted.

Now for the sizes , I display the sizes on the label name: lblSizeInfo and size I mean by if I was taking the text from the richTextBoxToSplit and put it in a text file and save it to the hard disk then this size. and same logic for the size of each splitted block. but I want to calculate the sizes even before clicking the SPLIT button.

you can see the example in the screenshot below.

and I also adding the completed code.

The problems:

1. I'm not sure if the calculation of the size/s is right and that I display the right size/s in the lblSizeInfo.
2. How to prevent from the user to type in the textBoxSplitAmount amount that is bigger than the whole text length in the richTextBoxToSplit? because it's not logic that the user will be able to type for example 11111111 if the in the richTextBoxToSplit the text will be for example HELLO WORLD. because HELLO WORLD is only 10 characters so it can be splitted for maximum 10 blocks maybe more, but each block can't contain more than the whole characters length in the richTExtBoxToSplit.

This is the main logic and the problems of my application. I hope I explained it better.

``````using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextSplitterApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

if (richTextBoxToSplit.Text.Length == 0)
{
btnSplit.Enabled = false;
btnCopyBlock.Enabled = false;
comboBoxBlocks.Enabled = false;
textBoxSplitAmount.Enabled = false;
}
else if (textBoxSplitAmount.Text.Length == 0)
{
textBoxSplitAmount.Text = "1";
textBoxSplitAmount.Enabled = true;
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void btnSplit_Click(object sender, EventArgs e)
{
string inputText = richTextBoxToSplit.Text;
int blockSize = int.Parse(textBoxSplitAmount.Text);
int blockNumber = 1;
richTextBoxSplitted.Clear();
comboBoxBlocks.Items.Clear(); // Clear existing items in ComboBox

for (int i = 0; i < inputText.Length; i += blockSize)
{
int length = Math.Min(blockSize, inputText.Length - i);
string block = inputText.Substring(i, length);

// Add the block number to the ComboBox

// Ensure a newline before and after the block title
if (blockNumber > 1)
{
richTextBoxSplitted.AppendText("\n\n");
}

// Format the block number in white color
richTextBoxSplitted.SelectionColor = Color.White;
richTextBoxSplitted.AppendText(\$"Block Number {blockNumber}:\n");
richTextBoxSplitted.SelectionColor = Color.Yellow; // Set text color back to yellow
richTextBoxSplitted.AppendText("\n"); // Add an empty line
richTextBoxSplitted.AppendText(block);

blockNumber++;
}

comboBoxBlocks.Enabled = true;
}

private void comboBoxBlocks_SelectedIndexChanged(object sender, EventArgs e)
{
// Scroll to the start of the selected block
string selectedBlock = comboBoxBlocks.SelectedItem.ToString();
int blockIndex = selectedBlock.IndexOf("Block Number ");
if (blockIndex >= 0)
{
int blockNumber = int.Parse(selectedBlock.Substring(blockIndex + 13)); // Extract the block number
int startIndex = richTextBoxSplitted.Text.IndexOf(\$"Block Number {blockNumber}:");
if (startIndex >= 0)
{
richTextBoxSplitted.Select(startIndex, 0);
richTextBoxSplitted.ScrollToCaret();
}

btnCopyBlock.Enabled = true;
}

btnSplit.Focus();
}

private void btnCopyBlock_Click(object sender, EventArgs e)
{
// Copy the text of the selected block to the clipboard
string selectedBlock = comboBoxBlocks.SelectedItem.ToString();
int blockIndex = selectedBlock.IndexOf("Block Number ");
if (blockIndex >= 0)
{
int blockNumber = int.Parse(selectedBlock.Substring(blockIndex + 13)); // Extract the block number
int startIndex = richTextBoxSplitted.Text.IndexOf(\$"Block Number {blockNumber}:");
int endIndex = richTextBoxSplitted.Text.Length;

if (startIndex >= 0)
{
// Find the end of the block (start of the next block or end of text)
int nextBlockIndex = richTextBoxSplitted.Text.IndexOf(\$"Block Number {blockNumber + 1}:", startIndex);
if (nextBlockIndex >= 0)
{
endIndex = nextBlockIndex;
}

// Extract the block text without empty lines
string blockText = richTextBoxSplitted.Text.Substring(startIndex, endIndex - startIndex).Trim();

// Copy the block text to the clipboard
Clipboard.SetText(blockText);
}
}
}

private void richTextBoxToSplit_TextChanged(object sender, EventArgs e)
{
}

private void textBoxSplitAmount_TextChanged(object sender, EventArgs e)
{
}

private void textBoxSplitAmount_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true; // Ignore the key press if it's not a digit or a control character (e.g., Backspace)
}
}

private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
richTextBoxToSplit.Paste();
}

{
// Check if the text box is empty or contains only '0'
bool isTextBoxEmptyOrZero = string.IsNullOrEmpty(textBoxSplitAmount.Text) || textBoxSplitAmount.Text.All(c => c == '0');

// Check if the rich text box is empty
bool isRichTextBoxEmpty = string.IsNullOrEmpty(richTextBoxToSplit.Text);

// Enable the "Split" button only if both conditions are met
btnSplit.Enabled = !(isTextBoxEmptyOrZero || isRichTextBoxEmpty);

textBoxSplitAmount.Enabled = !isRichTextBoxEmpty;

// Clear richTextBoxSplitted if isRichTextBoxEmpty is true
if (isRichTextBoxEmpty)
{
richTextBoxSplitted.Text = "";
}

// Calculate and display size information
if (!isRichTextBoxEmpty && !isTextBoxEmptyOrZero)
{
int textSizeInBytes = Encoding.UTF8.GetByteCount(richTextBoxToSplit.Text);
int blockSize = int.Parse(textBoxSplitAmount.Text);

if (blockSize > 0 && richTextBoxToSplit.Text.Length > 0)
{
long blockSizeInBytes = blockSize * Encoding.UTF8.GetMaxByteCount(1);
lblSizeInfo.Text = FormatSizeInfo(textSizeInBytes) + ", Block Size: " + FormatSizeInfo(blockSizeInBytes);
}
else
{
lblSizeInfo.Text = "";
}
}
else
{
lblSizeInfo.Text = ""; // Clear the label if the rich text box is empty or blockSize is zero
}
}

private string FormatSizeInfo(long sizeInBytes)
{
const long KB = 1024;
const long MB = 1024 * KB;
const long GB = 1024 * MB;

if (sizeInBytes >= GB)
{
double sizeInGB = (double)sizeInBytes / GB;
return string.Format("{0:F2} GB", sizeInGB);
}
else if (sizeInBytes >= MB)
{
double sizeInMB = (double)sizeInBytes / MB;
return string.Format("{0:F2} MB", sizeInMB);
}
else if (sizeInBytes >= KB)
{
double sizeInKB = (double)sizeInBytes / KB;
return string.Format("{0:F2} KB", sizeInKB);
}
else
{
return string.Format("{0} bytes", sizeInBytes);
}
}
}
}
``````
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,865 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,534 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,500 questions