Bring cursor to top in richtextbox after enter then

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bring cursor to top in richtextbox after enter then

Post by jvierra »

Normally we would not use the "KeyDown" event as it is intended for key flag detection. For basic character detection we would use "KeyPress" which is cancelable. Both will work but using the intended event prevents future issues as code changes. I recommend learning the basics of WinForms event handling and event cascade order. KeyDown, KeyPress and KeyUp are all executed whenever a key is pressed. This is true for most user activated event streams. The breakdown is intended to solve specific issues of coding controls and forms. Once you understand this then a huge chunk of understanding of coding with WinForms will become obvious.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bring cursor to top in richtextbox after enter then

Post by jvierra »

I just realized that the only way to cancel a key in an RTB is to do it in the "KeyUp" event.

There is no need to use an RTB for what you are doing. This is part of your problem. Never use a control that is more complex than needed. If you are not using RTF features, then just use a textbox. It will save you a lot of headaches in the longer term.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Bring cursor to top in richtextbox after enter then

Post by stevens »

Note: tried a textbox instead of a richtextbox but I don't see any difference. Now the cursor even doesn't go to top once entered (in the textbox).

I then added
$textboxInput.Text = ''
$textboxInput.Clear()
$_.Handled = $true

But cursor doesn't go to top at all now. So I better stick to richtextbox I guess.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bring cursor to top in richtextbox after enter then

Post by jvierra »

Works fine for me. You keep jumping from one thing to another and because you are confused about how it works you keep missing the clues.

Here is a simple demo.
Attachments
Demo-TextClear.psf
(14.27 KiB) Downloaded 45 times
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Bring cursor to top in richtextbox after enter then

Post by Alexander Riedel »

Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bring cursor to top in richtextbox after enter then

Post by jvierra »

Alex, that does not remove the "Enter" character. It just moves the caret. Doing that repeatedly will end up filling the box with "Enter" characters or, if using "Clear()" will always leave an extra "Enter" character.

Look at my demo. It is the best way and in most cases the only way to fully clear a textbox or RTB. It is also the easiest way.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Bring cursor to top in richtextbox after enter then

Post by Alexander Riedel »

I know. But there also seemed confusion as to how to place the caret.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Bring cursor to top in richtextbox after enter then

Post by jvierra »

Alex, that was because of a poorly asked question which had me confused for a while too. What Steven was actually asking was how to clear a box and both have the "Enter" character left in the box. That is why it was not at the to-left but one line down always.

Part of my problem was my poor decoding of the question and then trying to understand why it was an issue. Once I had the right question then the answer was obvious. I also thought to try and get Steve to look first at the documentation for the control and to approach the code from the event stream and not from linear methods which only work in a console script.

Getting techs new to coding in WinForms is hard because techs are not programmers and definitely not Windows programmers. Windows is an object state system. Scripts in the past have not needed to consider asynchronous execution which is what happens at a PS prompt. WinForms just removes all simplicity of a recipe-like code execution because events are async and the order of things from the keyboard, mouse and other devices generate multiple messages from the Windows system which are dispatched as events. Once a coder sees this and checks the event descriptions and examples then things tend to become clear fairly quickly.

Techs do not seem to like to read. All of my techs were like that but, with a little encouragement, they realized that the documentation was an easier and faster way to start to solve almost any problem. The docs also give us more info as we review events, methods and properties repeatedly. My poor techs were stuck carting around a pile of manuals although we usually cached manuals at the customer sites. Still, paper is a pain when looking things up. Computers and search engines turn lookups into a near instantaneous operation. I still rely on docs even with WinForms even though I have been building windows since Windows 0.9. You know, assembler and Borland C. At the Windows 2.0 release we acquired an IDE and compiler called Actor. The first object development system on Windows as far as I can tell. It was similar to using the Net Framework and Sapien PSS although without all of the Sapien conveniences.
This topic is 1 year and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked