call a method from another method in Code behind C# - c#

I need to call btn_submit_Click(object sender, EventArgs e) from another method protected void Timer1_Tick(object sender, EventArgs e) which normally calls by a button click.
Now here in Timer1_Tick compares a time and if current time exceeds, i need to call btn_submit_Click(object sender, EventArgs e) automatically.
protected void Timer1_Tick(object sender, EventArgs e)
{
DateTime et = DateTime.Parse(Session["endtime"].ToString());
if (DateTime.Now.TimeOfDay >= et.TimeOfDay)
{
// btn_submit_Click();
Response.Redirect("Welcome.aspx");
}
else
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
}
Please suggest me a way to do this.

Personally I would take a slightly different approach. You're not actually trying to say that a button was clicked - you're just interested in the same side effects as a button click. So extract a third method which only has the relevant parameters (there may not be any) and call that method from both btn_submit_Click and Timer1_Tick. That way you don't have to come up with a sender and EventArgs for a button click which didn't happen. So for example:
protected void btn_submit_Click(object sender, EventArgs e)
{
// Maybe validation?
Submit();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
DateTime et = DateTime.Parse(Session["endtime"].ToString());
if (DateTime.Now.TimeOfDay >= et.TimeOfDay)
{
Submit();
Response.Redirect("Welcome.aspx");
}
else
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
}
private void Submit()
{
// Common code to execute on either the timer tick or button click
}

Jon Skeet mentioned the right approach. Refactor the code in your btn_submit_click into a central method that can be called by both Button and Timer. But you can still do submit_click(sender, e)
protected void Timer1_Tick(object sender, EventArgs e)
{
....
btn_submit_Click(sender, e);
...
}

Related

Raise an Event Except when something happpen

Scenario: Only If the user follow the path Click on ListView > Click on Button the Button1 do something.
In other word I want to check in Button1_Click(object sender, EventArgs e) if the previous focus was on ListView.
So I tried this:
private void ListView_Test_Leave(object sender, EventArgs e)
{
_focusedControl = null;
}
I want raise previous event except when this event is raised:
private void Button1_Click(object sender, EventArgs e)
{
if(_focusedControl == listView_Test)
{
// ...
}
}
Edit: I have a variable that holds a reference to the currently focused control:
private Control _focusedControl;
and I update it in this way:
private void ListView_Test_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
If the user follow the path Click on ListView > Click on Button I want raise only the Button1_Click event, in all other case I want normal raise.
You could use a helper variable.
bool wasRaised=false;
private void Button1_Click(object sender, EventArgs e) { wasRaised=true;}
Then you can check that variable in your event, and only run if it is false.

Regroup button events in one event

I use 6 different buttons doing practically the same thing.
private void VisaCreaDoc_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.CREADOC);
}
private void VisaTravaux_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.TRAVAUX);
}
private void VisaRemiseOuvrageIR_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.REMISEOUVRIR);
}
private void VisaRemiseOuvrageExpl_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.REMISEOUVREXPL);
}
private void VisaMES_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.MISEENSERVICE);
}
private void VisaEncodageArchivage_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.ENCODAGEARCHIVAGE);
As you can see, they're using a function from the ViewModel with a different parameter.
Is there any way to regroup the 6 button events to have only one and kind of pass the parameter directly in the XAML call or something similar to avoid having the "code duplication" ?
Not sure if you like it "better" but you can check which button was clicked inside the handler:
void HandleButton_Click(object sender, RoutetEventArgs e)
{
if (sender is Button b)
{
if (b == VisaCreaDoc) # VisaCreaDoc is the given name of your button instace in xaml
ViewModel.ValidateItem(InfosPosteViewModel.CREADOC);
else if (b == VisaTravaux)
ViewModel.ValidateItem(InfosPosteViewModel.TRAVAUX);
else if (...) // etc.
}
}
You can spice it up with a switch pattern matching to get rid of the if / else if / else if / ... chains.
Maybe you can avoid some duplicate code by creating a command for the ViewModel function (see Introduction to WPF Commands). As far as I know you can also define a CommandParameter in XAML.

Why all dropdownlist_SelectedIndexChanged events trigger only after button click?

I have a simple ASP.NET Application. And On one of my pages all dropdownlist_SelectedIndexChanged events trigger only after I click save button.
This is part of my code behidn:
protected void ddlTimekeeperOffice_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
protected void ddlTkprDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
protected void ddlTkprSummaryTitle_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
this is 3 dropdowns
protected void btnSave_Click(object sender, EventArgs e)
{
trTimekeeperRow.Visible = true;
}
and a save button.
When I select anything in dropdowns > Nothing happens
Then I click save button and before save_click it goes through every ddl_selectedindexchanged.
Why?
Make sure AutoPostBack property of the dropdown controls is true.

How to perform Label click in C#?

I have a label working as a button. I would like when I press a button the click event to this label to take action. for example
private void Label1_Click(object sender, EventArgs e)
{
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Now I want when I press this button, the label1 click event to be performed
private void button1_Click(object sender, EventArgs e)
{
// I want when I press this button something like this happens
Label1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e);
}
now if you want to show a message of which control was clicked all in one method do the following
private void label1_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
var name = control.Name;
MessageBox.Show(string.Format("I pressed this {0} and showed me this messagebox",name));
}
Two ways to do this.
First:
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e); // Just call the Label's click handler
}
Second:
// Bind the Label1_Click handler to the button1 Click event, as they both use the same delegate
button1.Click += new EventHandler(Label1_Click);
With the second approach, note that in C# delegates are multi-cast, so both the button1_Click handler and the Label1_Click handler will be called when the button is clicked, in the order they were bound.
private void button1_Click(object sender, EventArgs e)
{
//What the label click do:
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Is that not easier?
Why do you want to do it ?
I think it would be easier for you to just include the lable click functionality with the button click. Maybe even separate each piece in their own method and call them from the button click. Here is how you'd call another click event.
private void button1_Click(object sender, EventArgs e)
{
label1_Click(sender, e);
}
public class MyLabel:Label
{
public void PerformClick()
{
OnClick(new EventArgs());//InvokeOnClick(this,new EventArgs());
}
}

SplitterLocation doesn't save in the settings

i have created a splitterLocation setting, Type system.Drawing.Point and Scope : User. to save the splitter location when the user change it. and load again with the new location when the form load.
private void splitter1_LocationChanged(object sender, EventArgs e)
{
MailSystem.Properties.Settings.Default.splitterLocation = splitter1.Location;
MailSystem.Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
splitter1.Location = MailSystem.Properties.Settings.Default.splitterLocation;
}
but it doesnt work i dont know why ?.
Try moving the saving code to the FormClosing event.
private void Form1_FormClosing(object sender, EventArgs e)
{
MailSystem.Properties.Settings.Default.splitterLocation = splitter1.Location;
MailSystem.Properties.Settings.Default.Save();
}
and make sure your splitter1 control isn't being docked, DockStyle.None.

Categories