I'm trying to Redraw TextBox Text
but my problem is
1- when the text exceeds the textbox width in case single line I'm still getting the first visible strings which fit the width of the textbox I want to continue drawing all the chars as on the textbox
2- for multiline I have on line missing from the top when the word length is bigger than one line in case RTL.
my code works very well with multiline LTR I Want to fix RTL And Single Line if you can help Me I will be thankful .
her is my code
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 Microsoft_TextBox
{
public partial class Form9 : Form
{
private System.Windows.Forms.TextBox textbox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.RadioButton radioButton5;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Button button1;
public Form9()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (StringFormat sf = new StringFormat())
{
Font font = textbox1.Font;
bool isRTL = textbox1.RightToLeft == RightToLeft.Yes;
Brush brush = new SolidBrush(textbox1.ForeColor);
Point startPoint = new Point((int)0, -(int)0);
Point endPoint = new Point((int)panel1.Width, (int)(panel1.Height));
int visibleLines = panel1.Height / font.Height;
int startIndex = textbox1.GetCharIndexFromPosition(startPoint);
int endIndex = textbox1.GetCharIndexFromPosition(endPoint);
int startLine, endLine;
int charIndex = textbox1.SelectionStart;
int lineNumber = textbox1.GetLineFromCharIndex(charIndex);
textBox2.Text = lineNumber.ToString();
textBox3.Text = startIndex.ToString();
textBox4.Text = (textbox1.GetCharIndexFromPosition(new Point(0, panel1.Height / visibleLines / 4))).ToString();
textBox6.Text = endIndex.ToString();
int lastVisibleLine = textbox1.GetLineFromCharIndex(endIndex);
if (isRTL)
{
try
{
if ((textbox1.GetCharIndexFromPosition(new Point(0, panel1.Height / visibleLines / 4)) - textbox1.Lines[0].Length) <= 0)
{
startLine = textbox1.GetLineFromCharIndex(startIndex);
endLine = textbox1.GetLineFromCharIndex(endIndex);
textBox5.Text = "";
textBox5.Text = "hello we are here";
}
else
{
startLine = textbox1.GetLineFromCharIndex(startIndex) + 1;
endLine = textbox1.GetLineFromCharIndex(endIndex);
textBox5.Text = "";
textBox5.Text = "hello we are not here";
}
}
catch
{
startLine = textbox1.GetLineFromCharIndex(startIndex);
endLine = textbox1.GetLineFromCharIndex(endIndex);
textBox5.Text = "";
}
}
else
{
if (textbox1.GetCharIndexFromPosition(new Point(0, panel1.Height / visibleLines / 4)) == 0)
{
startLine = textbox1.GetLineFromCharIndex(startIndex);
endLine = textbox1.GetLineFromCharIndex(endIndex);
}
else
{
startLine = textbox1.GetLineFromCharIndex(startIndex) + 1;
endLine = textbox1.GetLineFromCharIndex(endIndex);
}
}
for (int line = startLine; line <= endLine; line++)
{
int lineStartIndex = textbox1.GetFirstCharIndexFromLine(line);
int lineEndIndex = textbox1.GetFirstCharIndexFromLine(line + 1);
if (lineEndIndex == -1)
lineEndIndex = textbox1.Text.Length;
string lineText = textbox1.Text.Substring(lineStartIndex, lineEndIndex - lineStartIndex);
if (textbox1.Text == "\n" || textbox1.Text == "\r" || textbox1.Text == "\t" || textbox1.Text == "\f")
{
continue;
}
if (isRTL)
{
sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
else
{
sf.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
}
SizeF textSize = e.Graphics.MeasureString(lineText, font);
float xCoordinate = startPoint.X;
switch (textbox1.TextAlign)
{
case HorizontalAlignment.Center:
if (textbox1.RightToLeft == RightToLeft.Yes)
{
xCoordinate = (panel1.Width - textSize.Width) / 2; }
else
{
xCoordinate = (panel1.Width - textSize.Width) / 2;
}
break;
case HorizontalAlignment.Right:
if (textbox1.RightToLeft == RightToLeft.Yes)
{
xCoordinate = 0; }
else
{
xCoordinate = panel1.Width - textSize.Width;
}
break;
case HorizontalAlignment.Left:
if (textbox1.RightToLeft == RightToLeft.Yes)
{
xCoordinate = panel1.Width - textSize.Width; }
else
{
}
break;
default:
break;
}
TextFormatFlags flags = (TextFormatFlags)StringFormatFlags.NoWrap;
if (isRTL)
{
flags |= TextFormatFlags.RightToLeft;
}
TextRenderer.DrawText(e.Graphics, lineText, font, new Point((int)xCoordinate, startPoint.Y), textbox1.ForeColor, flags );
startPoint.Y += font.Height;
}
}
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
panel1.Invalidate();
}
private void textbox1_KeyDown(object sender, KeyEventArgs e)
{
panel1.Invalidate();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
textbox1.TextAlign = HorizontalAlignment.Left;
panel1.Invalidate();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
textbox1.TextAlign = HorizontalAlignment.Center;
panel1.Invalidate();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
textbox1.TextAlign = HorizontalAlignment.Right;
panel1.Invalidate();
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
textbox1.RightToLeft = RightToLeft.Yes;
panel1.Invalidate();
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
textbox1.RightToLeft = RightToLeft.No;
panel1.Invalidate();
}
private void transparentRichTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form9));
this.textbox1 = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.textBox2 = new System.Windows.Forms.TextBox();
this.radioButton5 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.textbox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textbox1.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Underline);
this.textbox1.ForeColor = System.Drawing.Color.YellowGreen;
this.textbox1.Location = new System.Drawing.Point(194, 71);
this.textbox1.Multiline = true;
this.textbox1.Name = "textbox1";
this.textbox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.textbox1.Size = new System.Drawing.Size(246, 104);
this.textbox1.TabIndex = 0;
this.textbox1.Text = "1111111111111111111111222222222222222222222233333333333333333333334444444444444444444444555555555555555555555566666666666666666666667777777777777777777777888888888888888888888899999999999999999999990000000000000000000000";
this.textbox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textbox1.TextChanged += new System.EventHandler(this.textbox1_TextChanged);
this.textbox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textbox1_KeyDown);
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.Location = new System.Drawing.Point(194, 197);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(246, 104);
this.panel1.TabIndex = 1;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
this.textBox2.Location = new System.Drawing.Point(194, 340);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(213, 24);
this.textBox2.TabIndex = 2;
this.radioButton5.AutoSize = true;
this.radioButton5.Location = new System.Drawing.Point(598, 280);
this.radioButton5.Name = "radioButton5";
this.radioButton5.Size = new System.Drawing.Size(141, 21);
this.radioButton5.TabIndex = 10;
this.radioButton5.TabStop = true;
this.radioButton5.Text = "Right To Left Flase";
this.radioButton5.UseVisualStyleBackColor = true;
this.radioButton5.CheckedChanged += new System.EventHandler(this.radioButton5_CheckedChanged);
this.radioButton4.AutoSize = true;
this.radioButton4.Location = new System.Drawing.Point(598, 242);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(140, 21);
this.radioButton4.TabIndex = 9;
this.radioButton4.TabStop = true;
this.radioButton4.Text = "Right To Left True";
this.radioButton4.UseVisualStyleBackColor = true;
this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged);
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(598, 204);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(61, 21);
this.radioButton3.TabIndex = 8;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "Right";
this.radioButton3.UseVisualStyleBackColor = true;
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(598, 166);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(70, 21);
this.radioButton2.TabIndex = 7;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "Center";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(598, 128);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(52, 21);
this.radioButton1.TabIndex = 6;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "Left";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
this.textBox3.Location = new System.Drawing.Point(194, 370);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(213, 24);
this.textBox3.TabIndex = 12;
this.textBox4.Location = new System.Drawing.Point(194, 400);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(213, 24);
this.textBox4.TabIndex = 13;
this.textBox5.Font = new System.Drawing.Font("Tahoma", 12F);
this.textBox5.ForeColor = System.Drawing.Color.YellowGreen;
this.textBox5.Location = new System.Drawing.Point(580, 333);
this.textBox5.Multiline = true;
this.textBox5.Name = "textBox5";
this.textBox5.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.textBox5.Size = new System.Drawing.Size(326, 105);
this.textBox5.TabIndex = 14;
this.textBox5.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBox6.Location = new System.Drawing.Point(413, 370);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(100, 24);
this.textBox6.TabIndex = 15;
this.button1.Location = new System.Drawing.Point(102, 351);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 16;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(800, 477);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.radioButton5);
this.Controls.Add(this.radioButton4);
this.Controls.Add(this.radioButton3);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.textbox1);
this.Name = "Form9";
this.Text = "Form9";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
}
|