I have an issue on my code.
I need to change RichTextBox Font, while keeping the previous text formatting (Bold, Italic, Underline, Strikeout).
My form has RichTextBox; and a ToolStrip containing some ComboBoxes (where I select the Font, and set the Font Format).
Each of the controls above Controls have two events (TextChanged and SelectedIndexChanged) subscribed; running the same line of code:
My RichTextBox HideSelection property is set to false; so I can always see what Font I'm changing to and to prevent my selection from disapearing.
Please find bellow my actual code:
private void TcboSize_TextChanged(object sender, EventArgs e)
{
rtfDirections.SelectionFont = new Font(tcboFonts.Text, (float)Convert.ToDecimal(tcboSize.Text), rtfDirections.SelectionFont.Style);
}
UPDATE:
I've moved to the Form Constructor:
rtfDirections.HideSelection = false;
In order to prevent my RichTextBox Text Selection from Disappearing.
I've subscribed the SelectedIndexChanged on my ComboBoxes; in order to change the Font when the Index changes.
This sets the Font style (including Size) on the RichTextBox, and updates the ComboBox selected Item(s) Text.
My problem:
I wasn't tracking any previous selection.
When dealing with selected text; I should've assigned it to a variable; then make sure the selection is equal to that variable.
I also noticed that (sometimes) when I click any ToolStrip Button; my Text Selection disappears; which should not be the case.
As you can see above; I've set the RichTextBox.HideSelection to false.
I assume that when you select new Text in the RichTextBox; it should over write what was previously selected and use the new selection.
Should I be clearing the ComboBox Selection? If so; how can I accomplish this?
I've put break points on all lines of code and added watches to the selected text; but the Font never changes; unless I clear the Selection, but that way there are no breakpoint hits.
Related
I'm trying to replicate this behavior in the ColumnMappings window from SQL Management Studio:
Image Here
When you click inside a destination cell, the type changes from text
to combobox (it seems), and when you leave the cell, it takes the selected value from the combobox and changes back to textboxCell with the value included.
It does this for every cell in that column.
So, when I load the data, all the cells are textbox, and when the user enters any cell in that column, I do this:
private void dgvwMapping_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
string txt = dgvwMapping[e.ColumnIndex, e.RowIndex].Value.ToString();
DataGridViewComboBoxCell cbbxDestination = new DataGridViewComboBoxCell() { DataSource = new List<string>(someList) };
cbbxDestination.Value = txt;
dgvwMapping[e.ColumnIndex, e.RowIndex] = cbbxDestination;
}
}
So far so good, if I change from cell to cell everything goes fine, except when I click the cell that has the coordinates [1,1]. The only one that throws the "Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore" exception is the one with the columnIndex equal to the rowIndex.
I already tried wrapping the line where I reassign the Cell Type in an invoke call, like this:
dgvwMapping.BeginInvoke(new MethodInvoker(delegate ()
{
dgvwMapping[e.ColumnIndex, e.RowIndex] = cbbxDestination;
}));
But It loops indefinitely the event. Even wrapping all the code inside the event makes the event loop indefinitely.
I haven't coded anything inside the CellEndEdit or the CellLeave yet.
Does anyone have any advice? Perhaps my aproach at replicating that behavior is not the best.
Thanks
Your issue:
When you click inside a destination cell, the type changes from text to combobox (it seems), and when you leave the cell, it takes the selected value from the combobox and changes back to textboxCell with the value included.
is just a simple style setting for the DataGridViewComboBoxColumn property:
yourGridColumnComboBox.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
This will make it appear like a TextBox, but when it has focus with selectable items, it will show the drop down button.
I'm a beginner in C# and I'm trying to style a DataGridView button in Windows Forms.
I don't know how to, as example, remove the border from a button or change the hover color.
Many configurations that are on a normal button are missing in the DataGridView settings.
How can I achieve a full editable button inside DataGridView?
Datagridview works a little different than normal buttons,
but you can still edit several things in it if you search for the subproperties inside the properties. Let's go in a bit of detail:
DefaultCellStyle
Once you've selected your dataGridView, go to properties > RowTemplate. In there, you find something called DefaultCellStyle. if you press on the '...' at the right. then it'll open a popup that allows you to change some of the standard design of the Cells.
The same can also be applied to ColumnHeadersDefaultCellStyle, which is almost the same as DefaultCellStyle.
Columns
You can also go to Columns and add a new Column. After you've added a new column, you're also able to set several properties unique to that Column as well. You can even set all the cells in a Column to act as buttons!
Datagridview has certainely the access to customise, but most of them are divided in the cellstyles and column collections.
Changing the backcolor on hover
This in't as easily done as on a normal button, I did a search and came with this solution:
In properties, at the lightning button, you can see the events, there you can doubleclick on 'CellMouseMove' and add this. (Change the datagridview1 name eventually to be equall to yours)
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Blue;
}
Then doubleclick the 'CellMouseLeave' event so it can revert the color.
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
}
I hope this has helped you further.
When I run my App first I fill my datagridview, then I loop over my rows and change the font color, everything works fine but when I sort my rows (clicking on the head of datagridview column) the font color is back to it's origin color.
After checking this subject I found about the attribute 'Default Cell Styles' - I need to know how to disable it on a specific column, I need my font color not to change.
CurrentCellChange Event allows you to provide new action once your datagridview's cell has changed, so basically:
private void DataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
//your style code
}
I have a Form, that contain a DataGridView control named gridParameters.
I'm adding control on-top of it, based on some values, to add controls that are not available (for example, DateTimePicker):
DateTimePicker dtp = new DateTimePicker();
gridParameters.Controls.Add(dtp);
dtp.Location = rowCellLocation;
dtp.Size = rowCellSize;
dtp.CustomFormat = "yyyy/MM/dd";
dtp.Format = DateTimePickerFormat.Custom;
dtp.Tag = rowId;
dtp.ValueChanged += dtp_SelectedValueChanged;
dtp.Visible = true;
That code is inside a loop that iterate throgh the grid rows, and is in an If statement (I add DateTimePicker for some rows and NumericUpDown for others based on a row cell value). The variables rowCellLocation, rowCellSize and rowId are set in a loop for each row.
My DataGridView selection mode is a full row selection.
I want to make the added control (DateTimePicker, NumericUpDown...) get focus when I switch to the row, for example, when I click on a row with the mouse.
To do so, I've tried the following:
In the loop that creates the controls, I set the control to the relevant row Tag property.
In the grid Row_Enter event (I also tried CurrentCellChanged) I added the following code:
if (gridParameters.CurrentRow.Tag != null)
{
((Control)gridParameters.CurrentRow.Tag).Focus();
}
I've also tried using Select() and also
this.ActiveControl = (Control)gridParameters.CurrentRow.Tag;
None of the above sets focus to the control.
I place a break point, and the code is getting to this line, the Tag property contain the control that need to be focused, and execute it, but I can't get the control to focus.
UPDATE: Setting focus to another control outside of the DataGridView, works just fine (for example, setting focus to an "OK" button works).
I want to set the text in a combo box. Below is the code-
private System.Windows.Forms.ComboBox selectModel;
this.selectModel = new System.Windows.Forms.ComboBox();
this.selectModel.Name = "selectModel";
this.selectModel.FormattingEnabled = true;
this.selectModel.Size = new System.Drawing.Size(64, 21);
this.selectModel.Location = new System.Drawing.Point(3, 76);
this.selectModel.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
The following line is not working-
selectModel.SelectedText = getModelNameFromConf();
The documentation says that "it Gets or sets the text that is selected in the editable portion of a ComboBox". I can't make it editable to user.
Any workaround please.
This is because when you use ComboBoxStyle.DropDownList, the dropdown has no editable portion. To make it editable, use ComboBoxStyle.DropDown.
Note too the remarks on the SelectedText property relating to whether the control has focus. You may find the Text property more suitable for many purposes.
EDIT For example:
selectModel.Text = getModelNameFromConf();
Assuming the combo contains that value in its list, setting Text will also set the SelectedIndex property of the dropdown.
(I think some of the property names of this control are particularly confusing, including DropDown vs. DropDownList. Someone at MS had a bad day when this control was coded. Note also that the word selection is being used in two different ways: here, you want to set the selected item, whereas SelectedText means some text that is selected—which might not be the whole of the item text. This is the same as in a textbox where the user has dragged the mouse to highlight some of the text but not all of it.)