这段代码远远超出了您的要求,但您应该从代码中获得一些如何管理 RichTextBox 中的输入的想法 该代码限制 RTB 中的行数并计算输入的字符
public partial class frmADE : Form
{
public string dbName = "People.db";
int rtbTotLen;
int index;
int line;
int firstChar;
int column;
string result;
bool YN;// Use with Edit Notes
bool showMe;// Use with Edit Notes
bool noEdit;// Use with Adding New Notes
bool noEnterKey;// Use with Add & Edit in Cmd ?
public UserData userData;
public frmADE()
{
InitializeComponent();
}
private void rtbInfo_KeyPress(object sender, KeyPressEventArgs e)
{
if (frmStart.doWhat == 4)// Delete Trap
{
tbMessage.ForeColor = Color.Blue;
tbMessage.Text = "NO Editing Click DELETE to Remove Information";
return;
}
if (frmStart.doWhat == 2)// View Notes Trap
{
tbMessage.ForeColor = Color.Blue;
tbMessage.Text = "No Editing Can ONLY View Notes";
return;
}
if (frmStart.doWhat == 1)
{
if (noEdit == true) // Adding Trap
{
tbMessage.Text = "Press ESC to Edit - Enter Key LOCKED";
return;
}
}
if (frmStart.doWhat == 3)
{
if (YN == true)// Editing Trap
{
rtbInfo.ReadOnly = true;
tbMessage.Text = "Press ESC to Edit - Enter Key LOCKED";
return;
}
}
if (frmStart.doWhat == 1)// Adding New Notes <====
{
rtbTotLen = rtbInfo.TextLength;//=============== testing
index = rtbInfo.SelectionStart = rtbInfo.Rtf.Length;
line = rtbInfo.GetLineFromCharIndex(index);
if (rtbTotLen == 135 | line > 3)
{
tbMessage.Text = "";
//rtbInfo.ReadOnly = false;
string message = "Select YES to Edit Notes" + '\n' + "Select NO to Exit and Save";
string title = "Adding New Notes";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show(message, title, buttons);
if (result == DialogResult.Yes)
{
SendKeys.Send("{BACKSPACE}");
rtbInfo.ScrollToCaret();
rtbInfo.Focus();
noEdit = true;
// showMe = true;
}
if (result == DialogResult.No)
{
noEdit = true;
tbMessage.Text = "Save Data First then Edit or CLOSE";
btnSave.Focus();
return;
}
}
}
if (frmStart.doWhat == 3)// Edit Notes <===== Set YN true here showME set to FALSE here
{
rtbTotLen = rtbInfo.TextLength;
index = rtbInfo.SelectionStart = rtbInfo.Rtf.Length;
line = rtbInfo.GetLineFromCharIndex(index);
if (rtbTotLen == 135 | line >= 4) //Edit Notes Message Box <=== // rtbTotLen == 135 ||
{
string title = "Edit Notes";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
string message = "Select YES to Edit Notes" + '\n' + "Select NO to Update or Close";
DialogResult result = MessageBox.Show(message, title, buttons);
if (result == DialogResult.Yes)
{
rtbInfo.ScrollToCaret();
rtbInfo.Focus();
YN = true;
//showMe = false;
}
if (result == DialogResult.No)
{
tbMessage.Text = "Press ESC to Edit Enter Key LOCKED";
rtbInfo.ReadOnly = true;
btnUpdate.Focus();
YN = true;
}
}
}
// Put if dowhat 1 or 3 here or make this a method and call it ?
index = rtbInfo.SelectionStart;
line = rtbInfo.GetLineFromCharIndex(index);
rtbTotLen = rtbInfo.TextLength;
// Get the column.
firstChar = rtbInfo.GetFirstCharIndexFromLine(line);
column = index - firstChar;
tbEmail.Text = "idx " + index + " line " + line + " fChar " + firstChar + " col " + column + "";
if (line == 0)
{
tbMessage.Text = "Total of " + (32 - column) + " more char can be entered on line " + (line + 1) + "";
if (column == 32)
{
SendKeys.Send("{ENTER}");
rtbInfo.ScrollToCaret();
rtbInfo.Focus();
}
}
else if (line == 1)
{
tbMessage.Text = "Total of " + (32 - column) + " more char can be entered on line " + (line + 1) + "";
if (column == 32)
{
SendKeys.Send("{ENTER}");
rtbInfo.ScrollToCaret();
rtbInfo.Focus();
}
}
else if (line == 2)
{
tbMessage.Text = "Total of " + (32 - column) + " more char can be entered on line " + (line + 1) + "";
if (column == 32)
{
SendKeys.Send("{ENTER}");
rtbInfo.ScrollToCaret();
rtbInfo.Focus();
}
}
else if (line == 3)
{
tbMessage.Text = "Total of " + (32 - column) + " more char can be entered on line " + (line + 1) + "";
if (column == 32)
{
SendKeys.Send("{ENTER}");
rtbInfo.ScrollToCaret();
rtbInfo.Focus();
}
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (frmStart.doWhat == 1 || frmStart.doWhat == 3)
{
if (keyData == Keys.Enter & noEdit == true | noEnterKey == true & frmStart.doWhat == 1)// <== Add New
{
tbMessage.Text = "Press ESC to Edit - Enter Key LOCKED";
return true;
}
if (keyData == Keys.Enter & YN == true | noEnterKey == true & frmStart.doWhat == 3)// <== Edit Function
{
tbMessage.Text = "Press ESC to Edit - Enter Key LOCKED";
return true;
}
if (keyData == Keys.Escape)
{
noEdit = false;
YN = false;
noEnterKey = false;
rtbInfo.Text = rtbInfo.Text.Replace('\n', '\v');
rtbInfo.ScrollToCaret();
rtbInfo.Focus();
SendKeys.Send("{BACKSPACE}");
rtbInfo.SelectionStart = rtbInfo.Rtf.Length;
rtbInfo.SelectionLength = 0;
rtbInfo.ReadOnly = false;
tbMessage.Text = "Edit with Backspace The Enter Keys Locked";
//noEnterKey = true;//// Entry Key is Disabled Backspace Key not involved ? I HOPE
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void frmADE_Load(object sender, System.EventArgs e)
{
if (frmStart.doWhat == 1)// Add Contact
{
btnSave.Visible = true;
ActiveControl = tbFirstName;
tbMessage.Text = "Enter Friends Contact Information";
}
if (frmStart.doWhat == 2)// View Selected Contact
{
readFriendsData();
setReadOnly();
ActiveControl = btnClose;
tbMessage.Text = "View Friends Contact Information";
}
if (frmStart.doWhat == 3)// Update Selected Contact
{
tbFirstName.SelectionStart = tbFirstName.Text.Length;
tbFirstName.Focus();
btnUpdate.Visible = true;
readFriendsData();
rtbInfo.SelectionStart = rtbInfo.Rtf.Length;
rtbInfo.SelectionLength = 0;
//if (showMe == true)
//{
//tbMessage.Text = "To Edit Notes Use Backspace Key";
//}
}
if (frmStart.doWhat == 4)// Delete Selected Contact
{
ActiveControl = btnDelete;
btnDelete.Visible = true;
readFriendsData();
setReadOnly();
tbMessage.Text = "Remove Friends Contact Information";
}
}