Changing the formatting of clicked text in a textblock - c#

I have a textBlock to which I've added some text e.g. as follows:
textBlock1.Inlines.Add(new Run("One "));
textBlock1.Inlines.Add(new Run("Two "));
textBlock1.Inlines.Add(new Run("Three "));
How can I add a click event that changes the color of the text of the inline that has been clicked?
e.g. if "One" is clicked I'd like it to have a red font; then if "Two" is clicked, I'd like "One" to be black again, and "Two" to be red, so that the color of the last word clicked is red.
I'm fairly new to programming with c# and wpf.
Thanks for your help

Something like this should do the trick
public MainWindow()
{
InitializeComponent();
textBlock1.Inlines.Add(new Run("One "));
textBlock1.Inlines.Add(new Run("Two "));
textBlock1.Inlines.Add(new Run("Three "));
}
private SolidColorBrush _blackBrush = new SolidColorBrush(Colors.Black);
private SolidColorBrush _redBrush = new SolidColorBrush(Colors.Red);
private Run _selectedRun;
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
var run = e.OriginalSource as Run;
if (run != null)
{
if (_selectedRun != null)
{
_selectedRun.Foreground = _blackBrush;
if (_selectedRun == run)
{
return;
}
}
run.Foreground = _redBrush;
_selectedRun = run;
}
}
But you will have to handle click with "MouseDown" or "MouseUp" as Textblock does not have Click event
To color one at a certain index this is a quick example.
private void ColorInlineAtIndex(InlineCollection inlines, int index, Brush brush)
{
if (index <= inlines.Count - 1)
{
inlines.ElementAt(index).Foreground = brush;
}
}
Usage:
ColorInlineAtIndex(textBlock1.Inlines, 2, new SolidColorBrush(Colors.Blue));
Find position:
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
var run = e.OriginalSource as Run;
if (run != null)
{
int position = (sender as TextBlock).Inlines.ToList().IndexOf(run);
}
}

Related

How to change color of text in windows form when you click a button?

When I click a button with a word e.g "cat" I want this word in richTextBox1 change to color red. Of course I made it wrong, but I would like to learn how to change it.
private void btn1(object sender, EventArgs e)
{
Button button = sender as Button;
string wordToColor = button.Text;
ChangeColorOfText(richTextBox1, wordToColor);
}
private void ChangeColorOfText(RichTextBox richTextBox1, string word)
{
ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Red = richTextBox1.SelectionColor;
int index;
do
{
index = richTextBox1.Find(word);
if (index >= 0)
{
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = ColorDialog.Red;
}
}
while (index >= 0);
}
I had few things that may be a solution for you:
It was a problem for me but it's not a must:
private void button1_Click(object sender, EventArgs e)
{
//Your code for finding and selecting your text
if (!String.IsNullOrEmpty(myRichTextBox.Text))
{
//Your code for selecting your text
myRichTextBox.SelectionStart = 0;
myRichTextBox.SelectionLength = myRichTextBox.Text.Length;
}
myRichTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
myRichTextBox.SelectionColor = Color.Red;
}
I think your problem was not setting the font before you set the color
(When I added the line myRichTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold); everything worked)
The Second thing I did in the code was to use
myRichTextBox.SelectionColor = Color.Red;
insted of
richTextBox1.SelectionColor = ColorDialog.Red;
it's easier and more efficient.
At the line
private void ChangeColorOfText(RichTextBox richTextBox1, string word)
you dont have to add the RichTextBox richTextBox1 element. (worked fine for me without it).
I deleted ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Red = richTextBox1.SelectionColor;
and changed richTextBox1.SelectionColor = ColorDialog.Red; torichTextBox1.SelectionColor = Color.Red;
I thought it will work but just when I write the same word in the text box that is in the button and click on the button the form freezes.

C# Windows Forms Application ListBox Controls shifted

I'm having problems with an owner drawn listbox in a windows forms application.
The listbox is filled with objects containing their own UserControl. The user control of each itemis shown in the listbox.
This all works but when I scroll up or down the UserControls appear shifted a bit.
Once I click them, they jump to the right position.
In the picture you can see the white UserControls shifted a bit to the right and a bit down.
This is how they look before scrolling.
The list is filled with objects of this type:
class Class1
{
public UserControl1 UC;
public string Text;
public Class1(UserControl1 uc, string text)
{
UC = uc;
Text = text;
}
}
This is the class that controls the list:
class ListDrawer
{
public ListBox LB;
public int HeaderHeight = 25;
public ListDrawer(ListBox lb)
{
LB = lb;
LB.DrawMode = DrawMode.OwnerDrawVariable;
LB.DrawItem += LB_DrawItem;
LB.MeasureItem += LB_MeasureItem;
}
private void LB_MeasureItem(object sender, MeasureItemEventArgs e)
{
ListBox lst = sender as ListBox;
Class1 c = (Class1)lst.Items[e.Index];
e.ItemHeight = HeaderHeight;
e.ItemHeight = e.ItemHeight + c.UC.Height;
}
private void LB_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox lst = sender as ListBox;
Class1 c = (Class1)lst.Items[e.Index];
e.DrawBackground();
e.Graphics.FillRectangle(Brushes.DarkSeaGreen, e.Bounds);
e.Graphics.DrawString(c.Text, LB.Font, SystemBrushes.HighlightText, e.Bounds.Left, e.Bounds.Top);
if (!lst.Controls.Contains(c.UC))
{
lst.Controls.Add(c.UC);
}
c.UC.Top = e.Bounds.Top + HeaderHeight;
}
}
The list is filled on a button click:
private void button1_Click(object sender, EventArgs e)
{
UserControl1 uc = new UserControl1();
Class1 c = new Class1(uc, "text 1");
ListDrawer LD = new ListDrawer(listBox1);
listBox1.Items.Add(c);
uc = new UserControl1();
c = new Class1(uc, "text 2");
listBox1.Items.Add(c);
}
Hope this can be fixed....
Cheers,
Robert.
In the user control override the onMove event:
protected override void OnMove( EventArgs e ) {
base.OnMove( e );
this.Parent.Invalidate();
}
It will probably flicker but will solve your problem

how to clear button color second click in flow layout panel c#

I have a lot of buttons in flow layout panel. I created these buttons programmatically.
But I didn't if click button its color will be light green after second click will be reset color. I wrote this handler for flow layout panel. How can I do second click will clear button color:
private void Form1_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append( "3 x coca cola 33 cl");
sb.Append(Environment.NewLine);
sb.Append("DIN");
sb.Append("\t\t");
sb.Append("200");
//button2.Text = sb.ToString();
string products = "30000|3;3110002;Cola;400;150;100;1;1000|3;3110003;Snickers;400;150;100;1;1000|3;3110004;NesttleCrunch;400;150;100;1;1000|3;3110005;Marlbora;400;150;100;1;1000|3;3110006;Orange;400;150;100;1;1000|3;3110007;Milk;400;150;100;1;1000|3;3110008;Water;400;150;100;1;1000|3;3110009;Banana;400;150;100;1;1000|3;3110010;Honey;400;150;100;1;1000|3;3110011;Beer;400;150;100;1;1000|3;3110012;Hazelnut;400;150;100;1;1000|3;3110013;RedBull;400;150;100;1;1000|3;3110014;ChewingGum;400;150;100;1;1000|3;3110015;Apple;400;150;100;1;1000";
string[] listproducts = products.Split('|');
lblAvaliablePoint.Text = listproducts[0];
for (int i = 1; i < listproducts.Count();i++ )
{
string[] perproduct = listproducts[i].Split(';');
Button newButton = new Button();
newButton.Size = new System.Drawing.Size(170, 87);
newButton.BackColor = Color.LightGray;
newButton.UseVisualStyleBackColor = false;
newButton.Tag =perproduct[1];
newButton.Text = perproduct[0] + "x" + perproduct[2];
newButton.Click += new EventHandler(ButtonClickHandler);
flowLayoutPanel1.Controls.Add(newButton);
}
flowLayoutPanel1.VerticalScroll.Maximum = flowLayoutPanel1.Height+40;
flowLayoutPanel1.VerticalScroll.LargeChange = 30;
}
public void ButtonClickHandler(Object sender,EventArgs e)
{
((Button)sender).BackColor = Color.LightGreen;
}
What about add simple if condition into button handler like this?
public void ButtonClickHandler(object sender, EventArgs e)
{
if (((Button)sender).BackColor == Color.LightGreen)
{
((Button)sender).BackColor = Color.White; // Your default color
}
else
{
((Button)sender).BackColor = Color.LightGreen;
}
}
Change your ButtonClickHandler
public void ButtonClickHandler(Object sender, EventArgs e)
{
var currentButton = ((Button)sender);
if(currentButton != null)
{
currentButton.BackColor = currentButton.BackColor == Color.LightGreen ? Color.LightGray : Color.LightGreen;
}
}

Custom Color Dialog in Windows Forms

Is there a way to have the color dialog display on the form without the window border? To make things a bit more clearer, I want it sort of like the one used in the Paint program so that I'm able to change colors of lines drawn on a picturebox without having to open up the dialog again and again:
I'm placing it inside a vertical groupbox, and I want it to be able to fit in it. Can it be done?
You can easily build it yourself. Use a TableLayoutPanel or FlowLayoutPanel and a add a list of small PictureBox with the background color set on the color you want. Then handle the Click event to return/use their background color.
private void Form1_Load(object sender, EventArgs e)
{
pictureBox3.Click += HandleColorPick;
pictureBox4.Click += HandleColorPick;
pictureBox5.Click += HandleColorPick;
pictureBox6.Click += HandleColorPick;
pictureBox7.Click += HandleColorPick;
pictureBox8.Click += HandleColorPick;
pictureBox9.Click += HandleColorPick;
}
private void HandleColorPick(object sender, EventArgs e)
{
var s =(PictureBox) sender;
MessageBox.Show(s.BackColor.ToString());
}
You'll have to create your own palette.
Here's a usercontrol i wrote with comments. I've added a OnSelectionChanged event that will be fired when user changes color.
You can either use the designer to specify colors, events and other properties or you can hard-code them.
public partial class UserControl1 : UserControl
{
// The event raised when a user changes color
[Category("Behavior")]
public event EventHandler OnSelectionChanged;
/*
* Properties
*
*/
private Color[] m_Colors = new Color[] {}; // Colors on the palette
private Color m_SelectedColor; // Stores selected color
private int m_MaxColorsPerLine = 14; // Max colors per line
public Color[] Colors
{
get { return m_Colors; }
set { m_Colors = value; }
}
[Browsable(false)]
public Color SelectedColor
{
get { return m_SelectedColor; }
}
public int MaxColorsPerLine
{
get { return m_MaxColorsPerLine; }
set { m_MaxColorsPerLine = value; }
}
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
this.Controls.Clear();
int COLOR_WIDTH = 16;
int COLOR_HEIGHT = 16;
// Border colors
Color NEUTRAL_COLOR = Color.Black;
Color HIGHLIGHTED_COLOR = Color.Orange;
Color SELECTED_COLOR = Color.Red;
// Populate the palette
for(int i = 0; i < m_Colors.Length; ++i)
{
// Get where the current color should be positioned.
int linePos = (int)Math.Floor((double) i / m_MaxColorsPerLine);
int colPos = i % m_MaxColorsPerLine;
int posX = COLOR_WIDTH * colPos;
int posY = COLOR_HEIGHT * linePos;
// Create the panel that will hold the color
Panel pnl = new Panel();
pnl.Width = COLOR_WIDTH;
pnl.Height = COLOR_HEIGHT;
pnl.Location = new Point(posX, posY);
pnl.BackColor = m_Colors[i];
pnl.ForeColor = NEUTRAL_COLOR;
// Change border color when highlighted
pnl.MouseEnter += (s, args) => pnl.ForeColor = HIGHLIGHTED_COLOR;
// Change border color when mouse leaves
pnl.MouseLeave += (s, args) =>
{
if (pnl.Tag != null && (bool)pnl.Tag == true)
{
pnl.ForeColor = SELECTED_COLOR; // restore selected border color on mouse leave if selected
}
else
{
pnl.ForeColor = NEUTRAL_COLOR; // restore normal border color on mouse leave if not selected
}
};
// Change border color when selected
pnl.Click += (s, args) =>
{
if (pnl.Tag == null || (bool)pnl.Tag == false)
{
pnl.ForeColor = SELECTED_COLOR;
pnl.Tag = true; // Use the Tag member to store whether the color is selected
m_SelectedColor = pnl.BackColor;
// Raise the SelectionChanged event if this panel is not already selected
if (OnSelectionChanged != null)
{
OnSelectionChanged(this, EventArgs.Empty);
}
}
// Unselect other colors
foreach (Panel otherColor in this.Controls)
{
if (otherColor == pnl)
continue;
if (pnl.Tag != null && (bool)pnl.Tag == true)
{
otherColor.ForeColor = NEUTRAL_COLOR;
otherColor.Tag = false;
}
}
};
// Draw borders
pnl.Paint += (s, args) =>
{
Rectangle outterRect = args.ClipRectangle;
Rectangle innerRect = new Rectangle(outterRect.X + 1, outterRect.Y + 1, outterRect.Width - 3, outterRect.Height - 3);
// Draw outter rectangle
args.Graphics.DrawRectangle(
new Pen(
new SolidBrush(pnl.ForeColor), 2),
outterRect);
// Draw inner rectangle
args.Graphics.DrawRectangle(
new Pen(
Brushes.White, 1),
innerRect);
};
// Finally, add color panel to the control
this.Controls.Add(pnl);
}
}
}
Why not some 3rd party opensource control?
http://www.codeproject.com/Tips/638824/Yet-Another-Color-Picker-for-Csharp-VB-NET-WinForm
or
http://www.blackbeltcoder.com/Articles/controls/colorpicker-controls-for-winforms

How to make image button

I was wondering how could I do this. I know I can use the button component but it has the little gray stuff around it when I give it a image. With image button how could I show another image for the hover effect
You want to create a button with no border but displays different images when the user hovers over it with the mouse? Here's how you can do it:
Add an ImageList control to your form at add two images, one for the button's normal appearance and one for when the mouse is hovering over.
Add your button and set the following properties:
FlatStyle = Flat
FlatAppearance.BorderColor (and maybe MouseOverBackColor & MouseDownBackColor) to your form's background color
ImageList = the ImageList you added to the form
ImageIndex to the index value of your normal image
Code the MouseHover and MouseLeave events for the button like this:
// ImageList index value for the hover image.
private void button1_MouseHover(object sender, EventArgs e) => button1.ImageIndex = 1;
// ImageList index value for the normal image.
private void button1_MouseLeave(object sender, EventArgs e) => button1.ImageIndex = 0;
I believe that will give you the visual effect you're looking for.
Small summary (Border, MouseDownBackColor, MouseOverBackColor)
FlatApperance
BorderColor = Black or what ever you want
BorderSize = can be set to 0
MouseDownBackColor = Transparent
MouseOverBackColor = Transparent
Text = none
For MouseDown:
// ImageList index value for the mouse down image.
private void button1_MouseDown(object sender, MouseEventArgs e) => button1.ImageIndex = 2;
You can assign the BackgroundImage property for the button. You can also use the OnMouseEnter and OnMouseExit events to change the background as per your request.
See BackgroundImage OnMouseEnter OnMouseLeave
I also needed an image button, but I wanted one like the ToolstripMenuButton.
With the correct borders and colors on hover.
So I made a custom control to do just that:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace LastenBoekInfrastructure.Controls.Controls
{
[DefaultEvent("Click")]
public class ImageButton : UserControl
{
public string ToolTipText
{
get { return _bButton.ToolTipText; }
set { _bButton.ToolTipText = value; }
}
public bool CheckOnClick
{
get { return _bButton.CheckOnClick; }
set { _bButton.CheckOnClick = value; }
}
public bool DoubleClickEnabled
{
get { return _bButton.DoubleClickEnabled; }
set { _bButton.DoubleClickEnabled = value; }
}
public System.Drawing.Image Image
{
get { return _bButton.Image; }
set { _bButton.Image = value; }
}
public new event EventHandler Click;
public new event EventHandler DoubleClick;
private ToolStrip _tsMain;
private ToolStripButton _bButton;
public ImageButton()
{
InitializeComponent();
}
private void InitializeComponent()
{
var resources = new ComponentResourceManager(typeof(ImageButton));
_tsMain = new ToolStrip();
_bButton = new ToolStripButton();
_tsMain.SuspendLayout();
SuspendLayout();
//
// tsMain
//
_tsMain.BackColor = System.Drawing.Color.Transparent;
_tsMain.CanOverflow = false;
_tsMain.Dock = DockStyle.Fill;
_tsMain.GripMargin = new Padding(0);
_tsMain.GripStyle = ToolStripGripStyle.Hidden;
_tsMain.Items.AddRange(new ToolStripItem[] {
_bButton});
_tsMain.Location = new System.Drawing.Point(0, 0);
_tsMain.Name = "_tsMain";
_tsMain.Size = new System.Drawing.Size(25, 25);
_tsMain.TabIndex = 0;
_tsMain.Renderer = new ImageButtonToolStripSystemRenderer();
//
// bButton
//
_bButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
_bButton.Image = ((System.Drawing.Image)(resources.GetObject("_bButton.Image")));
_bButton.ImageTransparentColor = System.Drawing.Color.Magenta;
_bButton.Name = "_bButton";
_bButton.Size = new System.Drawing.Size(23, 22);
_bButton.Click += bButton_Click;
_bButton.DoubleClick += bButton_DoubleClick;
//
// ImageButton
//
Controls.Add(_tsMain);
Name = "ImageButton";
Size = new System.Drawing.Size(25, 25);
_tsMain.ResumeLayout(false);
_tsMain.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
void bButton_Click(object sender, EventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
void bButton_DoubleClick(object sender, EventArgs e)
{
if(DoubleClick != null)
{
DoubleClick(this, e);
}
}
public class ImageButtonToolStripSystemRenderer : ToolStripSystemRenderer
{
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
//base.OnRenderToolStripBorder(e);
}
}
}
}

Categories

Resources