How to catch a event of AutoSuggestBox in Pivot_SelectionChanged? - c#

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.

Related

RoutedEvents Combobox not Firing

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);
}

How to Fire DropDownClosed Event from a Button

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);
}

EventHandler for all controls in form

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 can we return the name of the button on which the user clicks in a webBrowser control?

I thought we should raise an event
This is what I have found:
event OnButtonClicked ()EventArgs;
HTMLButtonClickEventArgs:EventArgs
{
String ButtonName;
}
I am doing a web Browser control and this is the code I wrote for its button clicked,but I want to know on which button user clicks:
public delegate void ButtonPressedEventHandler(object sender, EventArgs e);
public event ButtonPressedEventHandler ButtonPressed;
void OnButtonPressed()
{
if (ButtonPressed != null)
ButtonPressed(this, new EventArgs());
}
I write an example for you:try this:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<html><body><button id=\"btn1\" type=\"button\">Click Me!</button><button id=\"btn2\" type=\"button\">Click Me!</button></body></html>";
}
Call Click event:
//Edited
bool First_Call = true;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (First_Call)
{
webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);
First_Call = false;
}
}
Get Active Element When User click on document but
void Document_Click(object sender, HtmlElementEventArgs e)
{
// **Edited**
//Check Element is Button
if (webBrowser1.Document.ActiveElement.TagName == "BUTTON")
{
MessageBox.Show(webBrowser1.Document.ActiveElement.Id);
}
}

How to call a button click event from another method

How can I call SubGraphButton_Click(object sender, RoutedEventArgs args) from another method?
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
// call SubGraphButton-Click().
}
You can easily do it by the following piece of code (assuming that name of your button is btnButton):
btnButton.PerformClick();
You can call the button_click event by simply passing the arguments to it:
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click(sender, args);
}
you can call the button_click event by passing..
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click(sender, args);
}
Also without passing..
private void SubGraphButton_Click(object sender, EventArgs args)
{
}
private void Some_Method() //this method is called
{
SubGraphButton_Click(new object(), new EventArgs());
}
You can perform different approaches to work around this. The best approach is, if your both buttons are suppose to do the same job, you can define a third function to do the job. for example :
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
myJob()
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
myJob()
}
private void myJob()
{
// Your code here
}
but if you are still persisting on doing it in your way, the best action is :
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click.PerformClick();
}
In WPF, you can easily do it in this way:
this.button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Usually the better way is to trigger an event (click) instead of calling the method directly.
private void PictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Click Succes");
}
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
PictureBox1_Click(sender, e); //or try this one "this.PictureBox1_Click(sender, AcceptButton);"
}
}
You can simply call it:
SubGraphButton_Click(sender, args);
Now, if your SubGraphButton_Click does something with the args, you might be in trouble, but usually you don't do anything with them.
For me this worked in WPF
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
RoutedEventArgs routedEventArgs = new RoutedEventArgs(ButtonBase.ClickEvent, Button_OK);
Button_OK.RaiseEvent(routedEventArgs);
}
}
Use InvokeOnClick event. it works even if the button is invisible/disabled
A simple way to call it from anywhere is just use "null" and "RoutedEventArgs.Empty", like this:
SubGraphButton_Click(null, RoutedEventArgs.Empty);
For WPF:
YourButtonName.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));
Add it to the instance of the Click delegate:
ChildNode.Click += SubGraphButton_Click
which is inkeeping with the pattern .NET events follow (Observer).
For people wondering, this also works for button click.
For example:
private void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Test")
}
private void txb_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
btn_Click(sender, e);
}
When pressing Enter in the textfield(txb) in this case it will click the button which will active the MessageBox.
we have 2 form in this project.
in main form change
private void button1_Click(object sender, EventArgs e)
{
// work
}
to
public void button1_Click(object sender, EventArgs e)
{
// work
}
and in other form, when we need above function
private void button2_Click(object sender, EventArgs e)
{
main_page() obj = new main_page();
obj.button2_Click(sender, e);
}

Categories