I am making a program in c sharp/xaml. I have a save button, and figured the easiest way to make it effective was when pressed if I could send the "control s" signal. What is the command (and any includes VS wouldn't add as standard) to do so?
A completely different question to cut down on thread count, how would I make a textblock (or textbox if easier) automatically newline when you reach the end rather than continuing to send text offscreen.
There are better patterns that I would suggest looking into before coupling your UI to keypresses (such as Commands) but if you really want to do this, you can use the SendKeys class from windows forms. This will allow you to send key presses to the application as if the user pressed those keys.
As for the second question, if you're using WPF just create a textbox element and set AcceptsReturn="True" and TextWrapping="Wrap". Here's an example:
<TextBox
Name="tbMultiLine"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"
>
This TextBox will allow the user to enter multiple lines of text. When the RETURN key is pressed,
or when typed text reaches the edge of the text box, a new line is automatically inserted.
</TextBox>
You can use the SaveFileDialog() .
private void button1_Click(object sender, EventArgs e)
{
// When user clicks button, show the dialog.
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
// Get file name.
string name = saveFileDialog1.FileName;
// Write to the file name selected.
// ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test");
}
If you're using XAML I'll assume you're using WPF. So you should be able to bind the Command property of the Button to ApplicationCommands.Save.
Related
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;
}
I am trying to show the keyboard when I click a button, but it's not showing a keyboard at all.
The "TEST" gets printed but the keyboard isn't showing.
My code is :
private SurfaceTextBox mySurfaceTextBox = new SurfaceTextBox();
void showKeyBoard(object sender, RoutedEventArgs e)
{
//System.Windows.Input.Keyboard.Focus((IInputElement)getCanvasFromButton((SurfaceButton) sender));
System.Windows.Input.Keyboard.Focus((IInputElement)mySurfaceTextBox);
Console.Write("TEST");
SurfaceKeyboard.IsVisible = true;
SurfaceKeyboard.CenterX = (float)InteractiveSurface.PrimarySurfaceDevice.Bounds.Width - (SurfaceKeyboard.Width / 2);
SurfaceKeyboard.CenterY = (float)InteractiveSurface.PrimarySurfaceDevice.Bounds.Height - (SurfaceKeyboard.Height / 2);
SurfaceKeyboard.Layout = Microsoft.Surface.KeyboardLayout.Alphanumeric;
SurfaceKeyboard.Rotation = (float)(Math.PI / 2);
SurfaceKeyboard.ShowsFeedback = false;
}
Can someone help me please?
I don't know much about the surface framework; but usually you cannot force a keyboard to appear, the focused object needs to accept text as an input.
Because buttons generally don't accept text input, the keyboard's focus cannot be given to it, and thus
System.Windows.Input.Keyboard.Focus((IInputElement)sender);
will be ignored.
If the idea is to only make the keyboard appear, then an option is to add a SurfaceTextBox and to give focus to the textbox (this will inturn remove focus from the button)
XAML
Add this to your XAML file
<Canvas>
<s:SurfaceTextBox
Name="yourSurfaceTextBox"
Canvas.Top="200" Canvas.Left="200"
Width="100" Height="40" />
</Canvas>
Code File
void showKeyBoard(object sender, RoutedEventArgs e)
{
Console.Write("TEST");
System.Windows.Input.Keyboard.Focus((IInputElement)yourSurfaceTextBox);
// Rest of your code...
}
If the idea is to get navigation between buttons, you should consider using a SurfaceListBox since it accepts as a default behavior arrow navigation from the keyboard, then your code above should work.
Question in Comments
How I can test this on a non-surface device?
You can use a simulator which should be included in the 2.0 sdk
How I can change the cursor position in the SurfaceTextBox to the place it's touched?
I don't really understand what you mean by 'place it's touched', but you can change the cursor location in the textbox using the select method.
yourSurfaceTextBox.Select(position, 0);
To get the touch locations you can use
ReadOnlyTouchPointCollection touches = touchTarget.GetState();
Then you'll have to figure out where in relation to an object the touch was, but I this question is beyond the scope of the original question.
Have fun!
Is It possible that if I create two TextBoxes.
When the first TextBox is modified from input, the second text box is set to be read only and its value will update depending on what you had written in the first text box.
It's like when I am posting here in stackoverflow there is also a read only area that follows what I'm typing (The preview window). :)) Thanks!!!
If it's win-form application, it's so simple. try this :
private void txtFirstTextBox_TextChanged(object sender, EventArgs e) {
if (string.IsNullOrEmpty(txtFirstTextBox.Text)) {
txtSecondTextBox.Clear();
return;
}
txtSecondTextBox.Text = txtFirstTextBox.Text;
}
hope this help.
I should note: This is a solution if you're using WPF for your UI.
Yes that's easily possible if you have, for example the first textbox:
<TextBox x:Name="FirstBox"/>
You can bind to this text box's content via:
<TextBox x:Name"SecondBox" Text="{Binding ElementName="FirstBox", Path="Text", UpdateSourceTrigger=PropertyChanged}" IsEnabled="False"/>
And when the first text box changes, the second one should follow suit. This is all handled automatically for you via binding, it connects to the Text property on the TextBox named "FirstBox". This second TextBox is disabled by setting the IsEnabled property to "False"
Since there is already a WPF Solution and you didn't specify which you are using, I'll go ahead and post a WinForms solution.
Luckily, this is relatively simple in WinForms as well. You simply wire a TextChanged event handler for the first text box which updates the text of the second:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = ((TextBox)sender).Text;
}
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;
}
}
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;
}
}