记事本状态栏代码。救命! [英] Notepad Statusbar code. Help!

查看:82
本文介绍了记事本状态栏代码。救命!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我需要克隆Windows记事本,但我遇到了一些问题。 Windows记事本有一个状态栏,指示闪烁光标的当前行和列。我一直在做一些研究,我遇到了以下代码,我做了一些修改:



Hello,
I'm required to clone Windows Notepad but I've run into a bit of a problem. The windows note pad has a status bar that indicates the current line and column of the blinking cursor. I've been doing some research and I ran into the following code, to which I made some changes:

Public Sub UpdateStatusBar()
       Dim index As Integer = TextBox1.SelectionStart
       Dim currentLine As Integer = TextBox1.GetLineFromCharIndex(index)
       Dim currentColumn As Integer = index - TextBox1.GetFirstCharIndexFromLine(currentLine)

       tss1.Text = "Line " + (currentLine + 1).ToString + ", column " + (currentColumn + 1).ToString
   End Sub





我在textbox.text_changed事件上调用此sub并且它完美地运行我正在写作。我也在textbox_keydown上调用它,以便在用箭头键移动控件时更新状态栏。但在这里搞砸了。当我在文本中移动时,行和列值总是错误的。我试着玩一下将1或2或0添加到例如key.left按下的值下,但我还没想出一个算法,它代表光标所在的行和列。我需要它才能像窗户记事本。想法?任何人?



如果闪烁的光标位置可以以某种方式被修复,我也认为这样做会相当容易,但我不知道这是否可行。我也没有找到关于这个主题的任何内容。



谢谢



I call this sub on textbox.text_changed event and it works perfectly while I'm writing. I also call it on textbox_keydown so as to update the status bar when I move around the control with the arrow keys. But here it gets messed up. When I move around the text the line and column values are always wrong. I tried playing around adding 1 or 2 or 0 to the value under say for example key.left pressed but i haven't figured out an algorith the represents exactly what line and column the cursor is in. I need it to work just like the windows note pad. Ideas? anybody?

I also think it would be fairly easier to do if the blinking cursor position could be retreived somehow, but I don't know if that's possible. I haven't found anything on that subject either.

Thanks

推荐答案

问题不是那个你需要抵消任何东西,这是你在错误的时间做的。这有点难以理解,但从根本上说,您要么需要了解.NET框架(和Windows)的工作方式,或者改变您的工作方式。



最简单的方法是接受用户不需要立即更新状态行上的每一个小更改。实际上,这可能会降低整个应用程序的速度。你是否认为用户会注意到他的光标位置是否每秒更新4次?十次?第一个,是的,也许,第二个?没有。甚至没有一点。



通常情况下,我不推荐使用计时器,但有时它们是最明智的选择,这就是这种情况。在您的应用程序中添加计时器。将Interval设置为100(即1/10秒)并处理Tick事件。在tick事件中,更新您的状态行。删除所有其他更新代码。现在,就像魔术一样,上面的代码可以正常工作!
The problem is not that you need to offset anything, it is that you are doing it at the wrong time. This is a bit difficult to get your head round, but fundamentally you either need to know how the .NET framework (and Windows) works a bit better, or change the way you are doing things.

The easiest way around this is to accept that the user does not need to have every little change updated on the status line immediately. In fact, this can slow down the whole application. Do you think the user would notice if his cursor position was updated 4 times a second? Ten times? The first, yes, maybe, the second? No. Not even slightly.

Normally, I wouldn't recommend a timer, but sometimes they are the most sensible way to go, and this is such a case. Add a timer to your application. Set the Interval to 100 (i.e. 1/10 second) and handle the Tick event. In the tick event, update your status line. Remove all other update code. Now, like magic, your code above will work!


数字错误,因为你打开了Word Wrap。 TextBox中的一行不是屏幕上的可见行。按Enter键终止一行。



在记事本中注意,如果打开了View-> Word Wrap,状态栏将被关闭,这可能是为了这个非常理由。



如果启用状态栏,或者能够启用状态栏,则唯一的一次是关闭Word Wrap。然后,只有这样,行号才算准确。
The numbers are wrong because you have Word Wrap turned on. A line in a TextBox is not the visible lines on the screen. A line is terminated by hitting Enter.

Notice in Notepad that if View->Word Wrap is turned on, the Status Bar is turned off and it's probably for this very reason.

The only time you see the Status Bar enabled, or have the ability to enable the Status Bar, is if Word Wrap is turned off. Then, and only then, are the line number counts accurate.


这里的人!如果您遇到错误,请执行此操作。添加一个StatusStrip,然后添加一个ToolStripStatusLabel。添加一个Timer,启用它,然后将1设置为其间隔。双击定时器并将此代码写在除第一行和最后一行之外的代码上。另外,将tss1.text更改为ToolStripStatusLabel1.text。希望这有帮助!
Here guys! If you're getting errors then do this. Add a StatusStrip and in that add a ToolStripStatusLabel. Add a Timer, enable it and and set 1 as its interval. Double Click the Timer and Write this Above Code Except the First and the Last Line. Also, change tss1.text to ToolStripStatusLabel1.text. Hope this Helps!


这篇关于记事本状态栏代码。救命!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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