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.
Related
In my application I am displaying a new form which needs to be at TopLevel.
So, I am setting
someForm.TopLevel = true;
Now, I have a checkbox, which will allow user to set it to "not a top level".
When unchecked, i want to set TopLevel = false
But when I do this, my form disappears. Does anyone know why?
Here is my code:
private void stayOnTop_CheckedChanged(object sender, EventArgs e)
{
this.TopLevel = this.stayOnTop.Checked;
}
Because your checkbox is named stayOnTop, I assume you want to set the TopMost property instead of TopLevel.
The setting TopLevel is only meaningful in MDI applications - ones where a parent form has one or more child forms inside it (like Word and Excel used to work).
TopLevelControl is the main form of your application. By setting TopLevel to false the TopLevelControl is set to null. In that case there is no main form to display for your application. If you add a timer that will switch it back to true you will see that it appears again. (It is interesting though that there is no preventing mechanism. For instance it's not possible to add a top level form to another top level form. But you are allowed to get rid of top levels forms entirely.)
So that's why it disappears. If you want it only sent to background you can use SendToBack() method. It will change Z-index of the form. So if there is a window behind your control, the control will be moved behind the window.
Try these codes :
private void stayOnTop_CheckedChanged(object sender, EventArgs e)
{
if(e.checked == true)
{
someForm.TopLevel = false;
}
else
{
someForm.TopLevel = true;
}
}
I need to make password box as non-editable in wpf.
I used
IsEnabled = false
But it is affecting my style some blur effect came because of that...
Is there any other way to achieve this ?
I know this is two years old, but I had the same need and solved it this way:
The combination of these two properties, both set to false, will prevent entry/editing in the control, w/o affecting your desired style:
Focusable, IsHitTestVisible
You can handle the PreviewTextInput event, preventing the user from entering text. Like so:
Xaml:
<PasswordBox PreviewTextInput="HandleInput"/>
Codebehind:
private void HandleInput(object sender, TextCompositionEventArgs e) {
e.Handled = true;
}
One solution is to make a custom functionality to mimic the IsReadOnly.
There are couple things to take care of - e.g. clipboard pasting
also.
You'll get similar behavior by defining some attached property (e.g. IsPasswordReadOnly or just the same) - which would work out all that's required.
Here is a good starting example - which could, should I think work for Password box as well - but I haven't tried it and you gotta test yourself.
Readonly textbox for WPF with visible cursor (.NET 3.5)
You'd have to replace references to TextBox with PasswordBox, rename it to IsReadOnly - and I think the rest might work the same.
And you use it like...
<PasswordBox my:AttachReadOnly.IsReadOnly="True" />
Pretty simple..
Set an event handler for PreviewTextInput in your XML like so:
PreviewTextInput="PasswordBoxOnPreviewTextInput"
And within that method:
private void PasswordBoxOnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (m_DisablePasswordBox)
e.Handled = true;
}
It will now prevent you from typing anything in :)
How do I set a C# checkbox property to "unchecked" for the purpose of saving a setting? I'm using the .NET settings feature to store the state of a checkbox. When the form loads it grabs that setting and checks or un-checks that checkbox accordingly. The code below works to set the setting to true. But there is no definition for unchecked in this context.
Can anyone help?
Thanks.
PS. This is using VS2010, C# and .NET 3.5
chkBackup.Checked will contain false if the box is unchecked, so you can just always use chkBackup.Checked to get what you need. The following code should be all that you ever need -
private void chkBackup_CheckChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Backup = chkBackup.Checked;
Properties.Settings.Default.Save();
}
Use (chkBackup.Checked == true) as the value:
Properties.Settings.Default.Backup = (chkBackup.Checked == true);
Properties.Settings.Default.Save();
This also gets rid of your enclosing if statement, and will work with properties that are nullable bools (bool?)
I use frame to load pages in my WPF project, is there a way to detect a page loading is the first or not? Something like "IsPostBack" in ASP.NET, I'm trying to find an equivalent to it in WPF.
I found IsPostBack is a property in System.Web.UI, should I include this namespace in my page?
I still have to use a static variable "bool SystemLoad = true", at the first load it is True and then I set it to False, so when the page is reloaded, it doesn't do as at the first load.
Thank you!
IsPostBack is not relevant to a WPF application, and since your WPF application window does not inherit 'Page', there is no way you can use IsPostBack variable from System.Web.UI.
The best you can do is to implement your custom logic as below.
private bool isLoaded;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (isLoaded)
return;
isLoaded = true;
}
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;