RichTextBox selected Text property - c#

I've a RTB in my form to bold half of its text. Code is
int start = richTextBox.Text.ToUpper().IndexOf(text.ToUpper());
richTextBox.Select(start, text.Length);
richTextBox.SelectionFont = new Font(richTextBox.Font.Name, richTextBox.Font.Size, richTextBox.Font.Style ^ FontStyle.Bold);
and I am using MenuItem class to add custom menu's in my form. One of the menu is Copy Selected will be having event handler which triggers when user select that menu to clipboard for pasting selected text.
Issue is when user doesn't select any text and tries to paste, it is pasting the selected text which i made for bold(above in RTB) which is a bug. code in my handler
void noteCopySelectedMenu_Click(object sender, EventArgs e)
{
if (sender != null)
{
MenuItem noteCopyMenu = (MenuItem)sender;
ContextMenu noteContextMenu = (ContextMenu)noteCopyMenu.Parent;
RichTextBox text = (RichTextBox)noteContextMenu.SourceControl;
if (text != null)
{
// Copy note to Clipboard
Clipboard.Clear();
Clipboard.SetDataObject(text.SelectedText, false);
}
}
}
text.SelectedText is holding the text of bold from RTB
When user simply select menu without selecting any text, it should be empty. How to overcome this??

You can modify your code like this and put additional checks:
if (text != null && text.SelectionLength > 0)
{
// Copy note to Clipboard
Clipboard.Clear();
Clipboard.SetDataObject(text.SelectedText, false);
}

Related

How do I add the tag value and the text value of multiple TextBox, only when the text has changed in WPF?

I am creating a dictionary with = Tag value, and Text of multiple TextBox in a WPF application when the "submit button" is clicked. I only want to add to the dictionary the Tag and text of the Textboxes where the text has changed.
How do I add the tag value and the text value of multiple TextBox, only when the text has changed?
The code below is what I have so far, but I am stuck:
private void Submit_Button_Click(object sender, RoutedEventArgs e)
{
Dictionary<string, string> tagDict = new Dictionary<string, string>();
foreach(Control ctrl in MainGrid.Children)
{
if (ctrl.GetType() == typeof(textBox))
{
TextBox tb = ctrl as TextBox;
if (//I am trying to get a value that represents that the Text has changed here
)
{
string tagString = tb.Tag.ToString();
string textString = tb.Text;
tagDict.Add(tagString, textString);
}
}
}
}
I think a good solution is, first, to put your dictionary somewhere else (a private in the form, maybe)., and populate it when showing the form. This will allow you to update the dictionary when the text box is changed.
Assuming this, I would create a single method to be called every time the user "leaves" the edit box, and check if the text changed, on which case I would update the dictionary.
This way, the "submit" just have to use the dictionary that will be updated already.
ex:
private Dictionary<string, string> fDic; //This must be instantiated in the initialization
//Add this method to every Leave event in all text box controls.
private void textBox_Leave(object sender, EventArgs e)
{
TextBox text = sender as TextBox;
//Check if the text changed
if (text != null)
{
if (fDic.ContainsKey((string)text.Tag))
{
if (fDic[(string)text.Tag] != text.Text)
fDic[(string)text.Tag] = text.Text;
}
else if(text.Text != null)
{
fDic[(string)text.Tag] = text.Text;
}
}
}
The former code will update the dictionary if the TAG is already there AND the text associated with the tag is DIFFERENT from the text in the text box, and if THERE IS a text in the text box and the tag isn't in the dictionary already.
I'm assuming that the "Tag" is stored in the Tag property of each text box.
Also, it will store strings formed of only spaces. If you do not want this, the code can be changed to:
private Dictionary<string, string> fDic; //This must be instantiated in the initialization
//Add this method to every Leave event in all text box controls.
private void textBox_Leave(object sender, EventArgs e)
{
TextBox text = sender as TextBox;
//Check if the text changed
if (text != null)
{
if (fDic.ContainsKey((string)text.Tag))
{
if (fDic[(string)text.Tag] != text.Text && !string.IsNullOrWhiteSpace(text.Text))
fDic[(string)text.Tag] = text.Text;
}
else if(!string.IsNullOrWhiteSpace(text.Text) != null)
{
fDic[(string)text.Tag] = text.Text;
}
else if (string.IsNullOrWhiteSpace(text.Text)
fDic.Remove((string)text.Tag);
}
}
In the code above, it will also remove from the dictionary if the text is empty.
EDIT
After some chat, we came with a better option to accomplish what you want to do.
First, make the dictionary a Form variable, like in the option above.
private Dictionary<string, string> fDic;
Then, when you populate the TextBox's, put one entry (with the TAG) for each text box in the fDic.
To do this, you can use this kind of code:
foreach(Control ctrl in MainGrid.Children)
{
TextBox tb = ctrl as TextBox;
if (tb != null)
fDic[(string)text.Tag] = text.Text;
}
When the buttom click event is called, just compare the text in the text boxes with the one's previously stored in the dic, using the dictionary to do this.
private void Submit_Button_Click(object sender, RoutedEventArgs e)
{
Dictionary<string, string> tagDict = new Dictionary<string, string>();
foreach(Control ctrl in MainGrid.Children)
{
TextBox tb = ctrl as TextBox;
if (tb != null)
{
if (fDic[(string)text.Tag] != text.Text)
{
string tagString = tb.Tag.ToString();
string textString = tb.Text;
tagDict.Add(tagString, textString);
}
}
}
}

How to make textbox text disappear when I click on it

I managed to create textboxes dynamically in C#. When a textbox has text, can I make it disappear when I click on it?
I need to put a word inside textboxes that is the result of a select in Oracle.
Assign a value to the Text property of the TextBox. You could subscribe then to the GotFocus event, and set the text value to an empty string.
// Holds a value determining if this is the first time the box has been clicked
// So that the text value is not always wiped out.
bool hasBeenClicked = false;
private void TextBox_Focus(object sender, RoutedEventArgs e)
{
if (!hasBeenClicked)
{
TextBox box = sender as TextBox;
box.Text = String.Empty;
hasBeenClicked = true;
}
}
javascript is good for the delete.
onclick="$(this).val('');"
alternatively you can use HTML5 placeholder

Tooltip in wpf listview

I have created a WPF application in which i have a listview control. ListView will get populated when user click on browse button and select the files from the browse window. Listview will display only the selected file names. At the same time, the entire path of selected file will be added in an hashtable.
Requirement is when user move the mouse over the text block of listview , the exact path of the file must be displayed in tool tip. I have written the code as below in mouse move event of listview.
private void _listFiles_MouseMove(object sender, MouseEventArgs e)
{
_listFiles.ToolTip = null;
string _text = null;
var item = Mouse.DirectlyOver;
if (item != null && item is TextBlock)
{
if (_listFiles.Items.Count != 0)
{
_text = _arraylist[(item as TextBlock).Text].ToString();
_listFiles.ToolTip = _arraylist[(item as TextBlock).Text];
}
else
_listFiles.ToolTip = "";
}
}
which displays the tool tip. But in some cases its not displaying like when mouse cursor moves out of listview and then again place in listview its not displaying the tool tip.
Is my approach is correct or is there any other way to achieve this.
Regards
Sangeetha

Change Telerik GricViewComboBox Append List Font

i have a Telerik GridView with a ComboBox Column, while filtering in this Combobox an appending list dropped down.
Same as the image below...
So i want to make the font of the Append list larger.
How to do it?
RadDropDownList uses different popups for its default items representation and for its auto complete suggest items. Here is an example demonstrating how to change the font of both:
void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
if (editor != null)
{
editor.DropDownStyle = RadDropDownStyle.DropDown;
RadDropDownListEditorElement element = (RadDropDownListEditorElement)editor.EditorElement;
element.VisualItemFormatting -= element_VisualItemFormatting;
element.AutoCompleteSuggest.DropDownList.VisualItemFormatting -= element_VisualItemFormatting;
//this handles the default drop down formatting - when you press the arrow key to open the drop down
element.VisualItemFormatting += element_VisualItemFormatting;
//this handles the suggest popup formatting
element.AutoCompleteSuggest.DropDownList.VisualItemFormatting += element_VisualItemFormatting;
}
}
void element_VisualItemFormatting(object sender, VisualItemFormattingEventArgs args)
{
args.VisualItem.Font = new Font("Arial", 16);
}
You could either make a CellTemplate with your font, or you could handle the CellFormating event and do something like this:
void radGridView_CellFormatting(object sender, CellFormattingEventArgs e)
{
// For all cells under the Account Name column
if(e.CellElement.ColumnInfo.Name == "Account Name")
{
if(e.CellElement.Value != null)
{
System.Drawing.Font newfontsize = new System.Drawing.Font(e.CellElement.Font.FontFamily.Name,20);
for each(GridViewCellInfo cell in e.Row.Cells)
{
e.CellElement.Font = newfontsize;
}
}
}
// For all other cells under other columns
else
{
e.CellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.Font, Telerik.WinControls.ValueResetFlags.Local);
}
}
Plug in whatever size font you want for the "newfontsize" variable. Also note that, the else statement may not be necessary in your case, but you can reset the font to default using the ResetValue property.

How to extract font size of content

I've been working on making my own little text editor using a RichTextBox(MyRTB). I've made a Combobox to change the font of the selected text within the RichTextBox when the value changes using this code block:
private void CmbFont_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MyRTB != null)
{
string fontsize = (((ComboBoxItem)CmbFont.SelectedItem).Content).ToString();
MyRTB.Selection.ApplyPropertyValue(Run.FontSizeProperty, fontsize);
}
}
Now I'd like my Combobox value to change every time I select a string of text within the RichTextBox that has a different font size. Is this possible?
Thanks
Add an event handler to the selection changed event. In that event handler get the TextElement.FontSizeProperty from the RichTextBox selection
...
MyRTB.SelectionChanged += OnSelectionChanged;
...
void OnSelectionChanged()
{
var fontSize = MyRTB.Selection.GetPropertyValue(TextElement.FontSizeProperty);
if (fontSize == DependencyProperty.UnsetValue)
{
// Selection has text with different font sizes.
}
else {
// (double)fontSize is the current font size. Update Cmb_Font..
}
}
Make sure you don't call OnSelectionChanged & CmbFont_SelectionChanged recursively.

Categories