Textbox_Enter event not does nothing - c#

I want to be able to tell when the user cliked on a textbox and is in "edit" mode. Everywhere I look I see the textbox_Enter and textbox_Leave events being used with the instructions within that and it works fine. For me however it doesn't do anything. I tried elimination as many outside factors as possible, including creating a brand new project just for testing purposes and copied some code samples yet again nothing happens when I click on the textbox. I'm using Visual Studio 2017 on Windows 10 with Visual C# Application Windows Form (.NET Framework)
Also here's a sample of the code I try to use if it helps for whatever reason
private void textbox_Enter(object sender, ControlEventArgs e)
{
label.Text = "ok";
}

First of all the type of the e parameter is not correct: It must be EventArgs, not ControlEventArgs:
private void textbox_Enter(object sender, EventArgs e)
{
// Do something
}
Second, you need to register the event in the forms designer with the textbox control in the properties window:

You need to wire up this method to the textbox enter event. Select the control and then look at the events section in the properties tab.

Related

C# Winforms Event is adding _1 at the end of everything

My project became corrupt today and since fixing i have noticed that at the end of events like button click its adding _1 at the end of everything
for example
private void createNewUserToolStripMenuItem_Click_1(object sender, EventArgs e)
{
}
where as it should obviously be
private void createNewUserToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I have no idea what has happened but its causing me to repeat a lot of work. I have not duplicated controls or anything like that so i am very confused
Does anyone have any ideas? Please this is over 2 years of work and its looking like its knackered
Thanks to Idle_Mind this worked -
The designer lost the fact that the control was wired up and when you double click it it sees that there is already a method with that name so it adds one. All you need to do is select that control, go to the Events listing in the Properties pane, find that event, and then change the listed handler using the dropdown back to the original method. –

How do I add a Form Load event using the form designer in C# 2015? (like double clicking)?

I am new to C# in Visual Studio 2015. I just realized that the old classical way of adding event handlers by double-clicking on the item no longer works for some items like the form (or the Window).
After some Google searches I still can't figure out a way to add the Load event of the form using the designer.
Do I have to manually write code for that unlike in Visual Basic in Visual Studio 2005?
You can set the Load event of a Control from its properties window here. You create the
private void Form1_Load(object sender, EventArgs e)
{
// my code
}
event in your form class, and fill in its name (Form1_Load) where the arrow points in the picture.
Doing it manually would be something like:
Form1.Load += new System.EventHandler(this.Form1_Load);
considering that you created the Form1_Load event.
But on top of all that, double clicking should work.
You may have an issue with your instillation. However, select the form and go to the actions window and select load. This should give the desired result

C# multiple button with similar behavior

I am new to C# and I am following this C# tutorial at the moment. In this tutorial I came across the exercise to develop a calculator.
A C# .Net Calculator - Design Stage
In the solution given in the exercise, each digit button was given a btn*_click method which can be generalized pretty easily.
(source: homeandlearn.co.uk)
How can we write the code, so that we can generalize these 10 functions? I though it can be done by modifying initializeComponent(), but comment about it says it should not be modified using code editor.
How can this problem be tackled.
You can tie all buttons to the same click event handler, and use sender to get the text:
private void btnAnyButton_Click(object sender, EventArgs e)
{
Button theButton = sender as Button;
txtDisplay.Text += theButton.Text;
}
The tutorial you posted is using the visual editor in visual studios. By default the designer will generate code with the convention {controlname}_{eventname} you can explicitly assign a different event name in the properties window, and all the buttons could share the same event method.
And then it looks like you could refactor this like:
private void btn_click(object sender, EventArgs e)
{
Button btn = sender as Button;
if(btn != null)
txtDisplay.Text += btn.Text;
}
I hope that helps.
You could generate these buttons dynamically from the code and assign them some value in the tag attribute. From there, you can hook them all up to the same event handler (we're talking about the number buttons, as in 0,1,2,3,4...). In the onClick event handler you would get the tag value of the caller and do what you have to do.
Pseudocode:
void onClick(Button caller){
int btnNb = (int) caller.Tag;
//do what you have to do
}
The tag attribute is not necessary but I find it cleaner than getting the button text and converting to an int.

TextChanged event doesn't work.

I have a textbox in Form and i want to detect when the text has changed but the code I have found is giving me no joy.
I am probably missing something in the proporties or something you have to define before.
Here is my code:
private void tbxparkingTimesS1_TextChanged(Object sender, EventArgs e)
{
MessageBox.Show("You are in the ToolStripItem.TextChanged event.");
}
Thanks for any help with this trivial problem.
To wire the TextChanged event to a particular method inside your code do the following
Click on the TextBox inside your form
Open the properties windows (press F4 or menu View -> Property Window )
Select the event page (lightning icon)
Double click on the TextChanged property line
Insert your code inside the template build for you by Visual Studio
Have you assigned the event handler to the textbox?
Normally this will be done "behind the scenes" by Visual Studio - with the result being an additional line of code in your .designer file.
Something like:
this.tbxparkingTimesS1.TextChanged += new System.EventHandler(tbxparkingTimesS1_TextChanged);
(It['s been a while since I've done webforms - so that might be slightly off)
Double Click on Text box it will generate text change event for you.
private void tbxparkingTimesS1_TextChanged(object sender, EventArgs e)
{
// implement your code here.
}
When you double click VS will create event handler in your designer.cs file as bellow
this.tbxparkingTimesS1.TextChanged += new System.EventHandler(this.tbxparkingTimesS1_TextChanged);
You can do the same by using property window events or create event on code behind.

How do I register event handlers for a web user control in my code behind?

I'm having a problem setting up an event on a form. Here's the setup:
I was given a page with two controls, two different versions of a form for the end-user to fill out- standard and advanced. The two forms have different code and javascript, so the original dev put them in separate web user controls. Aside from the controls is a LinkButton that switches to Advanced mode.
<uc1:Standard runat="server" ID="StandardForm" />
<uc1:Advanced runat="server" ID="AdvancedForm" />
<asp:LinkButton runat="server" ID="lnkAdvanced" Text="Go To Advanced" OnClick="lnkAdvanced_Click" />
lnkAdvanced_Click just takes all the info currently entered to the advanced and flips the Visible.
My problem is that one of the bosses wants the 'Go to Advanced' button inside the standard form, but the .Visible code is on the page. So I thought it could be done using an event, but it doesn't seem to be working.
I tried to set up the event like this:
public event EventHandler AdvanceClick;
protected void lnkAdvanced_Click(object sender, EventArgs e) {
AdvanceClick(sender, e);
}
And when that didn't work, I tried to set up a delegate:
public delegate void AdvancedEventHandler(object sender, EventArgs e);
public event AdvancedEventHandler AdvanceClick;
When I moved the button to the standard form, I expected to be able to type something like:
StandardForm.AdvanceClick += new AdvancedEventHandler(GoToAdvanced);
But it doesn't seem to recognize any events within the control! I get an error: "Standard does not contain a definition for 'AdvanceClick' and no extension method 'AdvanceClick accepting a first argument of type 'Standard' could be found" It finds the other properties just fine, am I going about this the wrong way?
// in your Standard user control
public event EventHandler AdvancedClick;
private void lbtnAdvanced_Click(object sender, EventArgs e)
{
OnAdvancedClick(e);
}
protected void OnAdvancedClick(EventArgs e)
{
if (AdvancedClick != null)
AdvancedClick(this, e);
}
// on your page
StandardForm.AdvancedClick += new EventHandler(StandardForm_AdvancedClick);
private void StandardForm_AdvancedClick(object sender, EventArgs e)
{
// toggle logic here
}
If the standard form has the "Switch to advanced" button. Then, clearly, it has to know about the Advanced form and thus they seem to be pretty tightly coupled. If this is the case, it seems to me that you might as well just have the advanced form as a child of the standard form then... or better yet, merge them into one control.
If you don't like these options you might want to create a third controls which hosts the button and the two forms, along with the logic to move data between them and toggle their visibility.
I personally recommend the single control option. Having tighly coupled controls usually just leads to confusion down the road. You could loosen up the dependency in various ways, but think hard about it before you do so.
In the legacy project I currently work on we have a bunch of examples such as serach forms and search results being split up into multiple controls, but then in the end needing each others instances to function properly. As I said earlier, I wont reccomend this path.
You shouldn't need the delegate because you've created a standard event.
Try in your form load or thereabouts:
StandardForm.AdvanceClick += new EventHandler(GoToAdvanced);
Then somewhere on the page that hosts the 2 user controls
protected void GoToAdvanced(object sender, EventArgs e)
{
//Code that was previously in lnkAdvanced_Click on page.
}
Edit:
It does sound like the setup is wrong.
Can you post the markup for the Host page (at this point we are assuming it is simply the 2 user controls).
Then we are also assuming that the AdvanceClick event is declared in the Standard UC but the error message would indicate that it doesn't.. and the lnkAdvanced_Click method is in the Standard UC?
Then we are assuming the code that is attempting to attach to the custom event is declared in the Host page.
If you could confirm or deny the assumptions i'm sure we could get this cleared up.

Categories

Resources