C# 极限编程冒险中的勘误表?

发布于 2024-07-09 21:44:42 字数 388 浏览 6 评论 0原文

我正在尝试阅读 Ron Jeffries 的《C# 极限编程冒险》。 然而,我在第 3 章中陷入了困境,因为代码没有而且不能按照作者所说的那样做。

基本上,文本说我应该能够在启用自动换行的文本框中写入一些文本。 如果我随后将光标移动到中间行并按 Enter 键,则代码应重新显示光标之前的行,添加几行和一组 HTML 段落标记,然后附加其余行。 该代码与文本不匹配,因为它使用了 textbox.lines 属性。 好吧,无论文本框中有多少行自动换行,在按下回车键之前,Lines 属性中都只有一行。 因此,代码应该“将其余行复制到缓冲区中”的说法对我来说似乎是错误的。

我很感激任何有这本书经验的人告诉我我在读什么或做错了什么!

谢谢。

始祖猛禽

I'm trying to work my way through Ron Jeffries's Extreme Programming Adventures in C#. I am stuck, however, in Chapter 3 because the code does not, and cannot, do what the author says it does.

Basically, the text says that I should be able to write some text in a word-wrap enabled text box. If I then move the cursor to an intermediate line and hit enter, the code should re-display the lines before the cursor, add a couple of lines and a set of HTML paragraph tags, then append the rest of the lines. The code doesn't match the text because it uses the textbox.lines property. Well, no matter how many word-wrapped lines there are in a text box, there's only ONE line in the Lines property until you hit a carriage return. So, the statement that the code should, "Copy the rest of the lines into the buffer" appears wrong to me.

I'd appreciate anybody having experience with the book telling me what I'm reading, or doing, wrong!

Thanks.

EoRaptor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迎风吟唱 2024-07-16 21:44:43

尝试直接向罗恩·杰弗里斯发送电子邮件。 我有这本书——在某处,但我不记得它不起作用。 他的电子邮件地址是 acm dot org 的 ronjeffries,并将 [Ron] 放在主题行中。

(对于那些想知道的人 - 他的电子邮件信息直接来自他的网站 欢迎页面

Try emailing Ron Jeffries directly. I have the book - somewhere, but I don't remember it not working. His email address is ronjeffries at acm dot org and put [Ron] in the subject line.

(And for those wondering - his email info was right from his website Welcome page)

有深☉意 2024-07-16 21:44:43

我也刚刚开始这本书,并且遇到了完全相同的问题,尽管您所包含的代码看起来比我所在的位置更进一步。
发生“下标超出范围”有两个原因,首先,Ron 解释说他只是在测试,因此在编写 CursorLine() 函数之前返回了硬编码值 3,这意味着您认为至少是 4? 正如您所说,需要粘贴文本行,或者可能在运行之前将文本设置为该值,也正如您所说,它们需要回车符才能使 txtbox.Lines 返回字符串数组。
第二个原因甚至在 CursorLine() 已实现之后也会发生,但仅当文本框为空时才会发生,因为 txtbox.Lines 返回 string[0] 但我认为 Ron 正在实现一个“用户故事”,它表示当输入文本时并且用户按下回车键,所以不确定他以后是否修复了这个问题,但可能会发现!

作者确实声明他们正在学习 C#,并将展示开发的优点和全部,这是我选择学习这本书的原因之一,因为我认为它鼓励我开发项目。 我也尝试先编写代码,然后再查看他的解决方案,看看我是否以同样的方式思考,但也许我对 C# 的了解比我自以为的要好一点,或者我完全是垃圾,但我已经注意到一些事情,首先他说重写 OnKeyDown() 不起作用,但我认为他一定感到困惑并尝试在 Form 中执行,而不是从 TextBox 派生并重写那里。
这是我阅读“用户故事”时的代码:

int curPos = txtbox.SelectionStart;
string Wrd = Environment.NewLine + "<P></P>" + Environment.NewLine;              
txtbox.SelectedText = Wrd;
int pl = Environment.NewLine.Length + 3; // "<P>" length is 3
// Put text cursor inbetween <P> tags
txtbox.SelectionStart = curPos + pl;

它的工作方式与 Ron 的代码不同,但这只是我对“用户故事”的解释,不确定如果选择文本应该如何操作,或者如果文本光标在文本中是否要分割行 获取光标行位置,如果没有回车或调整大小,则

同样在“C# 极限编程冒险”中的“我的冒险”中,

txtbox.GetLineFromCharIndex(txtbox.SelectionStart)

无关紧要,
据我所知,我做了一些测试:

txtbox.GetLineFromCharIndex(txtbox.TextLength)

它返回总行数,如果调整文本框的大小,行数会有所不同。

使用 C#,我总是寻找已经存在的解决方案,人们可​​能会为此推荐我,但我认为 MS 已经创建了一种很棒的语言,其中包含很棒的组件,可以完成您期望它们做的事情,因此不必每次都重新创建轮子时间。
尽管就像我说的那样,这还处于本书的早期阶段,也许这些简单的解决方案扩展性还不够,也许 Ron 已经考虑到了这一点,尽管他确实提到了先让它工作,然后担心以后更多的是 XP 方式。

沃伦.

I've also just started this book and had exactly the same problem although the code you have included looks further along than where I am.
The 'subscript out of range' occurred for 2 reasons, first as Ron explains he was just testing and so returned a hard-coded value of 3 before he wrote the CursorLine() function, which means you I think at least 4? lines of text which as you say need to be pasted in, or maybe set the text to this value before running, also as you say they need to have carriage returns to make txtbox.Lines return an array of strings.
The second reason occurs even after CursorLine() has been implemented but only happens if the text box is empty as txtbox.Lines returns string[0] but I think Ron is implementing a 'User Story' which says that when text has been entered and user presses enter, so not sure if he fixes this later, but will probably find out!

The author's do state that they are learning C# and will show the development wart's and all, which is one of the reasons I have chosen to study this book as I think it is encouraging me to develop projects. I also try to do the code first before looking at his solutions to see if I'm thinking the same way, but maybe I know C# a little better than I give myself credit for, or I'm completly crap, but I've noticed a few things, first he says that overriding OnKeyDown() doesn't work, but I think he must have got confused and tried to do in Form, instead of deriving from TextBox and overriding there.
This was my code when reading the 'User Story':

int curPos = txtbox.SelectionStart;
string Wrd = Environment.NewLine + "<P></P>" + Environment.NewLine;              
txtbox.SelectedText = Wrd;
int pl = Environment.NewLine.Length + 3; // "<P>" length is 3
// Put text cursor inbetween <P> tags
txtbox.SelectionStart = curPos + pl;

It works differently to Ron's code, but was just my interpretation of the 'User Story' and not sure how should act if text is selected or wether to split line if text cursor in the middle etc.

Also in 'My Adventures' in Extreme Programming Adventures in C#

txtbox.GetLineFromCharIndex(txtbox.SelectionStart)

gets the cursor line position and doesn't matter if no carriage returns or resized,
as far as I can tell, I done little test with:

txtbox.GetLineFromCharIndex(txtbox.TextLength)

which returns the total amount of lines, which will vary if you resize the text box.

Using C# I always look for solutions which already exsist and people may slate me for this but I think MS have created a great language with great components which do what you expect them to do, so don't have to re-create the wheel each time.
Although like I say it's early days in this book and perhaps these simple solutions aren't extensible enough and maybe Ron's taking that into account, although he did mention just get it working then worry about that later is more the XP way.

Warren.

抽个烟儿 2024-07-16 21:44:43
print("using System;

使用系统集合;
使用 System.Collections.Generic;
使用系统文本;

命名空间 NotepadOne {

公共类 TextModel {

private String[] lines;
private int selectionStart;
private int cursorPosition;

public TextModel() {

}

public String[] Lines {
  get {
    return lines;
  }
  set {
    lines = value;
  }
}

public int SelectionStart {
  get {
    return selectionStart;
  }
  set {
    selectionStart = value;
  }
}

public int CursorPosition {
  get {
    return cursorPosition;
  }
  set {
    cursorPosition = value;
  }
}

public void InsertControlPText() {
  lines[lines.Length - 1] += "ControlP";
}

public void InsertParagraphTags() {
  int cursorLine = CursorLine();
  String[] newlines = new String[lines.Length + 2];
  for (int i = 0; i <= cursorLine; i++) {
    newlines[i] = lines[i];
  }
  newlines[cursorLine + 1] = "";
  newlines[cursorLine + 2] = "<P></P>";
  for (int i = cursorLine + 1; i < lines.Length; i++) {
    newlines[i + 2] = lines[i];
  }
  lines = newlines;
  selectionStart = NewSelectionStart(cursorLine + 2);
}

private int CursorLine() {
  int length = 0;
  int lineNr = 0;
  foreach (String s in lines) {
    if (length <= SelectionStart && SelectionStart <= length + s.Length + 2) {
      break;
      length += s.Length + Environment.NewLine.Length;
      lineNr++;
    }
    lineNr++;
  }
  return lineNr;
}

private int NewSelectionStart(int cursorLine) {
  int length = 0;
  for (int i = 0; i < cursorLine; i++) {
    length += lines[i].Length + Environment.NewLine.Length;
  }
  return length + 3;
}

}
}
");

通过在文本框中按 Enter 键来调用 InsertParagraphTags 方法。

顺便说一句,这里的中断是,如果您尝试在文本末尾按 Enter 键,则会出现下标超出范围错误。我确信我可以弄清楚如何解决这个问题,但我的代码看起来不像他的代码;这就是我想要学习的

print("using System;

using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace NotepadOne {

public class TextModel {

private String[] lines;
private int selectionStart;
private int cursorPosition;

public TextModel() {

}

public String[] Lines {
  get {
    return lines;
  }
  set {
    lines = value;
  }
}

public int SelectionStart {
  get {
    return selectionStart;
  }
  set {
    selectionStart = value;
  }
}

public int CursorPosition {
  get {
    return cursorPosition;
  }
  set {
    cursorPosition = value;
  }
}

public void InsertControlPText() {
  lines[lines.Length - 1] += "ControlP";
}

public void InsertParagraphTags() {
  int cursorLine = CursorLine();
  String[] newlines = new String[lines.Length + 2];
  for (int i = 0; i <= cursorLine; i++) {
    newlines[i] = lines[i];
  }
  newlines[cursorLine + 1] = "";
  newlines[cursorLine + 2] = "<P></P>";
  for (int i = cursorLine + 1; i < lines.Length; i++) {
    newlines[i + 2] = lines[i];
  }
  lines = newlines;
  selectionStart = NewSelectionStart(cursorLine + 2);
}

private int CursorLine() {
  int length = 0;
  int lineNr = 0;
  foreach (String s in lines) {
    if (length <= SelectionStart && SelectionStart <= length + s.Length + 2) {
      break;
      length += s.Length + Environment.NewLine.Length;
      lineNr++;
    }
    lineNr++;
  }
  return lineNr;
}

private int NewSelectionStart(int cursorLine) {
  int length = 0;
  for (int i = 0; i < cursorLine; i++) {
    length += lines[i].Length + Environment.NewLine.Length;
  }
  return length + 3;
}

}
}
");

The InsertParagraphTags method is called by pressing the enter key in the textbox.

BTW, the break here is that there is a subscript out of range error if you try to hit enter at the end of the text. I'm sure I could figure out how to get around this but then my code won't look like his code; which is what I'm trying to learn.

Randy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文