I wrote a code in Windows form with a checkbox that enable an Update button when the user check. Now, I am required to change the code into WPF because of UI requirement. How can I convert this code below into a working format in WPF.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
License_agreement();
}
private void License_agreement()
{
updatebutton.Enabled = checkBoxforupdate.Checked;
}
My attempted codes is below:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
}
private void License_agreement()
{
updatebutton.Enabled = checkboxlincense.Checked;
}
I would do it in the XAML rather than the code behind.
Set the Button's IsEnabled="{Binding ElementName=checkboxlincense, Path=IsChecked}"
I figured out how to do it. I just needed to Disable the Update button initially in the property section such as: IsEnabled="False".
Change
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
}
...to:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
License_agreement();
}
private void License_agreement()
{
updatebutton.Enabled = checkboxlincense.IsChecked;
}
I will leave it to you to simplify but this closest matches your original code.
Related
I am tryting to hide textbox when checkbox value is true which I've done but when unchecked the textbox doesn't hide what can I do to fix this?
Here is my code
private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void checkBox_Checked(object sender, RoutedEventArgs e)
{
Handle(sender as CheckBox);
}
private void checkBox_Unchecked(object sender, RoutedEventArgs e)
{
Handle(sender as CheckBox);
}
void Handle(CheckBox checkBox)
{
bool chkd = checkBox.IsChecked.Value;
if (chkd)
{
textBox4.Visibility = Visibility.Visible;
}
else
{
textBox4.Visibility = Visibility.Hidden;
}
}
Just use something like this:
private void checkBox_CheckChanged(object sender, RoutedEventArgs e)
{
textBox4.Visibility = (checkBox.IsChecked) ? Visibility.Visible : Visibility.Hidden;
}
Add this to the CheckChanged event like this:
checkBox.CheckedChanged += checkBox_CheckChanged;
You can try the following solution -> Binding to a WPF ToggleButton's IsChecked state
That solution basically related the check box to the content that it wants to hide on xaml using a converter instead of code behind.
I have a form with 6 buttons. These buttons serve to increase/decrease tha value of the respective textbox. Now I'm trying to "animate" the buttons. I want to get another effect on the button when the mouse is over him.
To do that, I have two diferent images in Resources and I am doing this code:
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.DownHover;
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.Down;
}
This works fine. My question is: it wouldn't be wise to create a class (ButtonBehaviour.cs) and put this code in that class?
So I would have something like this:
ButtonBehaviour buttonBehaviour = new ButtonBehaviour();
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
buttonBehaviour.buttonDownHover();
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
buttonBehaviour.buttonDownLeave();
}
And the class should be:
public class ButtonBehaviour {
public void buttonDownHover() {
// code
}
public void buttonDownLeave() {
// code
}
}
How can I create this Class Behaviour and make the buttons adapt this Behaviour?
if one effect should be applied for all buttons, try to add the same event handlers to them
private void btn_MouseHover(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.DownHover;
}
private void btn_MouseLeave(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.Down;
}
button which raised event is available via sender variable
this way you avoid code duplication for every button. creating a ButtonBehaviour or CustomButton is probably an over-engineering unless you need them in many forms
First, pardon my new-ness, I just started coding class recently. Now, upon startup, I want parts of my form (c#) to not be shown, however when I put
NameDisplay.Visible = false;
(NameDisplay being the label I wish to hide) into my Form1.cs it gives me the error of that it is a 'field' being used as a 'type'. How do I correct this, and apply to other object types (buttons, textboxes, etc?)
EDIT 1-
Code- as it stands
namespace ATM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label NameDisplay;
NameDisplay.Visible = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Remove Label NameDisplay;, and place NameDisplay.Visible = false; into your FormLoad event.
The loading of a form is an event just like clicking a button, and will execute the code like so.
Also, when I hide labels, I use .Hide(), but I believe that only works on WinForms.
Hope this helps!
You need to drag and drop the Label on the form and object will be created and initialized automatically in InitializeComponent.
In the form constructor (after InitializeComponent function) or Form_Load event, you may set the visibility to false
For example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NameDisplay.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
How to display the dropdown list of a comboBox when I click/enter it?
private void comboBox1_Click(object sender, EventArgs e)
{
}
or
private void comboBox1_Enter(object sender, EventArgs e)
{
}
Since you have not mentioned if this is a Web/Windows/WPF so I am suggesting to do this
Winforms
((ComboBox)sender).DroppedDown = true;
WPF
((ComboBox)sender).IsDropDownOpen = true;
i try to save user settings in c#.
I can read the value and put it in a textbox but i can not change it.
This is my WindowsForm with an textbox and a save button:
namespace tool
{
public partial class Form4 : Form
{
string source = Properties.Settings.Default.Source;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = source;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = source;
Properties.Settings.Default.Save();
Application.Exit();
}
}
}
And here is a picture with my settings:
I hope someone as an idea :-)
Thanks for help
Try this:
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = textBox1.Text;
Properties.Settings.Default.Save();
Application.Exit();
}
You need to use what is currently in the text box, not your member variable.
Or you can change this event to be as follows (and then you don't have to modify the above):
private void textBox1_TextChanged(object sender, EventArgs e)
{
source = textBox1.Text;
}
could you possibly have a read lock being applied to the key you're looking at while testing this? Maybe the code's not the problem?