I have a user control with some buttons (tmNewItem, tmEdit, tmInsert)
I write a clickButton event for them.
for example:
public void btnEdit_Click(object sender, EventArgs e)
{
btnNew.Enabled = false;
btnEdit.Enabled = false;
}
I used this user control in another project and write another method for the buttons and assign it to the usr control:
public void DTedit(object sender, EventArgs e)
{
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
and now, when I run the project and press btnEdit button, the first time, btnEdit_Click will execute and after that DTedit. can i change it? I mean the first time DTedit (that I define it in my project) run, and after it btnEdit_Click (that I define it in the user control) run?
how can I do that?
Try this
public void DTedit(object sender, EventArgs e)
{
//Place your code here
DT_Navigator.btnCancel.Click -= new EventHandler(DTedit); //This will remove handler from the button click and it will not be executed next time.
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
Suggested Code
//User control
public event CancelEventHandler BeginEdit;
public event EventHandler EndEdit;
private btnYourButton_Click(object sender, EventArgs e)
{
CancelEventArgs e = new CancelEventArgs();
e.Cancel = false;
if (BeginEdit != null)
BeginEdit(this, e);
if (e.Cancel == false)
{
if (EndEdit != null)
EndEdit(this, new EventArgs);
//You can place your code here to disable controls
}
}
Related
I have an entry_TextChanged event handler and a button.
How can I say if this button clicked go to that entry_TextChanged handler again?
private bool save = false;
private void entry_TextChanged(object sender, TextChangedEventArgs e)
{
if (save == true)
{
// Do Something
}
}
private void goto_Clicked(object sender, EventArgs e)
{
save = true;
// GO and do the entry texchanged??
}
you can just call it the same as any other function entry_TextChanged(null, null) or any other values that you want to send to the function
private void goto_Clicked(object sender, EventArgs e)
{
save = true;
entry_TextChanged(null, null)
}
I'm trying to implement a customized exit prompt in my WinForms. (I should not be using DialogBox)
I have a User Control Object placed in my main form that is invisible and disabled by default. Clicking in a certain button I have placed on the form shows and enables the object, disabling everything in my form except the User Control.
private void btn_close_Click(object sender, EventArgs e) {
prompt1.Visible = true;
prompt1.Enabled = true;
disableControls();
//Wait for a button to be pressed in prompt1
//Make an action based on a button pressed.
//closeApp returns a boolean
if (!prompt1.closeApp)
{
prompt1.Visible = false;
prompt1.Enabled = false;
enableControls();
}
else
{
Application.Exit();
}
}
Here's my code at the prompt object:
public partial class Prompt : UserControl
{
bool exit;
public bool closeApp
{
get{return exit;}
}
public Prompt()
{
InitializeComponent();
}
private void btn_yes_Click(object sender, EventArgs e)
{
exit = true;
}
private void btn_no_Click(object sender, EventArgs e)
{
exit = false;
this.Hide();
}
}
What I want to do is wait for a button to be pressed in my prompt object before proceeding to the next line in the btn_close_Click().
What should I do? Is there a better way to implement this?
Add events to your usercontrol then handle those events on your main form.
In your usercontrol:
public event EventHandler<EventArgs> ExitCancelled;
public event EventHandler<EventArgs> ExitApplication;
private void btn_yes_Click(object sender, EventArgs e)
{
ExitApplication?.Invoke(this, EventArgs.Empty);
}
private void btn_no_Click(object sender, EventArgs e)
{
ExitCancelled?.Invoke(this, EventArgs.Empty);
}
Handle the events on your form:
public void prompt1_ExitApplication(object sender, EventArgs e)
{
Application.Exit();
}
public void prompt1_ExitCancelled(object sender, EventArgs e)
{
prompt1.Hide();
enablecontrols();
}
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
}
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)
}
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);
}
}