guys i need a richtextbox which will show the data from database but when user want to add additional text, it will only allow the user to append text to the end of the last character.

have you seen anything like what i say?

Recommended Answers

All 5 Replies

You can add different RichTextBox to handle this, and append data to the RichTextBox programtically.

there will be only one richtextbox in the user interface

Let the richtextbox not editable, and handle the event of keyboard, show characters by the end.

May be there are some open sources for you. textbox

i attach the solution too.

i did it here is the code :

SmartAppendOnlyTextBox.cs :

#region Name Spaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion
namespace SmartControls
{
	public partial class SmartAppendOnlyTextBox : TextBox
	{
		#region Fields
		private bool textAssigned=false;
		private string originalText = string.Empty;
		private int originalTextLength = 0;
		private const string alert = "You can't modify previous comments, please append yours to the end.";
		#endregion

		#region Constructor
		public SmartAppendOnlyTextBox()
		{
			InitializeComponent();
		}
		#endregion

		#region Event Handlers


		protected override void OnTextChanged(EventArgs e)
		{
			base.OnTextChanged(e);
			// initial text assignment condition
			if (!textAssigned && !string.IsNullOrEmpty(Text))
			{
				originalTextLength = Text.Length;
				originalText = Text;
				textAssigned = true;
			}
		}
		
	
		protected override void OnKeyDown(KeyEventArgs e)
		{
			base.OnKeyDown(e);
			if (IsInNonEditableArea() && !(e.Alt || e.Control || e.Shift))
			{
				MessageBox.Show(alert);
				Text = originalText;
			}
		}

		private const int WM_PASTE = 0x0302;
		private const int WM_CUT = 0x0300;

		protected override void WndProc(ref Message m)
		{
			if ((m.Msg == WM_PASTE || m.Msg == WM_CUT) && IsInNonEditableArea())
			{
				MessageBox.Show(alert);
			}
			else
			{
				base.WndProc(ref m);
			}
		}
		#endregion

		#region Helper Methods

		private bool IsInNonEditableArea()
		{
			bool isNoneEditable = true;
			if (SelectionStart > (originalTextLength - 1))
				isNoneEditable = false;
			return isNoneEditable;
		}
		
		#endregion

	}
}

SmartAppendOnlyTextBox.designer.cs :

namespace SmartControls
{
	partial class SmartAppendOnlyTextBox
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Component Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}

		
		#endregion
	}
}

there was a problem with the previous attachment, here is the corrected one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.