I would like to make a program where you can type in a richTextBox and then change the color.
So I tried this.
private void redFontColor_Click_1(object sender, EventArgs e)
{
richTxtBox.FrontColor = Color.Red;
}
But when I click the redFontColor button all the text in the richTextbox changes to red. So I tried to change the the color of each character individually.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Color currentColor = Color.Black;
private void redFontColor_Click_1(object sender, EventArgs e)
{
currentColor = Color.Red;
}
private void richTxtBox_TextChanged(object sender, EventArgs e)
{
if (textBox.SelectionStart != 0)
{
richTextBox.Select(textBox.SelectionStart - 1, 1);
richTextBox.SelectionColor = currentColor;
}
}
}
But when I now type something the color will change but when you enter a character it will be selected like this:
How could I make this work so the char isn't selected each time I type something? Or do I have to search for another method to do this?
Basically richTxtBox.FrontColor = Color.Red; will change the color of all text to Red, but you requirement is to change only the color of selected text, for that you need to use, richTextBox1.SelectionColor = Color.Red; which will change the color of only selected text.
You can decide which portion of the text need to be selected by using SelectionStart and SelectionLength. it is purely working on the basics of the index.
The mentioned problem of selecting the typed text in richTextBox1_TextChanged can be avoid by using this snippet
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionStart != 0)
{
int length = richTextBox1.Text.Length;
richTextBox1.Select(richTextBox1.SelectionStart - 1, 1);
richTextBox1.SelectionColor = Color.Red ;
richTextBox1.SelectionStart = length;
}
}
Apart from other answers here i have a magic box, which will changes the colors based on text.
And this can be achieved by using the following code:
private void button1_Click(object sender, EventArgs e)
{
int selectionStart = 0;
foreach (var item in richTextBox1.Text.Split(' '))
{
Color rgb = Color.FromName(item);
richTextBox1.SelectionStart = selectionStart;
richTextBox1.SelectionLength = item.Length;
richTextBox1.SelectionColor = rgb;
selectionStart += item.Length + 1;
}
}
As for your code: You are selecting a length of 1, i.e. the last character and you now see it highlighted. This is all not necessary. If you want to set colors or other formatting to text you enter simply do this:
Set the SelectionStart to the point he text gets entered, for example the end of the Text
Set the SelectionLength to 0
Set the formatting you want.
It will carry over to all text that you enter; make sure no line break (which in a RTB really means a new paragraph) slips in, as it will break the automatic carrying over. This all works pretty much like it does in Word..
Btw: Do not call a RichTextBox a Textbox; yes, they are related but still..
Also: We need to understand the difference between highlighting and selecting!
Selections are meant to format text portions.
Highlighting happens along the way and looks like it is set to look in the system settings.
Here is an example with all you need to get started:
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
if ( richTextBox1.SelectionLength == 0)
richTextBox1.SelectionStart = richTextBox1.Text.Length;
Color c = ((Bitmap)panel1.BackgroundImage).GetPixel(e.X, e.Y);
if (e.Button.HasFlag(MouseButtons.Left))
{
richTextBox1.SelectionColor = c;
}
else
{
richTextBox1.SelectionBackColor = c;
}
}
Pick a nice color palette and display in in a Panel.BackgroundImage; the code the MouseClick event. Done.
You can keep typing and when you left-click a color the text color will start to appear in the color. When you right-click it the backcolor will change. When there is a selection already it will change its color.
For a full formatting rtb you will want to add other things like font style size etc..
Note: No need to code the TextChanged event for this.
Related
I have three columns in a DataGridView that are mutually dependent on each other. Lets label them column 1, 2, and 3 respectively. In this case column 1 must be less than column 2 and column 2 must be less than column three.
If I call the SetValidBandByRow() function below while in the sysIdentGV_CellValueChanged the cell colors are the expected Light Pink to indicate that there is an error in one of the three cells and needs to be addressed. However when SetValidBandByRow(...) is called from a KeyPress event it registers that a new value violates the condition but the cell that is being edited does not retain the cell colors set in SetValidBandByRow(...). In an attempt to fix this I have caught that the KeyPress by setting the textbox backcolor to Light pink however I get a white boarder see the "while editing" image below.
private void sysIdentGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
/*some other validation and building of the rltu object here */
if(!SetValidBandByRow(rltu))
{
/*building error msg and displaying error*/
}
return;
}
private bool SetValidBandByRow(Tuple<int,double,double,double> rowLowerTransUpper)
{
bool wasValid = true;
if (rowLowerTransUpper.Item1 < 0 || rowLowerTransUpper.Item1 >= GV.RowCount)
return wasValid;
Color color = Color.White;
if (!(rowLowerTransUpper.Item2 <= rowLowerTransUpper.Item3 && rowLowerTransUpper.Item3 <= rowLowerTransUpper.Item4))
{
color = Color.LightPink;
wasValid = false;
}
GV.Rows[row].Cells[(int)GVEnum.lowerCutoff].Style.BackColor = color;
GV.Rows[row].Cells[(int)GVEnum.TransFreq].Style.BackColor = color;
GV.Rows[row].Cells[(int)GVEnum.upperCutoff].Style.BackColor = color;
return wasValid;
}
Setting tb.backcolor on GV_Keypress (Cell is still in focus):
Not setting tb.backcolor on GV_Keypress (Cell is still in focus):
after setting GV.Rows[row].Cells[(int)GVEnum.lowerCutoff].Style.BackColor
and SelectionBackColor on GV_CellValueChanged:
I am hoping to either remove the white space around the "while editing" image or find another approach that seems like less of a workaround.
This is some things that I have tried to set the textbox back color but nothing seems to be working as I would expect:
private void GV_KeyPress(object sender, KeyPressEventArgs e)
{
/*some other validation and building of the rltu object here */
//the result of the section below can be seen in image 1 "Setting
//active textbox back color" with out this section I get image two.
TextBox tb = (TextBox)GV.EditingControl;
if (!SetValidBandByRow(rltu))
{
tb.Margin = new Padding(0);
GV.CurrentCell.Style.BackColor = Color.LightPink;
GV.CurrentCell.Style.SelectionBackColor = Color.LightPink;
tb.BackColor = Color.LightPink;
}
else
tb.BackColor = Color.White;
}
Thanks
If anyone else is looking to do something similar. The question was answered in another stack over flow question that can be found here
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/
I'm struggling since 2 days to with this problem.
I have an XML file which contains Colors as htmlColor Code, in my program I have a DataGridView which is showing me the values in hexCodes, and I can click on it and change the color value with a ColorDialog and the then set the cell BackColor to the selected Color and gives me the hexCode back as new value.
sorry I cant post a picture asI dont have 10 reputaions (I'm new)
what I want is that when I open the XML file in my programm, the cells should have the BackColor of what is wrriten inside the cell.
I tried this but doesn't work :(
private void dgvColors_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
Theme theme = new Theme();
foreach (KeyValuePair<string, Color> colour in theme.Colors)
dgvColors.Columns["colKey"].DefaultCellStyle.BackColor =
ColorTranslator.FromHtml(colour.Value.ToString());
}
After you have set the FlatStyle to Flat you can change the Backcolor for each cell. With the Normal style you only see a 1 pixel wide frame around the Button.
This example creates DataGridViewButtonCells and paints these Cells after the DGV has been loaded:
for (int r = 0; r < DGV.Rows.Count; r++)
{
DGV[4, r] = new DataGridViewButtonCell();
((DataGridViewButtonCell)DGV[4, r]).Style.BackColor = Color.OrangeRed;
((DataGridViewButtonCell)DGV[4, r]).FlatStyle = FlatStyle.Flat;
((DataGridViewButtonCell)DGV[4, r]).Value = r + "RR";
}
Your code should be adaptable if you get the hex values right.
So the problem was with the ColorTranslator(),
it works well with this version of code :
private void dgvMenuColors_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow row in dgvMenuColors.Rows)
{
row.DefaultCellStyle.BackColor = ColorTranslator.FromHtml(row.Cells[1].Value.ToString());
}
}
:D
I am trying to change the forecolor of a specific cell in datagridview. I want to give different colors to different cells in the same row.
grid.Rows[row].Cells[col].Style.ForeColor = Color.Red
Using the above will change all the Row color and not just the cell I want to change.
Is there a way to change color of specific cell solely - not affecting other cells of the row?
It seems like I need to change some Row property that I am not familiar with.
Using the above will change all the Row color and not just the cell I want to change
No, this is not correct. It will only change the text color (Forecolor) of the cell at the specified index.
You need to check that you are not setting the forecolor of row somewhere else in your code.
following code works fine for Changing the back and forecolor
//this will change the color of the text that is written
dataGridView1.Rows[0].Cells[4].Style.ForeColor = Color.Red;
//this will change the background of entire cell
dataGridView1.Rows[0].Cells[4].Style.BackColor = Color.Yellow;
If you apply the Style or set Row.DefaultStyle immediately after loading the default data(datagridview.Datasource=Table),
it will not affect until you load grid next time.
(ie if you set style in load event.It wont get affected.But if you call the same function again like after clicking button or something it will work)
Work around for this :
Set the style in DatagridView_DataBindingComplete event. It will work fine and change the color you can also
Use the CellFormatting event:
void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewCell cell = grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell.Value is double && 0 == (double)cell.Value) { e.CellStyle.ForeColor = Color.Red; }
}
in if you can write your condition to find specific cell.
or try this one.
private void ColorRows()
{
foreach (DataGridViewRow row in dataGridViewTest.Rows)
{
int value = Convert.ToInt32(row.Cells[0].Value);
row.DefaultCellStyle.BackColor = GetColor(value);
}
}
private Color GetColor(int value)
{
Color c = new Color();
if (value == 0)
c = Color.Red;
return c;
}
private void dataGridViewTest_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
ColorRows();
}
you can use
dgv.Rows[curRowIndex].DefaultCellStyle.SelectionBackColor = Color.Blue;
I have a List<Appointment> where an Appointment is.
public class Appointment
{
public string Title { get; set; }
public string Start { get; set; }
public string End { get; set; }
}
I want to dynamically add each list item on a separate line on the form like so:
item.Title + " between" + item.Start + " and " + item.End;
I want to be able to click each item (the text), then with each click it can toggle the colour of the text between red and black (that is, if black it turns red, if red it turns black when you click).
I come from a web background, but I am just struggling with Windows Forms data binding. I've tried with table layout panel but just don't know where to begin with changing the color of an item on click.
PS: If it helps, the number of items in the list will probably not be more than 10.
I've gotten a bit further as per Jamie Ide's comment:
var appts = GetAllCalendarItems();
foreach (var item in appts)
{
Label label = new Label();
label.Text = item.Title + " between" + item.Start + " and " + item.End;
label.Click += new EventHandler(label_Click);
flowLayoutPanel1.Controls.Add(label);
}
...
private void label_Click(object sender, EventArgs e)
{
// This is wrong - what goes here??
((Label)sender).ForeColor = Color.Red;
}
Dynamically laying out Windows Forms is a huge pain. I don't have time to code this but the steps are:
Add FlowLayoutPanel to form as a container
Look through your Appointments and create label controls for each
Add the label controls to the panel's Controls collection
Assign an OnClick handler to each label control to toggle the color
Don't bother with databinding for this.
If you haven't changed the label's initial color from the default, this will toggle it:
private void label1_Click(object sender, EventArgs e)
{
var lbl = (Label)sender;
var toggle = lbl.ForeColor == SystemColors.ControlText;
lbl.ForeColor = toggle ? Color.Red : SystemColors.ControlText;
}
You could add each text field as a member of a Label or List view item. Then handle the "OnClick" or "SelectedIndexChanged" event. To create an an OnClick event handler double click on the control in the design view. Edit the handler like this:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
listView1.SelectedItems[0].ForeColor = Color.Red;
}
If you are unsure about event handlers, don't be put off they are quite easy, just look them up here perhaps. If the list view is not what you are looking for, try the same approach on a different control.
I guess I can't comment on Jamie's answer, but raklos you can programmatically add the OnClick method by doing:
label.Click += new EventHandler(label_Click);
Visual Studio should auto-generate the stubs for you when you start typing that out.
Something like this could get you started:
private void label_Click(object sender, EventArgs e) { ToggleTextColor((Label)sender); }
private void ToggleTextColor(Control control)
{
var currentColor = control.ForeColor;
control.ForeColor = currentColor == Color.Red ? Color.Black : Color.Red;
}
You can cheat and make create it in a WebBrowserControl.
You will be in familiar ground.
Use ObjectforScripting for WeBbrowser <=> winforms communication.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting.aspx