I need to highlight specific text inside richtext box during runtime, based on a condition. I tried to replace the text with a text with html tags but it didn't render it.
How can I do that?
You can try something like this . Offcourse, change the code depending on your condition.
private void button1_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Contains("red"))
{
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = richTextBox1.Text.Length;
richTextBox1.SelectionBackColor = Color.Black;
richTextBox1.SelectionColor = Color.White;
}
}
Related
In my Winform 4.5 app, I have a DataGridView with first column as a link column. I would like to have the link color of the selected link cell to be white. Since by default the background color of a selected row (or a cell) is blue and the ForeColor of all the links are also blue, when user selects a row (or a link cell) the text of the link is not readable. I tried writing the following code but it does not change the link color of selected link cell at all.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewLinkCell cell in ((DataGridView)sender).SelectedCells)
{
if (cell.ColumnIndex == 0)
{
if (cell.Selected)
{
cell.Style = new DataGridViewCellStyle()
{
SelectionForeColor = SystemColors.HighlightText
};
}
}
}
}
I then modified the above code as follows. But it changes the link color of all the links to white - that makes non-selected link cells to be not readable since the backcolor of those links is also white:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewLinkCell cell in ((DataGridView)sender).SelectedCells)
{
if (cell.ColumnIndex == 0)
{
if (cell.Selected)
{
cell.LinkColor = SystemColors.HighlightText;
}
}
}
}
I tested both the codes by setting a breakpoint inside the foreach loop and selecting a link cell. I noticed that the code does go through exactly one iteration of the foreach loop correctly. Moreover, I have made no change to the default settings of the DataGridViewLinkColumn
Edit
By default the DataGridView looks like this on a row selection. Notice that the cell in the second column changes its ForeColor to white but not the cell in the first column:
I want it to looks like this on a row selection:
Edit The CellLeave event will always occur when an attempt is made to navigate away from a cell.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewLinkCell cell in
((DataGridView) sender).SelectedCells.OfType<DataGridViewLinkCell>())
{
if (cell.Selected)
{
cell.LinkColor = SystemColors.HighlightText;
}
}
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewLinkCell cell in
((DataGridView) sender).Rows[e.RowIndex].Cells.OfType<DataGridViewLinkCell>())
{
cell.LinkColor = cell.LinkVisited ? Color.Purple : Color.Blue;
}
}
I've experienced the same issue and I got it working using the CellFormatting event. Below you'll find a generic solution for this:
void grd_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
SetGridLinkColor(sender as DataGridView, e.RowIndex, e.ColumnIndex, Color.White);
}
public static void SetGridLinkColor(DataGridView grd, int rowIndex, int columnIndex, Color selectedColor)
{
if (grd == null || !(grd.Columns[columnIndex] is DataGridViewLinkColumn))
return;
if (grd.Rows[rowIndex].Selected)
{
((DataGridViewLinkCell)grd.Rows[rowIndex].Cells[columnIndex]).LinkColor = selectedColor;
((DataGridViewLinkColumn)grd.Columns[columnIndex]).VisitedLinkColor = selectedColor;
}
else
{
Color color = ((DataGridViewLinkColumn)grd.Columns[columnIndex]).LinkColor;
((DataGridViewLinkCell)grd.Rows[rowIndex].Cells[columnIndex]).LinkColor = color;
((DataGridViewLinkColumn)grd.Columns[columnIndex]).VisitedLinkColor = color;
}
}
How to make a custom date time picker control using mask edit textbox in C# WinForm. By default it should display the text 'MM/DD/YYYY' and after clicking on it, it should show the mask edit TextBox.
you can show a example by using below code snippet
in the designer.cs add following lines
this.maskedTextBox1.Location = new System.Drawing.Point(29, 251);
this.maskedTextBox1.Mask = "00/00/0000";
this.maskedTextBox1.Name = "maskedTextBox1";
this.maskedTextBox1.Size = new System.Drawing.Size(100, 20);
this.maskedTextBox1.TabIndex = 10;
this.maskedTextBox1.Text = "31071770";
this.maskedTextBox1.TextMaskFormat = System.Windows.Forms.MaskFormat.ExcludePromptAndLiterals;
this.maskedTextBox1.Enter += new System.EventHandler(this.maskedTextBox1_Enter);
this.maskedTextBox1.Leave += new System.EventHandler(this.maskedTextBox1_Leave);
and in the code.cs add these functions
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
if (maskedTextBox1.Text == "31071770")
{
maskedTextBox1.Text = "";
}
}
private void maskedTextBox1_Leave(object sender, EventArgs e)
{
if (maskedTextBox1.Text == "")
{
maskedTextBox1.Text = "31071770";
}
}
o/p
31/07/1970
Note : At last you have to validate that date
i have a field in my database for detect font syle of a row.
font syle is Regular where it is true.
I want to changing my row style when select it. i write this :
private void myGrid_SelectionChanged(object sender, EventArgs e)
{
DataBaseComponent.EditFieldofObject(object1.Serial, true);
if (myGrid.SelectedRows[0].VisualElement != null)
myGrid.SelectedRows[0].VisualElement.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(178)));
myGrid.SelectedRows[0].Cells["myField"].Value = true;
}
but it doesnot work and i must bind grid again to see this change.
Why not use ItemDataBound instead of SelectionChanged? This will work for your needs.
protected void myGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataBoundItem = (GridDataItem)e.Item;
if (dataBoundItem["ColumnName"].Text.ToString() == "True")
{
// Do something here
}
}
}
There is a good article on Telerik explaining it.
I have a WPF FlowDocument that has a few InlineUIContainers, these are simple InlineUIContainers, that contain a styled button with some text in the Button.Content. When I copy the text and InlineUIContainer containing the button from the FlowDocument to a TextBox, the button is not copied.
It is possible to somehow convert the inline button or convert the button to text in the pasted text data. I have tried using the FlowDocument.DataObject.Copying event, but I can't seem to find any good samples on how to use this or even if this is the right direction.
Thank you
I had the same problem and managed to get something like the following to work:
public class MyRichTextBox : RichTextBox
{
public MyRichTextBox()
: base()
{
CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
new CommandBinding(ApplicationCommands.Copy, OnCopy, OnCanExecuteCopy));
}
private static void OnCanExecuteCopy(object target, CanExecuteRoutedEventArgs args)
{
MyRichTextBox myRichTextBox = (MyRichTextBox)target;
args.CanExecute = myRichTextBox.IsEnabled && !myRichTextBox.Selection.IsEmpty;
}
private static void OnCopy(object sender, ExecutedRoutedEventArgs e)
{
MyRichTextBox myRichTextBox = (MyRichTextBox)sender;
Clipboard.SetText(GetInlineText(myRichTextBox));
e.Handled = true;
}
private static string GetInlineText(RichTextBox myRichTextBox)
{
StringBuilder sb = new StringBuilder();
foreach (Block b in myRichTextBox.Document.Blocks)
{
if (b is Paragraph)
{
foreach (Inline inline in ((Paragraph)b).Inlines)
{
if (inline is InlineUIContainer)
{
InlineUIContainer uiContainer = (InlineUIContainer)inline;
if (uiContainer.Child is Button)
sb.Append(((Button)uiContainer.Child).Content);
}
else if (inline is Run)
{
Run run = (Run)inline;
sb.Append(run.Text);
}
}
}
}
return sb.ToString();
}
}
Of course this is very simplistic - you would probably create a subclass of Button and define an interface-function like "GetCopyToClipboardText" instead of having the "how to get text from a button"-code inside the richtextbox.
The example copies all text inside the richtextbox - it would be more usefull if only the selected part of the textbox was copied to the clipboard. This post gives an example of how to achive that.
i want to have a tooltip for each item in a treeview, and each item in a listview, and different for each subitem (i.e. column) in the listview.
i can determine the text i want to show (using hit testing with the current mouse position, etc):
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (e.AssociatedControl == listView1)
{
toolTip1.SetToolTip(listView1, "foo");
}
}
but any attempt to set the tooltip text causes a stackoverflow.
How can i customize the tooltip (icon, title, text) just before it appears?
You need to guard your code in the Popup event handler so that if you are calling SetToolTip from within it, you don't call SetToolTip again.
Something like:
private bool updatingTooltip;
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (!this.updatingTooltip && (e.AssociatedControl == listView1))
{
this.updatingTooltip = true;
toolTip1.SetToolTip(listView1, "foo");
this.updatingTooltip = false;
}
}