换行时自动缩进 [英] Auto Indent when a new line is made

查看:392
本文介绍了换行时自动缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个HTML编辑器的学校项目.现在,我在换行时如何进行自动缩进存在问题.压痕的间距将与其上方的线相同.我希望我的问题足够清楚.

I'm working on a school project which is a HTML Editor. Right now, I've a problem on how to make an auto indentation whenever I make a new line. The spacing of indentation would be the same as the line above it. I hope my problem clear enough.

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Return)
   {
       richTextBox1.Text = //this part, i don't know how to make it
   }

推荐答案

与其在此处发布完整的代码,不如将其引导到逻辑上,以便您将其从概念应用于实现.我认为这将有助于建立对(a)一般如何解决编码问题和(b)具体解决该问题所需的代码的更扎实的理解.

Rather than post complete code here, I'm going to try and steer you toward the logic so that you can take it from concept to implementation. I think that will help build a more solid understanding of (a) how to approach a coding problem in general and (b) the code required to solve this problem specifically.

首先,将您的问题分解为一系列独立的步骤...

First, break your problem down into a set of self-contained steps...

  • 找到上一行
  • 找出该行以多少空格/制表符
  • 将相同数量的空格/制表符追加到最后一行

第二,了解您的方法的局限性,并确保您的要求与这些局限性不相容.

Second, understand the limitations of your approach and make sure your requirements aren't incompatible with those limitations.

  • 假设:此解决方案适用于在文本框末尾添加的新行.

然后,开始弄清楚如何以所使用的任何语言来实现逻辑.第一步是查找上一行".我们如何找到上一行?向后搜索直到碰到行终止符如何?查看您使用的语言是否可以自动执行某些操作(提示,C#可以... Google"LastIndexOf"并注意"StartIndex"参数).如果没有,请自行实现.

Then, start to figure out how to implement your logic in whatever language you are using. The first step was "Find the previous line." How do we find the previous line? How about searching backward until you hit a line terminator? Look to see if the language you're using has something to do that automatically (hint, C# does... Google "LastIndexOf" and pay attention to the "StartIndex" parameter) If not, implement it yourself.

当您分解问题时,可以搜索诸如SO之类的站点来寻找更具针对性和针对性的内容,例如如何向后搜索字符串",而不是像您所问的那样广泛的问题.

When you break the problem down you can search sites like SO for more focused, targeted things like "How to search a string backward" rather than broad questions like the one you asked.

祝您工作顺利,我给您的建议不专注于代码.专注于导致代码的思考过程,然后其他事情就会解决.

Good luck with your assignment, my advice to you don't focus on the code. Focus on the thought process that leads to the code and the other things will fall into place.

这篇关于换行时自动缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆