110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication35
{
public partial class Form1 : Form
{
String Prompt = "MYDOS>";
public Form1()
{
InitializeComponent();
WriteProtectedPrompt();
}
void WriteProtectedPrompt()
{
int Start = richTextBox1.TextLength;
richTextBox1.AppendText(Prompt);
richTextBox1.SelectionStart = Start;
richTextBox1.SelectionLength = richTextBox1.TextLength - Start;
richTextBox1.SelectionFont = new Font("宋体", 10);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionProtected = true; // 提示符部分是受保护的,无法修改
richTextBox1.SelectionStart = richTextBox1.TextLength;
}
void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
richTextBox1.SelectionColor = Color.Black;
if (e.KeyData == Keys.Up)
e.SuppressKeyPress = true; // 屏蔽向上按键
if (e.KeyData == Keys.Left && richTextBox1.TextLength > 1)
{
int Current = richTextBox1.SelectionStart;
richTextBox1.SelectionStart = richTextBox1.SelectionStart - 1;
richTextBox1.SelectionLength = 1;
if (richTextBox1.SelectionProtected) // 如果是向左按键,则检查左边那个字符是不是受保护的
e.SuppressKeyPress = true; // 如果是受保护的就屏蔽这次向左操作,不允许向左
richTextBox1.SelectionStart = Current;
richTextBox1.SelectionLength = 0;
}
}
void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.TextLength > 0 && richTextBox1.Text[richTextBox1.TextLength - 1] == '\n')
{
if (richTextBox1.Text.ToUpper().LastIndexOf(@"CD C:\") > 0) // 随便举个例子
Prompt = @"MYDOS C:\>"; // 修改提示符
WriteProtectedPrompt();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication31
{
public partial class Form1 : Form
{
TextBox Prompt = new TextBox();
TextBox Edit = new TextBox();
public Form1()
{
InitializeComponent();
this.Controls.Clear();
this.Size = new Size(500, 500);
Prompt.Multiline = true;
Prompt.ReadOnly = false;
Prompt.TabStop = false;
Prompt.BorderStyle = BorderStyle.None;
Prompt.Parent = this;
Prompt.Size = new Size(50, this.Size.Height);
Prompt.Location = new Point(0, 0);
Prompt.Text = "Prompt>";
Edit.Multiline = true;
Edit.ScrollBars = ScrollBars.Vertical;
Edit.BorderStyle = BorderStyle.None;
Edit.Parent = this;
Edit.Size = new Size(this.ClientSize.Width - Prompt.ClientSize.Width, this.ClientSize.Height);
Edit.Location = new Point(Prompt.Width, 0);
Edit.KeyDown += new KeyEventHandler(Edit_KeyDown);
Edit.TextChanged += new EventHandler(Edit_TextChanged);
Edit.Focus();
}
void Edit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Up)
e.Handled = true;
}
void Edit_TextChanged(object sender, EventArgs e)
{
if (Edit.Lines.Length > Prompt.Lines.Length)
Prompt.Text += Environment.NewLine + "Prompt>";
else if (Edit.Lines.Length < Prompt.Lines.Length)
{
Edit.Text += Environment.NewLine;
Edit.SelectionStart = Edit.TextLength;
}
}
}
}