C# textbox deletion question - c#

I am working on a project that I use textbox as telnet terminal.
The terminal has "->" as the command prompt in the textbox.
Is there a way to disable the delete or backspace once it reach the "->" prompt?
I don't want to delete the command prompt.
Thanks

Dave is right.
The best way to do this is to make a label on the left side of the textbox that says ->.
You can remove the textbox's border and put them both in a white (or non-white) box to make it look real.
This will be much easier for you to develop and maintain, and will also be more user-friendly. (For example, the Home key will behave better)

Two options:
Make the prompt ("->") an image or label, instead of being part of the textbox.
If it's a web app, handle the textchanged event in javascript and cancel the textchanged if it represents a deletion of the prompt. If its not a web app, do the same thing in c# rather than JS.

You could always make sure that when deleting, the index of the character you're deleting is > 1 (since -> would occupy positions 0 & 1)

This is a naive example, but you should be able to figure it out from here. You can peak at the keydown event and cancel it, when desired.
private void testTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back && testTextBox.SelectionStart == 2)
{
e.SuppressKeyPress = true;
}
}

Related

How to PgUp/PgDn with SendKeys to a foreground textbox

I am trying to page up/down the contents of a simple textbox control in a simple Windows Form on NET 6.0, but something is wrong. Textbox shortcuts are enabled (probably why SendKeys.SendWait("^a"); works) and readonly is false.
I have a method (not on the UI thread) that I call to SendKeys.SendWait("{PgUp}"); to the foreground app (which is both the key sender and textbox (with focus) receiver.
If I type PgUp on the keyboard, the textbox pages up as expected.
If I SendKeys.SendWait("^a");, the textbox selects all text as expected.
If I Sendkeys.SendWait("{PgUp}");, the textbox adds a blank line to the bottom of the text.
From this I conclude that my code is working because it sends "^a" and the textbox receives it and selects all text. But somehow the textbox does not handle the "{PgUp}" key, even though it does when the PgUp key is sent by the keyboard.
I've read easily a dozen articles and posts on the web and SO that talk about paging using scrolling events, positioning the caret and then scrolling to the caret, and so on. But none of them say anything about why SendKeys(^a) and keyboard PgUp would work but SendKeys.SendWait("{PgUp}") would fail.
Can anyone tell me what I'm doing wrong and maybe what I need to do (or read) to fix it? Thank you
UPDATE: Jimi asked for some code, so here is the code that I use to send the ^a and the {PgUp} keys. I know this is not on the UI thread because it is executed from a voice-driven recognizer thread. The app is a voice-driven app that displays content in the textbox by textbox.AppendLines calls. I was trying to PgUp and PgDn the multi-line textbox by voice as well.
When I tried to use Send (I normally use .SendWait for everything in other programs), I received the following error message:
System.InvalidOperationException: 'SendKeys cannot run inside this
application because the application is not handling Windows messages.
Either change the application to handle messages, or use the
SendKeys.SendWait method.'
It is true that my app does not intercept Windows messages. I can't figure out why the app can receive and properly process my keyboard keys, and my "^a' shortcut keys, but not the SendWait("{PgUp}") key.
internal static void
HelperPageUp() {
var keys = "{PgUp}";
keys = "^a";
SendKeys.SendWait(keys);
}
I'm starting to think that {PgUp} is never handled by a textbox or control. Instead, probably {PgUp} must be handled by logic in a case statement that converts PgUp "orders" into sets of actions that implement whatever PgUp means to the app that receives the PgUp key. So maybe I will have to add a keystroke handler to the form. Maybe something like this:
textBox1_KeyUp(object sender, KeyEventArgs e) {
// identify the special key and implement what it means
if (e.KeyCode == Keys.PageDown) {
...
e.Handled = true;
}
Yes, my thought at the end of the question was correct. The ^a was handled by the textbox because I had textbox.EnableShortcuts=true;, so the textbox handled the popular ^a shortcut. But keys like {PgUp} are a different matter; they are not included in shortcuts.
The solution was to write code to handle the {PgUp} key explicitly in the form. Here is my code that worked.
void
textBox1_KeyUp(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.PageUp) {
// page the viewport up; watch for end of content
var charIndex = textBox1.SelectionStart;
var lineIndex = textBox1.GetLineFromCharIndex(charIndex);
// move 20 lines up, but not past zero
var newLine = lineIndex - 20;
var newIndex = Math.Max(0, newLine);
// set the new anchor and scroll to it
var newAnchor = textBox1.GetFirstCharIndexFromLine(newIndex);
textBox1.Select(newAnchor,0);
textBox1.ScrollToCaret();
e.Handled = true;
}

Any Characters - Masked Text Box [duplicate]

This question already has answers here:
Masked TextBox Input Align Left
(2 answers)
Closed 9 years ago.
I'm writing an app in C# with Visual Studio 2012, and I'm needing to format some input text using MaskedTextBox. The user will type in a folder path to the text box, but since the folder path is relative to another path, I need it to start with ".\", but I do not care how long the path is.
Right now, I have the mask set for the box to \.\\CCCCCCCCCCCCCCCCCC. This works fine except for the fact that when the user clicks into the box, it places the cursor where they click instead of the beginning of the box.
Is there a way to set the mask to still put in the ".\" but not to set any limit on the characters that come after it?
Or is there a way I'm overlooking?
EDIT: More info
So I've tried a couple recommended things, but they don't seem to work. The answer linked here doesn't work well. While I can set it to go to that selection point when I click on the box, it will go there every time you need to click on the box. So you can't select the whole box or edit part of what you typed, which is even worse for usability.
I also tried the method suggested by Adelmo. I made an even handler like so:
public Form1()
{
InitializeComponent();
refreshList();
this.textBoxPrintFolder.GotFocus += new EventHandler(textBoxPrintFolder_GotFocus);
}
private void textBoxPrintFolder_GotFocus(object sender, EventArgs e)
{
this.textBoxPrintFolder.Select(2, 0);
}
This works when tabbing to the box, but apparently clicking on the box doesn't go into the GotFocus event.
I've also tried using the MouseEnter event. While it does work, it takes a few seconds before it will move. Not ideal.
Any help would be greatly appreciated.
Maybe using onFocus event:
You can control cursor position (and selection) by TextBox.SelectionStart and TextBox.SelectionLength properties.
Example if you want move cursor to before 3th character set SelectionStart = 2 and SelectionLength = 0.
http://social.msdn.microsoft.com/Forums/en-US/04362a62-8cbf-4d86-a1bc-2aba8e4978ca/cursor-position-in-textbox
Hope it help you

How can I prevent system clipboard image data being pasted into a WPF RichTextBox

I have some code in place currently to intercept all Cut, Copy and Paste events into a RichTextBox in WPF. These are designed to strip all content out except plain text, and allow no pasting except plain text (by using a check the Clipboard.ContainsText() method.) This seems to be successful at preventing all such operations from inside the forms. A user can only copy, cut and paste text around, with images / audio data / random junk not being allowed.
However, if I use the PrintScreen function, and paste it into one of the RichTextBoxes, the image is pasted in (not the wanted behaviour.) If you then try and paste this image from one RichTextBox to another though, it won't let you (the desired behaviour).
The commands I'm currently overriding are done using
// Command handlers for Cut, Copy and Paste commands.
// To enforce that data can be copied or pasted from the clipboard in text format only.
CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
new CommandBinding(ApplicationCommands.Copy, new ExecutedRoutedEventHandler(OnCopy),
new CanExecuteRoutedEventHandler(OnCanExecuteCopy)));
CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
new CommandBinding(ApplicationCommands.Paste, new ExecutedRoutedEventHandler(OnPaste),
new CanExecuteRoutedEventHandler(OnCanExecutePaste)));
CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
new CommandBinding(ApplicationCommands.Cut, new ExecutedRoutedEventHandler(OnCut),
new CanExecuteRoutedEventHandler(OnCanExecuteCut)));
The OnCopy (etc) methods then essentially check that only text is present before allowing any operations.
There seems to be two Clipboards at work here, one of which I'm not restricting / locking down. Does anyone know of the technicalities of this, and any way in which all Clipboard activity (both Form and System) can be locked down and customized effectively?
Thanks in advance.
It might be a bit unforgiving for the user but you could do it as simple as hijacking and clearing the Clipboard before pasting. Just hook the PreviewKeyDown (since on KeyUp it´s already been inserted) and clear the clipboard if we´ve got an image and is pressing Ctrl+V:
public Window1()
{
InitializeComponent();
_rtf.PreviewKeyDown += OnClearClipboard;
}
private void OnClearClipboard(object sender, KeyEventArgs keyEventArgs)
{
if (Clipboard.ContainsImage() && keyEventArgs.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) != 0)
Clipboard.Clear();
}
Not the neatest solution but it´ll do the trick.
Actually you dont need any hack like catching the KeyDown events (which wouldn't prevent pasting through the context menu or drag and drop anyway). There's a specific attached event for that: DataObject.Pasting.
XAML:
<RichTextBox DataObject.Pasting="RichTextBox1_Pasting" ... />
Code-behind:
private void RichTextBox1_Pasting(object sender, DataObjectPastingEventArgs e)
{
if (e.FormatToApply == "Bitmap")
{
e.CancelCommand();
}
}
It prevents all forms of pasting (Ctrl-V, right-click -> Paste, drag and drop).
If you want to be smarter than that, you can also replace the DataObject with one that contains only the formats you want to support (rather than completely cancelling the paste).
I think this is probably a better way if your goal is to just allow the plain text to be pasted:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
{
if (Clipboard.GetData("Text") != null)
Clipboard.SetText((string)Clipboard.GetData("Text"), TextDataFormat.Text);
else
e.Handled = true;
}
}

how to identity a click out of the form and 1 more question

i want to build a simple program which help you selecting pictures.
if you have lot of picture and you want to choose some of them then you see them 1 by 1 and when you see a picture you would lik to save on other folder on your pcyou just press a button ,lets say f5 and the program copy the phtot from the path you looking at to the destiny folder.
for that program i need to ask how to know if someone pressed f5 out of the form area and how to know in which path the user looking at.(i want to build it for myself atm so if its help i look with microsoft office picture manager)
about the clicking i search a little and get something named global clicking and hooks which i dont understand so much and about identify the path i have no idea .
tyvm for help:)
I'm not sure I follow the rest -- but if you want to capture the keypress event, simply add an event handler for KeyPress and determine if the pressed key is equal to the F5 button by using the Keys constants.
Here is a project on Code Project that does exactly what you need :)
http://www.codeproject.com/KB/cs/globalhook.aspx
The key press event wont work with the following (Link):
TAB
INSERT
DELETE
HOME
END
PAGE UP
PAGE DOWN
F1-F2
ALT
Arrow keys
Note: I think there is a typo on the page and the F1-F2 really should be F1-F12.
When you decide the key for your on key press event for the form area you are talking about it will look like this:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == [keypressvalue])
{
//do your copy logic
}
}
[keypressvalue] will be the code for F5 if you choose to use this. I have found a mix of values for this (i could not get my test keypress event to pick up the F5 event, hence my note above) so i recommend running the event once with a brake point, inspecting the code, then brake and update your code, then test your logic.
Like the rest i'm not really sure what you want your custom logic to do.
Clairifcation: I'm trying to understand your question, so what you want is: When in Microsoft Picture Manager when you press F5 you want the image that is currently being viewed to be moved to a particular directory? Now if you are writing your own picture viewer and move software then i think we can help if it is the above i'm not really sure you can do that.

How to detect previous state of text field in C#?

I'm coding a simple text editor using Windows Forms. As in many editors, when the text changes the title bar displays an asterisk next to the title, showing that there is unsaved work. When the user saves, this goes away.
However, there is a problem. This is handled in the change event of the main text box. But this gets called too when a file is opened or the user selects "New file", so that if you open the editor and then open a file, the program says that there are unsaved changes. What is a possible solution?
I thought of having a global variable that says whether the text changed in a way that shouldn't trigger the asterisk, but there has to be a better way.
before loading data to a textbox, unassociate first the eventhandler for change
uxName.TextChanged -= uxName_TextChanged;
uxName.Text = File.ReadAllText("something.txt");
uxName.TextChanged += uxName_TextChanged;
This is a horrible solution, but every time the text change event fires, compare the value of the textbox to some variable, and if they are different store the contents on the textbox in a variable and add the asterisk. When the method is invoked via the New File dialog or any other such event that is NOT changing the text, the asterisk won't appear.
This is not a viable solution for a real text editor since the memory would quickly get out of hand on even medium-sized files. Using a finger tree or whatever data structure text editors use to compare "versions" of the text is the only real efficient solution, but the premise is the same.
http://scienceblogs.com/goodmath/2009/05/finally_finger_trees.php
Below the second picture he mentions the use of finger trees in text editors to implement an extremely cheap "undo" feature, but I'm sure you can see the validity of the tree for your problem as well.
There are no global variables in C#. You should have such an variable as an instance variable in your form (or better yet, in a model for which your form is a view), and that is perfectly fine.
This is a very simple and stupid solution. I would use a MVP design pattern for this but here the fastest and simple solution:
//Declare a flag to block the processing of your event
private bool isEventBlocked = false;
private void OnTextChanged(object sender, EventArgs e)
{
if(!isEventBlocked)
{
//do your stuff
}
}
private void OnNewFile() //OR OnOpenFile()
{
try
{
isEventBlocked = true;
CreateFile();
}
catch
{
//manage exception
}
finally
{
isEventBlocked = false;
}
}

Categories