update datagridview cell style - c#

I am trying to allow the user to change the color of the datagrid displayed in my app.
for this I use user settings and colordialog boxes.
problem is, when I try to update the color, it isnt displayed, and I either have to close/reopen the app to see the changes, or load a completly different DGV (they are in a tabcontrol).
here is the code I use at the update:
AlternatingRowsDefaultCellStyle.BackColor = Properties.Settings.Default.CellBackColor1;
am I missing something?
I tried to refresh the DGV, but it doesnt change a thing.

I added a Button to a form that opens up a ColorDialog and waits for the users to select a color. If the user hits the OK button, it first saves the setting and then sets the dataGridView.CellStyle.BackColor to whatever color the user selects. This forces an immediate update of the form with the correct background color.
private void button1_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
Properties.Settings.Default.CellBackColor1 = colorDialog1.Color;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = colorDialog1.Color;
}
}
I assume your Form.Load and Form.FormClosing events handle the saving of the settings.

Related

How do I highlight the selected item (tab index location) in my windows form application?

I want to be able to highlight the selected item on my page because it's busy and there is a lot going on. The user will not have access to their mouse, only keyboard so currently they tab through buttons quickly and enter in to what they need to do (it's a fast data entry sort of app if you must know).
I want to be able to highlight the selected button (so when you tab through currently it will select a button but it isn't very noticeable, it just has a slight border around it when selected).
I know that you can use a focusEnter and focusLeave event, but I would like to avoid that if at all possible just because there are so many buttons on the page that I would have to have a ton of repetitive events with almost the same code.
You can and should use just two common event handlers for Enter and Leave events for all your buttons!
Use the sender param to access the buttons:
private void buttons_Leaveobject sender, EventArgs e)
{
((Button)sender).BackColor = SystemColors.Control;
((Button)sender).ForeColor = SystemColors.ControlText;
}
private void buttons_Enter((object sender, EventArgs e)
{
((Button)sender).ForeColor = SystemColors.Control;
((Button)sender).BackColor = SystemColors.ControlText;
}
Use your own ideas about how to highlight the focussed button; this is a bit excessive imo..:
Of course Button with FlatAppearance can do the highlighting all by themselves as they have separate Colors for their states.
I would suggest creating your own class derived from Button, and then handling the background painting yourself. That would allow you to play with the background look/color and/or the border effects.

Windows Phone TextBox Control Foreground and Background Brushes not updating properly

TLDR: my textboxes will update their Foreground and Background colors properly when manually selected by touch input, but will fail to update if I use the Textbox.Focus() method after they are created.
The question is similar to this - windows phone - textbox background
My Application creates a textbox with an associated GotFocus event, that changes the Foreground and Background colors of the textbox to the system default whenever the textbox receives focus. Upon the user pressing the enter key on the keypad, the app then generates another identical textbox below the first.
The issue is that for any of these textboxes, if the textbox is pressed manually, everything works fine, and the textbox is displayed how it's meant to be. However, if I use TextBox.Focus() after the enter key is pressed, although focus is passed to the textbox, and the GotFocus event has been processed, the Background and Foreground colors are not updated, leaving white text on a white background.
I have tried passing focus between the textbox for a number of times (up to 10), and even though I can confirm that the focus is being passed as it should, the colors are only updated if the user gives focus to the textbox (if I give focus to the textbox via code, I must then manually deselect and then reselect it for the color change to apply. If I don't give focus via code I can simply select it).
The code for this is:
public void txtInputKeyUp(object sender, KeyEventArgs e)
{
TextBox txtBox = (TextBox)sender;
if (e.Key == Key.Enter)
{
Evaluate(txtBox);
InitializeDivider();
InitializeTextInput();
InitializeTextOutput();
txtInput[txtInput.Length - 1].Focus();
}
}
public void txtInputGotFocus(object sender, RoutedEventArgs e)
{
TextBox txtBox = (TextBox)sender;
if(txtBox.Text == "Input Here")
{
txtBox.Text = "";
}
txtBox.Foreground = (SolidColorBrush)App.Current.Resources["PhoneForegroundBrush"];
txtBox.Background = (SolidColorBrush)App.Current.Resources["PhoneBackgroundBrush"];
}
The Initializeblabla stuff basically just creates the relevant textboxes and all associated data. I've tried switching focus between textboxes, disabling and enabling the specified textboxes and a few other options, and consistently the only thing that works is not giving focus to the textbox via code, but rather waiting for the user to select the textbox, something I'm not really satisfied with. Attempting to manually edit the style didn't help either.
My eventual solution was to add a timer to the app with a very short interval (short enough to make the change not visible) and have that change the color of the textbox after the textbox had received focus.

RichTextBox as output and TextBox as input on one form, how to select from output while maintaining focus on input?

I have a RichTextBox as output and TextBox as input on the main WinForms form. I would like to be able to keep focus on the TextBox while highlighting text in the output with the mouse. That would allow me, as an example, to type something in the input and simultaneously select something in the output with the mouse.
I saw this done in one application which isn't necessarily WinForms based, but it does run on a Windows machine.
How can I do this with WinForms?
You can try something along the lines of
bool selecting;
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
selecting = true;
}
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
if (selecting)
textBox1.Focus();
selecting = false;
}
This resets the focus on the TextBox as soon as you finish selecting from the RichTextBox. The problem however is that, as soon as the focus is restored, the selection is cleared.

how do i change the checkbox color to dark grey (disable like) in c#?

i made custom checkbox tree and i want the first check box to be in Dark grey color instead of white (the check box itself not his text..)
*. the custom checkbox tree is several checkboxes that order in different places in the form..
how can i do it ?
I don't think there is a direct way to do so. However, you can make your own CheckBox using a Button and a Label.
Change button background image or color on events like "button1_Click".
Inside the event you can make a flag that says if its checked or not checked, and change it with every click event. When the flag is "true" the background image will be image with "V" and when its "false" it will be changed to an image without "V".
Instead of calling checkBox1.Checked you can check if the flag is true or false.
(source: katzer at www.uni-graz.at)
if you need help with making the 2 images, you can e-mail me and I will do it.
The Code can be like this:
//"checked" and "notChecked" are the images names.
private void button1_Click(object sender, EventArgs e)
{
if(checkedFlag == true)
{
button1.BackgroundImage = Properties.Resources.notchecked;
checkedFlag = false;
}
else
{
button1.BackgroundImage = Properties.Resources.checked;
checkedFlag = true;
}
}
You only need to make the flag start value as false and make the image centered or stretched.

Winforms: how to open combobox properly?

I have a combobox on the winforms. On Enter even I open it:
cbo.DroppedDown = true;
But if I do that the combo opens and closes immediately. What am I doing wrong?
The reason why I use this event is I need to open combo on Tab, when user click tab on the previous control, this combo opens properly. But if user clicks the combo it opens and closes. How to do that properly?
Thanks
I tried it just like this:
private void comboBox1_enter(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
no changes to mouseup or timers. it behaved just as expected. Whether I selected the comboBox with a mouse click or tabbed into it the drop down list appeared and stayed open until I selected something.
I would look to see if there is something else pulling focus off the box.
The reason you are having this problem is because the mouseup event occurs after the enter event and the default window procedure is closing the combobox.
In the enter you could check the mouse button status and if the button is down, do not open the combo. Then have another event handler for the mouseup event to open the combo.
Another option is to set a timer for a few milliseconds, and open the combo when it goes off.
Knowing this is a bit old, but I found that this works well. You can TAB into the combo box and it opens and if you click the arrow it doesn't close back up.
private void ComboBox_Enter(object sender, EventArgs e)
{
if (MouseButtons == MouseButtons.None)
((System.Windows.Forms.ComboBox)sender).DroppedDown = true;
}
Set DroppedDown = true in GotFocus event of the combobox. Otherwise, the dropdown list will show at wrong location.
void cbo_GotFocus(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
cbo.DroppedDown = true;
}
I think you just need to focus it first before open the comboBox.
cbo.Focus();
cbo.DroppedDown = True
Hope it works for you.

Categories

Resources