How to Call a Button Click Event from Another Method

How to call a button click event from another method

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

How to call a button click event when i click another button in c#

I'd suggest removing the logic from behind the click events into separate methods.

private void MethodOne()
{
progressBar1.Value = 0;
String[] a = textBox7.Text.Split('#');
progressBar1.Maximum = a.Length;
for (var i = 0; i <= a.GetUpperBound(0); i++)
{
ansh.Close();
progressBar1.Value++;
}
}

private void MethodTwo()
{
foreach (string item in listBox2.Items)
textBox7.Text += item.Contains("@") ? string.Format("{0}#", item.Split('@')[0]) : string.Empty;
}

private void button1_Click(object sender, EventArgs e)
{
MethodTwo();
MethodOne();
}

private void button2_Click(object sender, EventArgs e)
{
MethodTwo();
}

In my experience, it's easier to maintain and test this way. Having different controls' events all calling each other makes it tougher to follow the logic.

How to call a method from another class in a button click event?

Your two choices are based on whether or not you want a new instance of ListDisplay when you click the button or not.

Option 1:

namespace Project
{
public partial class AddTask : Form
{
private void btnSubmit_Click(object sender, EventArgs e)
{
var ld = new ListDisplay();
ld.Show();
ld.LoadingListBox();
}
}
}

Option 2:

namespace Project
{
public partial class AddTask : Form
{
private ListDisplay _ld;
public AddTask(ListDisplay ld)
{
_ld = ld;
}

private void btnSubmit_Click(object sender, EventArgs e)
{
_ld.LoadingListBox();
}
}
}

Button click event with two methods which execute one after another by timer

Use an async Click event handler with await Task.Delay:

// class member
private TimeSpan clickActionDelay = TimeSpan.FromSeconds(3);

private async void Button_Click(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
button.IsEnabled = false;

firstAction();

// wait 3 seconds without blocking the UI thread
await Task.Delay(clickActionDelay);

secondAction();

button.IsEnabled = true;
}

private void firstAction()
{
...
}

private void secondAction()
{
...
}

calling button click function from another class

as stuartd said you can add a call to your parent Form:

public partial class InfoSettings : UserControl
{
public InfoSettings()
{
InitializeComponent();
}

private void settingBtn_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1) this.Parent;
form1.buttonHandler(sender);
}
}

Calling a button click event in another function iOS objective-c

your button action has a parameter , so you need to append in the parameter as nil or self.do like

- (void)checkStatus{
if(somecondition){
[self save:nil];
}
}

calling another function after finishing button click function in jQuery

Here is one way to do it:
You define an action for the button click. In my example I created a promise with a setTimeout() call. This could be an Ajax call or anything else (not necessarily returning a promise). As soon as the action is done or the promise fulfilled, func2 will step into action.

$("#btn-click").click(func1);

function func1(){
console.log("func1 started...");
$.when(new Promise(res=>
setTimeout(res,1000)))
.then(()=>{
console.log("func1 done.");
func2()});
}

function func2() {
console.log("func2");
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<button id="btn-click">click</button>

Cannot raise button click event of another form

The problem with your current approach is that code STOPS in the current form when ShowDialog() is run.

One solution would be to create an instance of Form2 and wire up the Shown() event, which gets run AFTER the form is displayed.

From there, you can click the button using the instance of the form you created:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f2 As New Form2
AddHandler f2.Shown, Sub()
f2.Button2.PerformClick()
End Sub
f2.ShowDialog()
End Sub


Related Topics



Leave a reply



Submit