换行时自动缩进

Auto Indent when a new line is made [closed]
2021-05-11
  •  译文(汉语)
  •  原文(英语)

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

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
   }
速聊1:
你都尝试了些什么?您需要缩进制表符或空格吗?
速聊2:
嗨,哈菲兹,到目前为止,您的代码是什么?
速聊3:
实际上,当与标签结合使用时,您的问题就很有意义了.感觉就像是1)我要做作业,2)你将得到免费的工作,实际上并不在乎从结果中学到什么.
速聊4:
先生/女士,我不是在开玩笑.
速聊5:
我尝试了使用按钮,但仍然无法正常工作
解决过程1

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

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

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

其次,了解您的方法的局限性,并确保您的要求与这些局限性不兼容.

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

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

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

祝您工作顺利,我对您的建议不专注于代码.专注于导致代码的思考过程,其他事情就会落到位.

速聊1:
谢谢您,先生/女士.那对我有很大帮助!我从来没有考虑过如何搜索解决方案的方法.
解决过程2

您说已经搜索了两天.您搜寻了什么?How to implement automatic indentation?好吧,这可能不会导致您找到解决方案.(编辑:实际上,即使这样也会给您一些结果...)

尝试将问题分成几部分.自动缩进需要哪些步骤?注意,这不限于用户按下Enter键的情况!一旦确定了这些步骤,就可以对每个步骤进行单独搜索.这样,您将学到相当多的语言和框架功能,这些将比完整的代码解决方案有更多帮助.

首先,可能的步骤可能包括:

  • 确定何时在文本中添加新行/某些字符
  • 确定何时将文本粘贴到文本框
  • 将文字附加到现有文字
  • 等等...
速聊1:
先生/女士,谢谢您的建议.每当尝试搜索解决方案时,我都不会使用"如何去..",因为我们不允许使用"如何去..","如何去.."等等.再次,谢谢您,先生/女士

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
   }
Talk1:
What have you tried so far? Do you need to indent with tabs or whitespaces?
Talk2:
Hi Hafiz, what's your code so far?
Talk3:
Actually, when combined with the tags your question makes complete sense. It just feels like 1) I'd be doing a homework assignment and 2) you'd be getting free work and don't actually care about learning anything from the result.
Talk4:
Sir/Madam, I'm not joking.
Talk5:
I've tried use button but still not working
Solutions1

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...

  • Find the previous line
  • Figure out how many spaces/tabs that line begins with
  • Append the same number of spaces/tabs to the last line

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

  • Assumption: This solution is intended for new lines added at the END of a textbox.

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.

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.

Talk1:
Thank you, sir/madam. That helps me a lot! I've never think about that way on how to search for solution(s).
Solutions2

You say that you have searched for this for two days. What did you search for? How to implement automatic indentation? Well, this will probably not lead you to a solution. (EDIT: Actually, even this will give you some results...)

Try to split the problem into pieces. What steps are required for automatic indentation? Note that this is not limited to the case when the user presses the Enter key! Once you have identifed those steps, you can start individual searches for each step. This way, you will learn quite some language and framework features which will help you a lot more than a complete code solution.

As a starter, possible steps might include:

  • determine when a new line/certain character is added to the text
  • determine when text is pasted to the textbox
  • append text to the existing text
  • etc...
Talk1:
Thank you for your advice, sir/madam. I never use "How to.." whenever I try to search for solution(s) since we're not allowed to use "How to.." "How can I.." and more. and again, Thank you, Sir/Madam
转载于:https://stackoverflow.com/questions/20217407/auto-indent-when-a-new-line-is-made

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入