C# - Switch button event dynamically - c#

Hy there,
i'm trying to change the button event when the code it's executing. So this is my code:
// I will call this function in many places of my code and send a callback function.
// For example, when i click in the confirm button, the event that will be called it's the Callback()
void ShowModal<T>(string Title, IList<T> Data, Func<object, EventArgs, int> Callback)
{
this.lblTitle.Text = Title;
this.gvResults.DataSource = Data;
this.gvResults.DataBind();
// Executes the sended function when click
this.lkbConfirm.Click += new EventHandler(
(object sender, EventArgs e)
=> Callback(sender, e)
);
this.ModalBusca.Show();
}
// I call the ShowModal function
this.ShowModal("Search", data, CallbackSelect);
// This function will be execute
int CallbackSelect(object sender, EventArgs e)
{
// my code to execute
return 0;
}
But the CallbackSelect function is not called.
I'm using Asp.NET 4.7 web application, thanks!

Related

onClick Function for created button with parameters

I've created a button and now I need to create an onClick function for it.
I've found this sollution:
btnEasyLvl.Click += new EventHandler(Game);
...
protected void Game(object sender, EventArgs e)
{
//some actions
}
But what if my Game function has to accept some parameters (e.g. n,m,k). So that I'll need to write:
btnEasyLvl.Click += new EventHandler(Game(n,m,k));
How to rewrite it?
You can not pass parameters to Game because it is an event, and those have the predefined parameters object sender and Eventargs e, what you must do is create a method that receives these parameters and call it from the event
btnEasyLvl.Click += new EventHandler(Game);
protected void Game(object sender, EventArgs e)
{
//Here call the method SomeActions
SomeActions(n, m ,k);
}
private void SomeActions(n, m, k)
{
//some actions
}

Raise control event programmatically in Xamarin.Forms

I have a button defined in XAML:
<Button x:Name="loginButton" Text="Login" Clicked="OnLoginButtonClicked" />
Is there a possibility to raise the Clicked event programmatically? Of course I could call OnLoginButtonClicked, but I'm interested how the Clicked event itself can be raised.
If you just want to call the Clicked action, you can do this trick:
var b = new Button();
b.Clicked += (x, y) =>
{
//Your method here
};
var t = b as IButtonController;
t.SendClicked(); //This will call the action
It is important to note this is not the right one.
As it was mentioned before, calling the actual method is preferred.
You can call DoSomething by event handler or any other place in your code
void OnLoginButtonClicked(object sender, EventArgs e)
{
DoSomething ();
}
private void DoSomething()
{
//Click code here.
}
Assing a delegate method
testButton3.TouchUpInside += HandleTouchUpInside;
Add the method
void HandleTouchUpInside (object sender, EventArgs ea)
{
new UIAlertView("Touch3", "TouchUpInside handled", null, "OK", null).Show();
}

how to check whether a button is clicked, inside another button click event in windows form application in C#

well i am having two buttons on a form and I want to start data transfer with the first button and stop on the press of a second button.
code is:
private void stdaq_Click(object sender, EventArgs e)
{
stopped = false;
//while (stopped == false)
if (sender == spdaq)
{
stopped = true;
///break;
Process();
}
else if (sender == stdaq)
{
Process();
}
}
here stdaq is the start button and spdaq is the stop button, the process function is a function which i am implementing and in that with the stopped variable of bool type i am implementing two different functions inside process method, but i want to continually check whether the stop button is pressed or not but here with this code i got no success.
so please help me with how to pass the value true to the stopped variable inside the event click function of start button itself on the press of stop button.
Create cancellation token, start asynchronous Task in button start event handler put your method in this Task, pass reference to this cancellation token and use
it to stop this task in Stop button event handler when you'll need it later.
More information : https://msdn.microsoft.com/en-us/library/jj155759.aspx
Example of how you can use it:
static CancellationTokenSource cts;
static Task t;
private void Method()
{
while (!cts.IsCancellationRequested)
{
// your logic here
}
t = null;
}
private void stdaq_click (object sender, EventArgs e)
{
if(t != null) return;
cts = new CancellationTokenSource();
t = new Task(Method, cts.Token, TaskCreationOptions.None);
t.Start();
}
private void spdaq_Click(object sender, EventArgs e)
{
if(t != null) cts.Cancel();
}
Use two separate Handlers for the start and the stop button. This makes your logic much simpler to follow. Then do soemthing like this:
private void stdaq_Click(object sender, EventArgs e) // Start
{
Process(true);
}
private void spdaq_Click(object sender, EventArgs e) // Stop
{
Process(false);
}
Or even better: Create two seperate Methods StartProcess() and StopProcess().

how to generate a SelectionRangeChanged Event Programatically ChartControl WinForms

want to create a selectionRangeChanged event programatically not really getting how to do it
private void btn_10D_Click(object sender, EventArgs e)
{
double varRange = 10;
double var_Sel1 = DatesX[0].ToOADate();
Chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
Chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionColor = Color.LightGray;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionStart = var_Sel1;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionEnd = varRange + var_Sel1;
Chart1.ChartAreas["ChartArea1"].CursorX.Position = varRange + var_Sel1;
Chart1.SelectionRangeChanged += new EventHandler<CursorEventArgs>(Chart1_SelectionRangeChanged);
}
void Chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
throw new NotImplementedException();
}
thank you
For all events in C# is true that if class creator did not make extra effort to allow event firing form outside of class it is impossible to fire them.
According to MSDN
Chart.SelectionRangeChanged event Occurs when the selection start position or end position is changed.
But from my tests I can see that it is fired only if it is changed by user not program.
If I understand your intention correctly you want to handle those small buttons under your chart and btn_10D_Click method is a click handler for one of them. Try to move this line
Chart1.SelectionRangeChanged += new EventHandler<CursorEventArgs>(Chart1_SelectionRangeChanged);
to your constructor and ensure it is called once (remove it form other handlers). This will ensure your code is executed when user changes selection. If you want to execute same code for your button you should simply extract handler contents to method and call it form button click handler.
void Chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
DoSomething(/*some arguments if you need them*/);
}
private void btn_10D_Click(object sender, EventArgs e)
{
\\your code
DoSomething();
}

Some events dont work in Winform in C#

I am using YoutubeExtractor's dll.. videoDownloader_ProgressChanged and videoDownloader_DownloadFinished events are working in console application but in winform, it doesnt work.. I dont understand why..
private void btnStart_Click(object sender, EventArgs e)
{
string link = textBox1.Text;
start(link);
}
static void start(string link)
{
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
DownloadVideo(videoInfos);
}
private static void DownloadVideo(IEnumerable<VideoInfo> videoInfos)
{
VideoInfo video = videoInfos
.First(info => info.VideoFormat == VideoFormat.Standard360);
var videoDownloader = new VideoDownloader(video, Path.Combine("C:/Downloads", video.Title + video.VideoExtension));
videoDownloader.DownloadFinished += new EventHandler(videoDownloader_DownloadFinished);
videoDownloader.ProgressChanged += new EventHandler<ProgressEventArgs>(videoDownloader_ProgressChanged);
videoDownloader.Execute();
}
static void videoDownloader_ProgressChanged(object sender, ProgressEventArgs e)
{
//some code..
}
static void videoDownloader_DownloadFinished(object sender, EventArgs e)
{
//some code..
}
my second question is, I want to access a form control in a static videoDownloader_ProgressChanged event. e.ProgressPercentage paramter gives me percent of video downloaded. I want to show it in label. But I cant access label because of static event.. I tried to use delegate but nothing changed..
Please modify both Start() and DownloadVideo() routines to instance methods. Remove 'static' keyword from them and event handlers as well.
Thread off 'videoDownloader.Execute()' and BeginInvoke() in the changed/finished handlers.
Don't call methods that take forever, (in computer terms), in GUI event handlers. If it takes more than about 50ms, thread it off. Any net thingy, eg. something with 'YouTube' in it, will take longer than that just to establish a connection!

Categories

Resources