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);
}
Related
I need a way to pass user control to user controls.
I am using Windows Forms.
For example. Say I have a radiobutton in User control 1 and I want User Control 2 to call and see if that radiobutton is checked on User control 1. How would I reference that?
And for some sample code:
This is UserControl1
public void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
This is UserControl2
private void button4_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
//do something
else
//do something
}
Seems like you have two unrelated user controls on your form. And UserControl2 (UC2) should change it's behavior is something happens on UserControl1 (UC1). That means you should have an event on UC1 which will fire if radiobutton1 checked state changes. You also will need to expose radiobutton status. You can do it either with custom EventArgs or with public property:
UserControl1
public event EventHandler SomethingChanged;
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (SomethingChanged != null)
SomethingChanged(this, EventArgs.Empty);
}
public bool IsSomethingEnabled => radioButton1.Checked;
UC2 should allow changing it's behavior. That can be done with public property
UserControl2
public bool UseCoolFeature { get; set; }
private void button4_Click(object sender, EventArgs e)
{
if (UseCoolFeature)
//do something
else
//do something else
}
And last step - coordinator which will manage both usercontrols. It's your form. Subscribe to event from UC1 and change state of UC2:
Form
private void userControl1_SomethinChanged(object sender, EventArgs e)
{
userControl2.UseCoolFeature = ((UserControl1)sender).IsSomethingEnabled;
}
You can even use in-place event handler:
userControl1.SomethingChanged += (s,e) =>
userControl2.UseCoolFeature = userControl1.IsSomethingEnabled;
You can store the value in your session:
UserControl1
public void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Session["radioButton1Checked"] = radioButton1.Checked;
}
UserControl2
private void button4_Click(object sender, EventArgs e)
{
if (Session["radioButton1Checked"] != null && (bool)Session["radioButton1Checked"])
//do something
else
//do something
}
private void SuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if(args.QueryText!=null)
{
...
}
}
Pivot_SelectionChanged
private void privotContent_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SuggestBox.QuerySubmitted += SuggestBox_QuerySubmitted;
}
don't call SuggestBox.QuerySumitted in privotContent. But I want to do that.
I think you want to fire the QuerySubmitted event of a AutoSuggestBox inside of the SelectionChanged event of a Pivot control. You can for example code like this:
private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var ee = new AutoSuggestBoxQuerySubmittedEventArgs();
AutoSuggestBox_QuerySubmitted(autoSuggestBox, ee);
}
private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if (args.QueryText != null)
{
}
}
the autoSuggestBox in this code is the Name of the AutoSuggestBox control.
I'm working on a WinForms app. My ComboBox has DropDownClosed event, but I need to fire this event from a Button. How can I do this?
Like this:
private void button1_Click(object sender, EventArgs e)
{
comboBox1_DropDownClosed(sender, e);
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
Please take a look , I believe this is what you are talking about
private void abc_Click(object sender, RoutedEventArgs args)
{
}
private void xyz_Click(object sender, RoutedEventArgs args)
{
abc_Click(sender, args);
}
In my case i have 8 checkbox, each checkbox has click event, if checkbox is clicked, that will call same method like this.
public void _setting(CheckBox check)
{
...
}
And this is the code how click event call the method
private void check1_Click(object sender, RoutedEventArgs e)
{
_setting(check1);
}
How to simplify the click event to call method? Or i have to call like in check1_click until check8?
private void check2_Click(object sender, RoutedEventArgs e)
{
_setting(check2);
}
private void check3_Click(object sender, RoutedEventArgs e)
{
_setting(check3);
}
...
private void check8_Click(object sender, RoutedEventArgs e)
{
_setting(check8);
}
or can it be simplified?
I think so. It seems that check1..check8 are the controls being clicked. In that case, you could assign all events to a single event handler and use the sender:
private void check_Click(object sender, RoutedEventArgs e)
{
_setting((CheckBox)sender);
}
If sender is not the control you expect, possibly Source or e.OriginalSource is.
private void check1_Click(object sender, RoutedEventArgs e)
{
_setting(sender as CheckBox);
}
This way if you call _setting and sender is not a checkbox, it will be null, not Exception.
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)
}