1

first of all, my english is not very good so if you could be kind it would be appreciated. Thanks.

Now my problem, like I said in the title, I would like to pass a "method name" as a parameter in another method. Like a picture is worth thousand words, there is a chunk of my function:

 public void RemoveDecimalPoints(TextBox txtBoxName, Func<string, TextBox> txtBoxMethod) { //Some Code txtBoxName.KeyPress += new KeyPressEventHandler(txtBoxMethod); } 

I want the second parameter to point to this other method:

 private void txtIncomeSelfValue1_KeyPress(object sender, KeyPressEventArgs e) { //Some Code } 

Sorry if im not clear for some, I lack some vocabulary...

Thanks for your help.

0

2 Answers 2

2

Assuming you are calling this RemoveDecimalPoints method from the same class that contains the txtIncomeSelfValue1_KeyPress method you could pass it like this:

RemoveDecimalPoints(someTextBox, this.txtIncomeSelfValue1_KeyPress); 

but you will have to modify the signature as Func<string, TextBox> doesn't match the txtIncomeSelfValue1_KeyPress method:

public void RemoveDecimalPoints(TextBox txtBoxName, KeyPressEventHandler txtBoxMethod) { //Some Code txtBoxName.KeyPress += txtBoxMethod; } 
Sign up to request clarification or add additional context in comments.

2 Comments

had to give you an upvote - your answer was (independently) almost exactly the same as mine. :-)
Tanks for your help! I tried that but not the good way, in txtBoxName.KeyPress += txtBoxMethod; I was using txtIncomeSelfValue1.KeyPress += new KeyPressEventHandler(txtBoxMethod); I was close! lol ^^
1

If you're happy to write your code like this:

RemoveDecimalPoints(txtBoxName, txtIncomeSelfValue1_KeyPress); 

then you can use:

public void RemoveDecimalPoints( TextBox txtBoxName, KeyPressEventHandler txtBoxMethod) { //Some Code txtBoxName.KeyPress += txtBoxMethod; } 

If you want to use a string then you need to use reflection and your signature would need to look like this:

void RemoveDecimalPoints(TextBox txtBoxName, string txtBoxMethod) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.