Calling my own event code and custom event code in c# - c#

I have made a WinForms application with a custom richtextbox control.
I am referring it as a Windows control (a dll file) in my project. In my program for the richtextbox I have written functionality inside it's textchanged event.
I want to do additional work only after the textchanged event is fired or in other ways once the text is added to the textbox. Something like I want to call a function foo() in the text_changedevent. It only calls foo and fails to process the underlying textchanged event.
Any way in C# I can make it process the internal textchanged event first, and then look into my text changed event?
think of the scenario I have written the code for mytextbox_textchanged
private void txt_code_TextChanged(object sender, EventArgs e)
{
//some code which will always be called whenever textchanged event occurs.
}
Now I inherit this control in my project say MyApp1. Here I have a label where I want to display the number of lines contained inside my textbox. So I would write
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
//code to update the label with my_inherited_txt_code.lines.length
}
so my problem was, I first wanted the txt_code_TextChanged event to be called and then do the code written inside my_inherited_txt_code_TextChanged. Which was solved by writing
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
base.OnTextChanged(e);
MessageBox.Show("foo");
}

Do you mean:
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
// your code here...
}

I cant see why you shouldnt be able to call a method from the text_changed event?
Do you get any errors or what happens exactly?

Related

Repeat same steps on every textChanged event

I have in my project 5 text boxes.
Every TextBox should accept only digits.
For that I created a function which takes not prepared text and returns the proper one.
Now I'm wondering if there is any simpler way to perform this action on every TextBox, on every TextChanged event without repeating almost same code?
private void TextGoldPack_TextChanged(object sender, EventArgs e)
{
(sender as TextBox).Text = Only_digits((sender as TextBox).Text);
}
private void TextGoldTake_TextChanged(object sender, EventArgs e)
{
//repeat here and on every _TextChanged event
}
If I'm understanding you correctly, just because it's named TextGoldTake_TextChanged, doesn't mean that's the only textbox that can use that code. On the events tab, you can set the TextChanged function for all you textboxes to lead to that function. If it helps, rename it something that doesn't sound textbox-specific such as TextChanged.
Change all the TextBoxes to refer this method upon TextChanged.
Use the sender property to get the actual caller TextBox.

C# WinForms - Detect User Click Into Text Box

I have been developing a windows form application and ran into a problem.
After trying various things (Listed below) I have come to seek your knowledge to help point me in the right direction.
I have replicated a much simpler version of my program:
As you can see, I have two textboxes. I want to be able to click on the textbox on the bottom (textbox1) and call some form of an event, in this case, for simplicity, pop up a message box.
I have been through the events listed here:
https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events(v=vs.110).aspx
And implemented them into my code as I expected one of them to work. However, this is not the case.
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
//Above - Will pop message box when text entered.
private void textBox1_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
private void textBox1_Enter(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
private void textBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
Does anybody know what I am missing? I presume what I am trying to achieve is actually possible?
Kind Regards,
B.
Ensure the event is subscribed to the methods you have written. You can do this in the design view using the Events tab of the property window (looks like a lightning bolt). As mentioned by others, a double click in the events window will generate the event's method for you, and subscribe to it automatically.
Another way is to subscribe directly using code; you could write this in the form constructor for example:
textBox1.TextChanged += textBox1_TextChanged;

What determines the event

I'm only a beginner so ,my question might sound a bit stupid or basic.
I learn programming in asp.net, therefore I see a lot of functions activated by events. Yet, I didn't find anything in the code nor in the type signature that defines which event activates the function.
So, in functions like public void Page_Load (object sender, EventArgs e), where are the code lines that determine what event will make the function to start? Does it have any relation to the function's name?
Thanks :)
Functions like Page_Load are called by ASP.NET in a particular order. You cannot configure which will fire first. The idea is that you override the ones you need to fire your code in the particular order you need.
Here is the MSDN Page Lifecycle information which talks about which event can be overridden and what order they go in.
In ASP.Net 1.1, we used to have the following system generated code in every code behind files.
public class Default : System.Web.UI.Page
{
// ----- System generated code
protected System.Web.UI.WebControls.TextBox Name;
protected System.Web.UI.WebControls.TextBox Email;
public Default()
{
Page.Init += new System.EventHandler(Page_Init);
}
// ----- System generated code
private void Page_Init(object sender, System.EventArgs e)
{
}
}
It basically registers method to page event. They are nothing but just make the code behind file dirty.
Start from ASP.Net 2, they moved the system generated code to designer file, and code behind file becomes clean and easy read.
public class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
}
-- OR --
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
}
where are the code lines that determine what event will make the
function to start?
ASP.Net uses conversion over configuration approach to register events. It means, you can name a Protected method with following event name, and the page will know how to attach those event. For example, Page_Init, Page_Load and Page_PreRender
In addition, you can override those events explicit if you want.
https://msdn.microsoft.com/en-us/library/ms178472.aspx

How to register event for TextBox end editing

Super newbie to C# (first day coding), so please don't judge me too much if the following is really a stupid question to you.
But I am looking for a way to register an event for when a TextBox end editing, which is similar textFieldDidEndEditing(_ textField: UITextField) delegate in iOS.
After some googling around, I know how to register an textChanged event with the following:
In .xaml file:
<TextBox TextChanged="textChangedEventHandler"/>
In .cs file:
protected void textChangedEventHandler(object sender, TextChangedEventArgs args)
{
}
I also notice this SO, and finally this documentation by MS and notice this following function:
// This method handles the LostFocus event for textBox1 by setting the
// dialog's InitialDirectory property to the text in textBox1.
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
// ...
}
But what is not obvious to me is how do I register this event function? Or how do I let the GUI know to call this function when the TextBox end editing?
This is finally what it takes for it to work:
In .xaml file:
<TextBox LostFocus="textFinishedEditingEventHandler"/>
In .cs file:
public void textFinishedEditingEventHandler(object sender, RoutedEventArgs e)
{
}
Thanks to #Dacker!~
Normally an event can be registered in two ways:
In markup. In ASP.Net each type of event is exposed with the prefix On, so for Click it's OnClick. In your xaml I don't see the On prefix, so that makes me guess it is the following in your case:
<TextBox LostFocus="textBox1_LostFocus" />
In code behind (.cs)
textBox1.LostFocus += textBox1_LostFocus
If you understand this, you can use better names for textBox1_LostFocus to describe more what will happen instead of when it will happen.

How to call an event handler from one control to the another control?

In Visual C# Form Application, When I Click on the button I want to add to the other controls(like listboxes,labels,textboxes) in same form.
How do I do this?
I have no idea what "to come to the other controls" might mean. But the event handlers in your Form derived class is the switchboard. Implement the button's Click event and have it do whatever you want done with any other controls. A trivial example:
private void button1_Click(object sender, EventArgs e) {
label1.Text = "You clicked the button!";
}
In the form designer, add an event handler to the button's Click event.
The form designer will give you a new method like this; add your code into this method:
private void button_Click(object sender, EventArgs e)
{
// Write some code that uses list boxes, labels, text boxes etc.
}
You question is somewhat unclear, but if you simply want to access other controls on the form, just go ahead and do so:
private void YourButton_Click(object sender, EventArgs e)
{
string someValue = yourTextBox.Text;
// do something with the value
}
If you want to add one event handler to many controls, you can do it.
Just go to properties of control you wish to subscribe, find appropriate event from list (ex: onClick) and choise your existed handler.
But this method will be sutable if events compotable.
Describe your task more detail.

Categories