How to check if button from user control was clicked? - c#

It seems like a straight forward thing, but I can't figure it out.
How can I check if a certain button was clicked from user control?
my user control is uc_test and button name is btnTest .
I assume its some sort of event handler added to uc_test.btnTest ?
I'm working in WinForms.

It sounds like you've created a button but want to make it do something on click. I don't often use Winforms but from memory double-clicking on the control in the form should automatically create a btnTest_Click method. If not, just go to code and put in a method like:
protected void btnTest_Click(object sender, Eventargs e)
{
//do something
}
Then set in the properties of the button the OnClick event to btnTest_Click.

Related

Onclick event for textbox in csharp form application

I'm creating form application on c# . I have dragged a textbox with some text in it.
private void textBox1_Click(object sender, EventArgs e)
{
}
Now what is the event for the onlick on textBox1 ?
I need to add this on that function textBox1.Clear();
P.S I searched everywhere. But all i can find is jquery and javascripts... No c#.
EDIT
I tried onfocus like below..but its not working
private void textBox1_OnFocus(object sender, EventArgs e)
{
MessageBox.Show("dsd");
}
If you want to do something when the control is clicked the handle the Click event, not the TextChanged event. Presumably you just double-clicked the control in the designer. That will only handle the default event. To handle other events, open the Properties window, click the Events button at the top and then double-click the appropriate event.
That said, is Click really appropriate? What if the user enters the TextBox using the Tab key? If what you actually want to do is act when the control gets focus then you should handle the Enter event.
You can handle OnFocus/GotGocus event in the TextBox, and clear the text in the textbox.
Hope this helps.

how to execute code when the tab index changes?

I currently have a tabcontrol on my windows form in visual studio, i would like it so that when the user clicks on a different tab that i can execute some code (for example populate a listbox).
when i double click on the tab it only brings up an onclick event for the body of the tabcontrol.
i was thinking that i may have to create a thread in the form load that will constantly check whether the tab index changes and if it does then execute some code. but surely there must be an easier way?
You can use TabControl.Selecting or TabControl.SelectedIndexChanged event
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
//Your code goes here.
}
You should look at TabControl events
http://msdn.microsoft.com/en-ie/library/system.windows.forms.tabcontrol.selecting.aspx

Creating an event for a textbox and AJAX Update Panel Control

I wanted to create a "Click" event for a textbox in C# (as there isn't any).
So, this way
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt1OnClick")
{
txt1_Click();
}
txt1.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt1, "txt1OnClick"));
}
private void txt1_Click()
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
Then I wanted to load the image without reloading the page.
So I used the AJAX UpdatePanel Control and this worked fine with
protected void Button1_Click(object sender, EventArgs e)
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
But not with the event I created, because the compiler doesn't identify my new events as
a real event or something I couldn't figure out.
I added the button1_click event according to Step 8 of "Refreshing an UpdatePanel Control with an External Button".
The click event of textbox is not shown in this option:
So my question is is there any way to add this event within System.Web.UI.WebControls.TextBox class or, to make this event visible within the above option?
So that I can include click event of the textbox within the Triggers of the update panel.
If you try to create a Click event for a TextBox, every time a user clicks your textbox you'll trigger a postback to the server (even to evaluate if you need to do something as part of handling the event). This is very inefficient - you should handle clicks in the browser, using JavaScript and then trigger the UpdatePanel using client-side logic.
This lets you trigger a call to the server if you need it but avoid it when you don't. If you have a server-side event handler, your code will post back to the server (reloading the page) every time the user clicks the TextBox.
You can read this link (and others) about using __doPostBack() on the client side to trigger an UpdatePanel to perform a postback.
http://encosia.com/easily-refresh-an-updatepanel-using-javascript/

C#/J# Find function by name / Manual control click

I have a control which is a button and I want to manually click it. (When the user presses a button)
I have the control but I don't know how to get it's function. (The format of the click function is [Control.Name]_Click(object sender, EventArgs e) as usual
Does anyone know how to do this?
If you have the button, instead of trying to find the method, I would recommend just calling Button.PerformClick instead. This will trigger all of the event handlers on the button's Click event, no matter what name they've been assigned.

How can I give focus to a textBox after a Tab has been selected?

I'd like give focus to a textBox after a Tab has been selected but no matter what I try it doesn't work. I've looked at similar questions here but they don't get me the results I need. Here is what Ive tried.
private void tabBDERip_Click(object sender, EventArgs e)
{
textBoxPassword.Focus();
}
and
private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabAll.SelectedTab == tabBDERip)
{
textBoxPassword.Focus();
}
}
Can someone please tell me what I'm doing wrong?
Thanks
First thing the Click event of the TabPage control fires when the user clicks inside the TabPage not on the header so your SelectedIndexChanged event is the one you want to use.
I just tested code very similiar to yours:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabPage2)
{
textBox4.Focus();
}
}
And it worked fine.
Is the password textbox not enabled or something like that?
If you try to call Focus() on a different control does that also not work?
If you set a breakpoint inside the SelectedIndexChanged code does it get hit?
Update: Interesting. If the breakpoint isn't getting hit (before the if) I would double check that your eventhandler is properly attached. Look in your designer.cs for something like:
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
Update: I put my working example at http://www.ccswe.com/temp/SO_TextBoxFocus.zip maybe looking at it will help you figure out where the issue is.
Update: The easier way to attach an event handler to a control on your form:
1: Select the Control to want to attach an event handler to and then click the Events icon (lightning bolt) in the Properties window.
alt text http://www.ccswe.com/temp/Attach_EventHandler_1.png
2: Find the event you want to attach to and double click to the right.
alt text http://www.ccswe.com/temp/Attach_EventHandler_2.png
3: A code stub will be automatically generated for you and the event will be attached in the designer.
alt text http://www.ccswe.com/temp/Attach_EventHandler_3.png
If you look at the properties window again you'll now see the name of the method that was generated.

Categories

Resources