How to extract font size of content - c#

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.

Related

Increase font size of ListItem in Combo box that comes as suggested in dropdown

I am using Combo box while creating Desktop Application using C# in Visual Studio, I increased the font size to "20" now when I run the application and click the dropdown button list elements font-size also increased.
That's fine. But, when I write something in the combo box it gives suggestions as shown in the picture below.
I, also want to increase the font size of this suggestion list, I have set "AutoCompleteMode" property set to "suggest". Is anybody can help me with this?
The font of the auto-complete text presented is fixed. There is no corresponding properties to set it.
A workaround is that you can create a custom control and replace the suggest-drop-down with custom listbox.
public Form1()
{
InitializeComponent();
comboBox1.Size = new Size(120, 30);
listBox.SelectedIndexChanged += ListBox_SelectedIndexChanged;
}
private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Text = listBox.Text;
listBox.Visible = false;
}
ListBox listBox = new ListBox();
// event triggered when the user enters text
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
// set the font of listbox to be consistent with combobox
listBox.Font = comboBox1.Font;
// add listbox below combobox
listBox.Location = new Point(comboBox1.Location.X, comboBox1.Location.Y + comboBox1.Height);
// filter suggest items
listBox.Items.Clear();
foreach (string item in comboBox1.Items)
{
if (item.StartsWith(comboBox1.Text) && comboBox1.Text != string.Empty)
{
listBox.Items.Add(item);
}
}
listBox.Visible = listBox.Items.Count > 0;
this.Controls.Add(listBox);
}
Test result:
Those are actually two areas which you can configure independently. According to this MSDN article,
To change settings:
Go to Tools – Options – Environment - Fonts and Colors
Under Show settings for: select either Statement Completion or Editor Tooltips (for Parameter Info and Quick Tips)
Change either the font or font size

RichTextBox selected Text property

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);
}

How to append the string at a cursor position in a rich text box C#?

I want to add string in rich text box at that position where the cursor is blinking. I also want to change the color of text that I entered. For example If there is pre-entered text which color is black, then I entered a new string then this string should have blue color. And it should be for all the time i entered the string. And the strings I entered, they should remain blue. thanks
The current cursor position is also know as the SelectionStart. Simply set the SelectionStart=0 and then set all SelectionXXX properties you want to:
richtTextBox1.SelectionLength=0;
richtTextBox1.SelectionColor = Color.Blue;
Now all you type will be blue.
No more and no less is needed. No need to subscribe to the TextChanged event.
Note that if you move the cursor position to a different spot that spot will have a color of its own and when you enter new text there it will have that color.
If you want to apply the new color (Blue) to other spots you need to code the SelectionChanged event maybe like this:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
// you should check if there is no text selected
// or else any selection will be colored immediately:
if ( richTextBox1.1.SelectionLength == 0)
richTextBox1.1.SelectionColor = yourNewColor;
}
And as usual, if you want to color a portion that is already there with a new color you need to select that portion and again set the color. Best use a color palette for this! Using a few colored Labels you might write:
private void colorLabel_Click(object sender, EventArgs e)
{
Color yourNewColor= ((sender) as Label).BackColor;
richTextBox1.1.SelectionColor = yourNewColor;
}
If you want to you can use the image of a richer palette in a Panel's BackgroundImage and code its MouseClick event:
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
Color c = ((Bitmap)panel1.BackgroundImage).GetPixel(e.X, e.Y);
if (e.Button.HasFlag(MouseButtons.Left))
{
richTextBox1.SelectionColor = c;
}
else // pick new BackColor:
{
richTextBox1.SelectionBackColor = c;
}
}
So you need to subscribe to the event where text is changing from the textbox, this will give you the text that has been entered up to the cursor.
Then it is a simple matter of just appending text to that as statements, where you can apply different formatting, as described here:
http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/

Selecting an item in a combo cause a cyclic call

I am writing a winform application.
On the form I have a Label and a combobox.
The combobox is populated with fonts names. When selecting a font from it, the label's font text is changing accordingly. When clicking on the label, the combobox's selecteditem is set accordingly.
The problem is when I am selecting the label, I get a cyclic call: I am setting the combobox item according to the font of the label, then the SelectedIndexChanged is fired which is in charge to set the font name of the label (ChangeLabelFont).
So the label font is again updated while it is the trigger of all those calls.
private void FontToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (_selectedLabel == null)
{
return;
}
ChangeLabelFont(FontNameToolStripComboBox.SelectedItem.ToString(), FontSizeToolStripComboBox.SelectedItem.ToString());
}
private void SelectLabel(Point location)
{
SetComboBoxesFont(_selectedLabel.Font.Name, _selectedLabel.Font.Size);
}
private void SetComboBoxesFont(string name, float size)
{
FontNameToolStripComboBox.SelectedItem = name;
FontSizeToolStripComboBox.SelectedItem = size;
}
private void ChangeLabelFont(string name, string size)
{
if (_selectedLabel == null)
{
return;
}
FontFamily fontFamily = new FontFamily(name);
float fontSize = float.Parse(size);
_selectedLabel.Font = new Font(fontFamily, fontSize);
}
Is there a way to prevent this cyclic call? Maybe I am doing something wrong?
There is something missing in the code you're showing. I'd like to see the ChangeLabelFont method as well as the handler for the "select label" event. But I suppose it's the SelectLabel method in the end. So how about using a member flag:
private bool m_bInhibitCycle = false;
private void SelectLabel(Point location)
{
if (m_bInhibitCycle) return;
m_bInhibitCycle = true;
SetComboBoxesFont(_selectedLabel.Font.Name, _selectedLabel.Font.Size);
m_bInhibitCycle = false;
}

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.

Categories