2

In my code I create an array of TextBoxes :

namespace TCalc
{
    public partial class MainWindow : Window
    {
        public TextBox[] pubAltArray;

        public MainWindow()
        {
            InitializeComponent();

            pubAltArray = new TextBox[10];

Then I create the TextBoxes programmatically using the following code :

private void generatePublishedTxtBox()
{
    for (int i = 0; i < 10; i++)
    {
        TextBox pubAlt = new TextBox();
        grid_profile.Children.Add(pubAlt);
        pubAlt.SetValue(Grid.RowProperty, 1);
        ...
        pubAltArray[i] = pubAlt;
    }
}

Than I have some routine I want to run when the content of each TextBox changes :

private void doTheStuff(object sender, TextChangedEventArgs e)
{
...
} 

So I tried to add event handler during the definition of new TextBox however without success :

pubAlt.TextChanged += new System.EventHandler(doTheStuff());

or

pubAlt.TextChanged += RoutedEventHandler(calculateCorAlts());

Any hint for me?

2
  • this is a basic understanding about c# other than anything else. I can imagine that you'll have a hard time experiencing WPF (unless you are some XAML designer but now want to learn C#). Commented Oct 7, 2015 at 23:58
  • Carefully inspect what the debugger says and google it before ask here. Commented Oct 8, 2015 at 6:35

2 Answers 2

2

Try:

pubAlt.TextChanged += new TextChangedEventHandler(doTheStuff);

or:

pubAlt.TextChanged += doTheStuff;

Both lines do the same thing. The second one is just shorthand for the first line since it makes code easier to read.

Sign up to request clarification or add additional context in comments.

4 Comments

That is actually what I have there, made and error when trying to simplify the code for the question. Anyway I receive an error CS0123 C# No overload for 'doTheStuff' matches delegate 'TextChangedEventHandler'. Thanks
your doTheStuff method must have the following signature: void doTheStuff(object sender, System.Windows.Controls.TextChangedEventArgs e). Make sure that the namespace you're using for TextChangedEventArgs is correct.
This actually did the stuff! Could you please explain why do I need to add the System.Windows.Controls in such case?
You should read up on namespaces. There is plenty of information available on the web. That'll answer your question.
0

You are invoking the method using (). Change your code to be like this:

pubAlt.TextChanged += new System.EventHandler((s,e) => doTheStuff());
pubAlt.TextChanged += RoutedEventHandler((s,e) =>calculateCorAlts());

Your method are not matching what it was asking for.

2 Comments

That is actually what I have there, made and error when trying to simplify the code for the question. Anyway I receive an error CS0123 C# No overload for 'doTheStuff' matches delegate 'TextChangedEventHandler' Thanks
I updated the code. I assume you had your parameter the same as the event handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.