I have a ComboBox. It is critical that the user cannot scroll by accident and change the selected value.
How can I prevent the ComboBox from changing the value and text when the use scrolls? Thanks.
Visual Studio 2008
combobox.MouseWheel += new MouseEventHandler(combobox_MouseWheel);
void combobox_MouseWheel(object sender, MouseEventArgs e)
{
((HandledMouseEventArgs)e).Handled = true;
}
If you don't want the user messing with the control, disable it. On another level however, if it's critical the user NOT use the control... maybe you should change the control.
ComboBox.Enabled = false;
Related
I'm working with Xamarin.Forms and I made a CustomRenderer for Checkbox in UWP. When I set all the Checkboxes of my items in the ListView to true by clicking the button "Alle", the Checkboxes are displayed correctly with the check inside the box:
However, if I hover my mouse over the Checkboxes, they immediately change their appearence (the check disappears but it's still selected). In the following screenshot, I moved my cursor over the 3rd - 7th Checkboxes:
This is my overridden OnElementChanged method in the CustomRenderer:
protected override void OnElementChanged(ElementChangedEventArgs<EvaCheckbox> e)
{
base.OnElementChanged(e);
var model = e.NewElement;
if (model == null)
{
return;
}
nativeCheckbox = new CheckBox();
CheckboxPropertyChanged(model, null);
model.PropertyChanged += OnElementPropertyChanged;
nativeCheckbox.Checked += (object sender, Windows.UI.Xaml.RoutedEventArgs eargs) =>
{
model.IsChecked = (bool)nativeCheckbox.IsChecked;
};
nativeCheckbox.Unchecked += (object sender, Windows.UI.Xaml.RoutedEventArgs eargs) =>
{
model.IsChecked = (bool)nativeCheckbox.IsChecked;
};
SetNativeControl(nativeCheckbox);
}
I tried to override the PointerEntered event of nativeCheckbox. It works, for example if I set the model.IsChecked to true on this event, it will be set to true:
nativeCheckbox.PointerEntered += (s, args) =>
{
model.IsChecked = true;
};
But I don't know how to (if even at this place) prevent the checkbox from changing it's appearance when moving the cursor above the Checkbox. Just leaving the triggered event with empty code like this won't change anything about the described behaviour:
nativeCheckbox.PointerEntered += (s, args) => { };
How can I prevent the Checkbox from changing it's appearance when I move my cursor over it?
Update:
I've created a sample project for this issue. You can find the repository here: https://github.com/Zure1/CustomCheckbox
It has the exact same described behavior. In the following screenshot I pressed the button "All" on the bottom of the screen and then the checkboxes look like correct with a check inside of them:
After moving the mouse cursor over the bottom 3 checkboxes, their change their appearance:
Information: I'm debugging on my desktop (Windows 10). I don't know if this issue exists on WinPhone. Just in case you're wondering why my checkboxes are red: My system color in Windows is red.
This is a tricky one as I have been struggling with this issue for a while, I'll try my best to answer this.
TL;DR: It's caused by ViewCell.
The issue comes down to Xamarin Forms ListView and ViewCell.
I haven't been able to track down the cause yet for many months and the way I get around this issue is by refreshing the ListView every time a change happens forcing a redraw of the entire ListView which can really impact performance.
My educated guess on what the cause could be is the rendering code for the ViewCell is missing something.
As for your particular issue, I have created a CheckBoxCell which you can use to display a list of checkboxes with a title. I forked your project and made the changes.
This will display something similar to what you are trying to achieve and doesn't have rendering issues so will be a good starting point. You are able to customize this to display images and the like but you'll have to do that in the platform-specific layout code.
Please note that I have only created the code for UWP and that should be enough to get you going for the other platforms.
I hope this helps somewhat.
Can i make ListView or ListBox editable by user?
For example: user can add a new item at list (without any buttons).
Can i do that? Maybe give some simple example.
P.S. It is about WPF.
In winforms this is simple with a ComboBox. The Text is added if it is new when the user presses Enter:
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
if (!comboBox1.Items.Contains(comboBox1.Text))
comboBox1.Items.Add(comboBox1.Text);
}
There was no WPF tag at first, but the same should be possible in WPF as well..
(Make it editable, set the dropdown to visible and catch the enter key..can't provide code atm)
Update: After a rather quick check it seems WPF can't do it out of the box. I'm (somewhat) surprised that a useful control (an editable listbox) that has bee with Windows since the 90s (at least) is no longer there. But maybe I'm wrong..
There is no suitable method to do this trick without buttons. Try DataGridView instead http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx
sorry my english language is bad
I want make a button by another control and this button editable in design mode (change size, define onclick,... )
i create a button in design mode by programming but onclick of this not work in design time
btnTest.Click += new System.EventHandler(this._Click);
protected void _Click(object sender, EventArgs e)
{
if (DesignMode)
{
MessageBox.Show("Hi Design Mode.");
}
else
{
MessageBox.Show("Hi Run Mode.");
}
}
don't show show message
for that you need to understand what are you putting in the condition. read here on Control.DesignMode Property:
Gets a value indicating whether a control is being used on a design
surface.
The DesignMode property will be set to True when you are editing your winform page in Visual Studio. For example if you create a user control you could show a user control based on dummy data during design-time and at run-time generate a user control based on the provided data
i don't think you can push a button in designer time so this code does nothing i'm afraid
I know that the question might seems to easy, but I can't find solution. I have CheckdListBox in my form. I have list of checkboxes inside. If I do that:clbxMyControl.Enabled = false; then I can't scroll to see all items. How to allow scrolling on disabled CheckedListBox?
Thanks!
Instead of disabling the control you should change it's SelectionMode like this:
checkedListBox1.SelectionMode = SelectionMode.None;
The user won't be able to select an item but will be allowed to scroll
You can prevent the user from checking items with the ItemCheck event:
bool listEnabled = true;
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (!listEnabled) e.NewValue = e.CurrentValue;
}
But do make sure that it is obvious that checking no longer works:
private void setListEnabled(bool enabled) {
listEnabled = enabled;
if (listEnabled) checkedListBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
else checkedListBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
The ListBox has only one handle in the Windows API which is set to enabled or disabled. Therefore there is no native way to enable the scrolling but the items.
You might be able to build your own control that has the requested behavior but you will have to paint it your self from scratch which might be a lot of work or you find a third party control that supports the behavior.
Or you can not use the enabled property but change the background/font color to make it look disabled and set:
checkedListBox1.SelectionMode = SelectionMode.None;
Another alternative might be the DataGridView. It is much more powerful, I'm not sure if it disables the scrollbar, too but if it does you are able to make the cells readonly and color them.
You can use this code to make CheckedListBox scrollable.
clbxMyControl.SelectionMode = SelectionMode.None;
Because Enable=false; make control unscrollable because this make the container of checkbox disabled.
I want to set the default property of a checkbox to true
I suppose you only mean that on opening a form, one or more check boxes are checked .
Simply write in the Form_Load method
private void Form_Loaded (object sender, RoutedEventArgs e) {
CheckBox1.IsChecked = true;
}
chkEntregue.CheckState = CheckState.Checked;
Set the Checked property to True in the Properties window of Visual Studio at design time.
To set CheckBox:
CheckBoxName.SetCurrentValue(CheckBox.IsCheckedProperty, true);
To unset CheckBox:
CheckBoxName.SetCurrentValue(CheckBox.IsCheckedProperty, false);
You haven't specified which platform, so I'll answer this for WPF, in which it's definitely possible.
You can use the OverrideMetadata method on CheckBox.IsCheckedProperty to provide a default value of "true" for all CheckBoxes. Add this code to your App class (in App.xaml.cs):
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
CheckBox.IsCheckedProperty.OverrideMetadata(typeof(CheckBox),
new FrameworkPropertyMetadata(true));
}
For a given checkbox? Edit the form in the forms designer, and change the Checked property to true.
For all checkboxes in the environment, without changing each individually? Can't be done. Though I suppose if you got really ambitious, you could write a post-compiler or some such.