right click mouse to paste on a richtextbox vc++ windows forms - winforms

I am searching on how to implement a mouse down right click to paste data from a buffer into a richtextbox with visual c++ windows forms. My richtextbox is working but I can only paste data from buffer using shift+insert key.
Thank you so much in advance.

Solution:
Changed the RichTextBox to TextBox and use the code:
private: System::Void button1_Click(System::Object^ sender System::EventArgs^ e) {
String^ fileName = "output.txt";
StreamWriter^ sw = gcnew StreamWriter(fileName);
sw->WriteLine(textBox1->Text);
sw->Close();
}
The "paste" option is automatically available on the textbox (multi-line in my case). I think this is not possible with RichTextBox without heavy coding?

Do you mean a right click menu which can paste the clipboard's text to the RichTextBox?
1) Add a ContextMenuStrip (contextMenuStrip1)
2) Add a ToolStripMenuItem and name it paste (pasteToolStripMenuItem)
3) Set richTextBox1's ContextMenuStrip to contextMenuStrip1
4) Double-click the pasteToolStripMenuItem
private: System::Void pasteToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Paste();
}

Related

how to get index of current element in a parent element in WPF

I am developing an application in WPF and kinda new to WPF. I have been struggling to find the solution but failed. So posting here. Please guide me.
A stack panel has five text box. I want to know when ever user types in a text box, the index of the text box in the stack panel.
private void OnKeyDownHandler(object sender, KeyEventArgs e) {//required code??}
What I mean is, is he/she typing in the first textbox or second box or third text box or fourth text box or fifth text box? How can I get this in C# script?
Try this:
private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
{
TextBox textBox = (TextBox) sender;
StackPanel parent = (StackPanel)textBox.Parent;
int index = parent.Children.IndexOf(textBox);
}

Create button to move WPF window

I have a borderless WPF window that I am using this answer to move at the moment: Move a borderless window in wpf
But I would really like to have have a button that when you click and hold lets you drag the window around. Is that possible?
Thank you.
Just attach a PreviewMouseDown event to the Button and use similar code
XAML
<Button PreviewMouseDown="Move" />
or CodeBehind
Button button = new Button();
button.PreviewMouseDown += Move;
Code
private void Move(object sender, RoutedEventArgs e)
{
// "window" is the name of the window
window.DragMove();
e.Handled = true;
}

Which control to show readonly colorful text in WPF?

I want an element or a control to show readyonly, colorful, selectable, scrollable text which is a kind of log in my application. I don't know whether it is fixed document or flow document.
The RichText may be the seeming choice, but it originally supports editing. I believe even I set readonly=true, the build-in editing support takes some resources. I want to find a lighter-weight one.
Perhaps the FlowDocumentScrollViewer? It is readonly and do not show tool bar by default. Even I turn IsToolBarVisible on, the tool bar is just a small control.
The Block came into my mind. Although it may be the lightest control, I cannot select the text in it without other effort.
Maybe other choices exist? What's your opinions?
I made an experiment to help me choose my preferable control among FlowDocumentScrollViewer, RichTextBox, and TextBlock. I find FlowDocumentScrollViewer is the best.
In each window I have two controls of same type: FlowDocumentScrollViewer, RichTextBox, or TextBlock. And I made three such windows, as the MainWindow has three buttons.
private void prepareButton_Click(object sender, RoutedEventArgs e)
{
document1 = HelperClass.GetDocument();
document2 = HelperClass.GetDocument();
}
private void loadButton_Click_1(object sender, RoutedEventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
viewer1.Document = document1;
viewer2.Document = document2;
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(() =>
{
watch.Stop();
MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms",Title);
}));
}
Where viewer1 and viewer2 can be FlowDocumentScrollViewer or RichTextBox.
For TextBlock, I use
private void prepareButton_Click(object sender, RoutedEventArgs e)
{
inlines1 = HelperClass.GetInlines();
inlines2 = HelperClass.GetInlines();
}
private void loadButton_Click_1(object sender, RoutedEventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
viewer1.Inlines.AddRange(inlines1);
viewer2.Inlines.AddRange(inlines2);
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(() =>
{
watch.Stop();
MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms");
}));
}
The test indicates FlowDocumentScrollViewer has best performance among the three:
FlowDocumentScrollViewer RichTextBox TextBlock
Working set 65400 67252 82124
Loading Time 1045 1414 45119
I'm not sure what type of resources you think are being taken up by "editing" functionality. The ability to select text goes hand in hand with ability to edit text.
If you want one, you'll have to put up with the other. Luckilly, setting IsReadOnly to "True" will satisfy your functional requirements.
If your application machine is capable of running the .NET Framework with WPF, I wouldn't worry about tiny amounts of resources which may (or may not) be consumed by the ability to edit simple text.

tab multiple selected lines of wpf textbox c#

I am facing a problem with the text box control in WPF application.
The problem is that when the user selects multilines of the text and then clicks on tab, the selected lines are deleted instead of being indented to the right.
Is there a way to solve this issue?
Appreciate any help.
Thanks
Ahmad
You will need to handle it in the code behind as it is not the default action of a textbox. Many ways you can handle it. You will need to override the PreviewKeyDown and you can set the e.handled to true in order for text to not be overridden.
private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
TextBox tbx = sender as TextBox;
if (e.Key == Key.Tab)
{
tbx.Text = tbx.Text.Insert(tbx.SelectionStart, "\t" + tbx.Text.Substring(tbx.SelectionStart));
e.Handled = true;
}
}
Sadly have to say that you have to implement that functionality, as the tab key was not made for that in the TextBox.

How to auto cap a textbox

How do I make it so that when a user types letters into a textbox in silverlight that it changes these letters to all capitals? And at the same time still fires all the same events such as keypress.
If you want to avoid code behind or custom controls (preferable), this sort of functionality is available via behaviours.
e.g. this one in the Expression blend Gallery converts any text box to which it is attached into uppercase.
Then is just a matter of drag-drop in Blend or adding the trigger manually in VS2010.
There was no easy way to do it except to use an embedded all caps font. All other ways interfered with how the textbox worked.
The simplest way would be:
private void tb_TextChanged(object sender, TextChangedEventArgs e)
{
var tb = (TextBox)sender;
var caret = tb.CaretIndex;
tb.Text = tb.Text.ToUpper();
tb.CaretIndex = caret;
}
If you wanna save even more performance, handle the LostFocus event instead (which you then don't have to worry about carret position either):
private void tb_LostFocus(object sender, RoutedEventArgs e)
{
var tb = (TextBox)sender;
tb.Text = tb.Text.ToUpper();
}
But if the data is bound to a model/entity I would call the ToUpper() on the bound object property setter / OnPropertyChanged, OnTextChanged or whatever it is in the entity.

Resources