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.
Related
Hi could some kind person help. I have two user controls. One with a textbox, the other with a Combobox. The Main window will perform calculation routine as soon as combos and textboxes are modified.
The Textbox version works, the ComboBox doesn't. The only difference I can see is
Textbox uses TextChangedEventArgs
whereas
Combobox uses System.EventArgs
Any ideas?
Thanks
// UserControl - with TextBox
public event RoutedEventHandler ucTextChanged;
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if (ucTextChanged != null)
{
ucTextChanged(this, new RoutedEventArgs());
}
}
private void txtValue_TextChanged(object sender, TextChangedEventArgs e)
{
OnTextChanged(sender, e);
}
// UserControl - ComboBox
public event RoutedEventHandler ucComboChanged;
private void OnComboChanged(object sender, RoutedEventArgs e)
{
if (ucComboChanged != null)
{
ucComboChanged(this, new RoutedEventArgs());
}
}
private void ucCombo_DropDownClosed(object sender, System.EventArgs e)
{
OnComboChanged(sender, e);
}
Try looking at the event SelectionChanged of the ComboBox ( https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.combobox.onselectionchanged )
XAML:
<ComboBox SelectionChanged="ucCombo_SelectionChanged"></ComboBox>
C#:
private void ucCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// your code here...
OnComboChanged(sender, e);
}
I am trying to make the text of the button change colour when a checkbox is checked, but for some reason, I just don't know how. Would I need to write an If statement, if so how do I do that?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ColourCheckBox.ForeColor = Color.Red;
}
private void ColourCheckBox_CheckedChanged(object sender, EventArgs e)
{
ColourCheckBox.ForeColor = Color.Black;
}
}
Your question is so obscure, but based on the things that I understand, you should check the Checked property.
private void ColourCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (ColourCheckBox.Checked)
{
ColourCheckBox.ForeColor = Color.Black;
}
else
{
ColourCheckBox.ForeColor = Color.Red;
}
}
In the CheckedChanged event, you can use the Checked property:
ColourCheckBox.ForeColor = ColourCheckBox.Checked ? Color.Black : Color.Red;
In case of a Triple state checkbox, with 3 color you can switch on CheckState value :
Unchecked = 0
Checked = 1
Indeterminate = 2
using System.Drawing;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
button1.ForeColor = Color.Red;
}
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.
In my code I want to perform some actions when some controls are focused. So instead of having one handler for each control i was wondering if there could be any way of adding all controls to the handler and inside the handler function perform the desired action.
I have this:
private void tb_page_GotFocus(Object sender, EventArgs e)
{
tb_page.Visible = false;
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
I want this:
private void AnyControl_GotFocus(Object sender, EventArgs e)
{
if(tb_page.isFocused == true)
{
...
}
else if (tb_maxPrice.isFocused == true)
{
...
}
else
{
...
}
}
Is this possible? How could I do it? Thanks a lot.
Iterate your controls in your form or panel and subscribe to their GotFocus Event
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this)
{
c.GotFocus += new EventHandler(AnyControl_GotFocus);
}
}
void AnyControl_GotFocus(object sender, EventArgs e)
{
//You'll need to identify the sender, for that you could:
if( sender == tb_page) {...}
//OR:
//Make sender implement an interface
//Inherit from control
//Set the tag property of the control with a string so you can identify what it is and what to do with it
//And other tricks
//(Read #Steve and #Taw comment below)
}
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;