I need to display value and text contained in the particular CheckBox which is checked on its checked event in WPF. How to do that?
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show(........need help here......);
}
I'm not sure I understood your expectations. You want to retrieve the value "checked" - "unchecked" from the checkBox?
So can you try this?
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
//Get the boolean current value [true or false]
bool valueSelectedToBool = (sender as CheckBox).IsChecked;
//Get the string current value ["true" or "false"]
string valueSelectedToString = (sender as CheckBox).IsChecked.ToString();
MessageBox.Show(valueSelectedToString );
}
You could try this :
I don't know if you want the uncheck action to trigger the event but I put it.
In XAML :
<CheckBox Content="CheckBox" VerticalAlignment="Top" Unchecked="CheckBox_Checked_1" Checked="CheckBox_Checked_1"/>
In C# :
private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
{
CheckBox check = sender as CheckBox;
MessageBox.Show(check.IsChecked.Value.ToString());
}
Just tested it, it works.
I hope it is what you are searching for.
I'm working with WPF.
I used these lines of code. In my case it works properly.
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
bool Chked = Convert.ToBoolean((sender as CheckBox).IsChecked);
string ChkBoxContent = (sender as CheckBox).Content.ToString();
TxtHabitsHx.AppendText(ChkBoxContent);
}
Related
I am trying to get the text value from a button that was clicked. In my head, it looks something like this:
private void button2_Click(object sender, EventArgs e)
{
string s = thisbutton.text
}
The object which fired the event is sender, so:
private void button2_Click(object sender, EventArgs e)
{
string s = (sender as Button).Text;
}
Just cast the sender Object to a Button Object and access the text attribute :
protected void btn_Click (object sender, EventArgs e){
Button btn = sender as Button;
string s= btn.Text
}
Should be like this:
private void button2_Click(object sender, EventArgs e)
{
string s = this.button2.Text;
}
In every build in event handler there are 2 parameters sender and e.Sender takes reference to that object which fires the event.The second parameter e holds some information about the event(such as the location of pointer and other of this kind)
You need only bring it to Button type and get what information you want
try and apply this example in your button event
private void button_click(object sender, EventArgs e)
{
var getValue = ((Button)sender).Text; //this will get the value of the text using sender
}
This was asked some time ago and the platform in my case may be a little different to what the OP used but I arrived at the same question for GTK.
I am developing in Xaramin / Visual Studio in OSX using GTK2+, and for me the original accepted answer is close, but yields an error that .Text doesn't exist. In my case, it needs to be Label. This works for me:
protected void Button_Clicked(object sender, EventArgs e)
{
Button btn = sender as Button;
lblWhichButton.Text = btn.Label;
if (btn.Label == "<<<" )
i--;
else
i++;
lblCounter.Text = "" + i;
}
I am using mask edit TextBox.The textbox always shows 0 (zero). I cannot type any key from the keyboard. I need to delete the zero first then I can type digits. Therefore I am doing extra steps here. Is it possible to type as soon as I type from the keyboard? Any suggestion is welcome.
private void DateDay_GotFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == "Day")
((TextBox)sender).Text = string.Empty;
}
private void DateDay_LostFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == string.Empty)
((TextBox)sender).Text = "Day";
else
CheckForCorrectDateDay((TextBox)sender);
}
I have tried with Focus event but not successful:
You need to select all content in the textbox in GotFocus event. For MaskedTextBox control it handle the selection internally after the focus event fire. So we need to do BeginInvoke to call the SelectAll() afterward.
private void DateDay_GotFocus(object sender, RoutedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate() {
((TextBox)sender).SelectAll();
});
}
This way you can start typing directly.
You can't make the text null if null is not allowed.
WPF version:
private void TextBox_GotFocus(object sender, RoutedEventArgs e) {
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() {
((TextBox)sender).SelectAll();
});
}
Alternate solution for MaskedTextBox using the Enter Event
private void maskedEdit_Enter(object sender, EventArgs e)
{
MaskedTextBox maskedTextBox = (MaskedTextBox)sender;
maskedTextBox.BeginInvoke
(new Action
(() =>
{
maskedTextBox.SelectAll();
}
)
);
}
I have a textBox and I call an event get focus, when click on it. The behaviour is different when I make a double click on it, how can I make an event for getting focus for double click on this textbox?
You can use the OnTap() and OnDoubleTap() methods of the TextBox. And in each method you can define the different logic and set the focus on the TextBox.
Update:
Here's a simple code structure on how to make it work:
XAML:
<TextBox x:Name="InputTextBox" Margin="0,0,0,520" />
C#
public MainPage()
{
InitializeComponent();
InputTextBox.Tap += InputTextBoxTap;
InputTextBox.DoubleTap += InputTextBoxDoubleTap;
}
private void InputTextBoxDoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Double tapped!";
}
private void InputTextBoxTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Tapped!";
}
I tested this on both the emulator and on a device and it works in both cases!
The reason is simple.
If you have notice the arguments supplied are different
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void textBox1_Click(object sender, EventArgs e)
{
}
Yes if you want them to resemble the same you can select "MouseClick" event from the properties.
Cheers!
I'm converting a program from WinForms to WPF. There seems to be a lot of unnecessary syntax changes. But the one I'm having trouble with is saving the "checked" or "unchecked" status to Properties.Settings. In WinForms I used:
private void chkBackup_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Backup = chkBackup.Checked;
Properties.Settings.Default.Save();
}
There doesn't seem to be an Event for "CheckedChanged" in WPF, So I'm trying:
private void chkBackup_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = (chkBackup.IsChecked == true);
Properties.Settings.Default.Save();
}
private void chkBackup_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = (chkBackup.IsChecked == false);
Properties.Settings.Default.Save();
}
I get no errors with this, but when I uncheck the checkBox, the settings aren't changed. Please help. What am I doing wrong.
Thanks
You're using a different expression each time. In the checked event you're using chkBackup.IsChecked == true which evaluates to true if the box is checked and false otherwise.
In the unchecked event you're using chkBackup.IsChecked == false which evaluates to true if the box isn't checked and false otherwise.
What you're interested in is if the box is checked or not. The expression to use for this is chkBackup.IsChecked == true. Your current solution will always save true.
you are using the Settings object correctly (from the code you provided), so maybe you need to attach the Checked/Unchecked event handlers.... (MSDN here)
<!-- in your xaml -->
<CheckBox Checked="OnChecked" Unchecked="OnUnchecked"/>
//in your code-behind....
myCheckbox.OnChecked += myHandler;
Your code looks fine. Are you hooking up your handlers in XAML?
<CheckBox Name="chkbox"
Content="Some Checkbox"
Checked="chkBackup_Checked"
Unchecked="chkBackup_Unchecked" />
If you want it to be a little more streamlined, you could do something like this:
<CheckBox Name="chkbox"
Content="Some Checkbox"
Checked="chkBackup_CheckChanged"
Unchecked="chkBackup_CheckChanged" />
private void chkBackup_CheckChanged(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = chkBackup.IsChecked;
Properties.Settings.Default.Save();
}
I've found, that in WPF you can use strings to save a value. It's inconvenient, but it works. :/
private void chkBackup_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = "true";
Properties.Settings.Default.Save();
}
private void chkBackup_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = "false";
Properties.Settings.Default.Save();
}
and to check if it's checked, you can use
if (Properties.Settings.Default.Backup == "false")
{
//enter code here
}
I have a dropdown list and radio button. If something is selected from the dropdown by the user, I want the radio button cleared. If the radio button is selected I want the selection of the dropdown cleared. Unfortunately, this creates events that cancel each other out. I tried using the sender as shown below to determine if the value was being changed by code or by the user, but that doesn't work. How do I make these events only work if the user is the source of the action?
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender is RadioButton)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender is ComboBox)
{
// Display
rbBlank.IsChecked = false;
}
}
You won't be able to tell the difference between the two since the source will be the same instance for both occasions.
This doesn't answer the question directly but if you compare the SelectedIndex of comboBoxTitles in the SelectionChanged event handler, your problem should be solved
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBoxTitles.SelectedIndex != -1)
{
rbBlank.IsChecked = false;
}
}
Try to compare if sender == instance of a control instead of is type of.
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender == rbBlank)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender == comboBoxTitles)
{
// Display
rbBlank.IsChecked = false;
}
}
If you know the IDs of those controls, you can try something like this:
System.Web.UI.WebControls.WebControl webControl = (System.Web.UI.WebControls.WebControl) sender;
if( webControl.ID == <comboboxId>)
{
//Do something
}
I havent tried this, but I guess it might work.