0

I have a method in VB.Net that calls button of form:

Private Sub BUTTON_CAL( _
    ByVal frm As Object, ByVal e As System.Windows.Forms.KeyEventArgs)

    If e.KeyCode = Keys.A AndAlso e.Modifiers = Keys.Control Then
        If frm.AddButton.Enabled = True Then Call frm.AddButton.PerformClick()
        e.SuppressKeyPress = True
    End if
End Sub

I have converted this code into c#

public static void BUTTON_CAL(object frm, System.Windows.Forms.KeyEventArgs e) {
    if(e.KeyCode==Keys.A&&e.Modifiers==Keys.Control) {
        if(frm.AddButton.Enabled==true) {
            frm.AddButton.PerformClick();
        }
        e.SuppressKeyPress=true;
    }
}

in C#.Net I am getting the error

'object' does not contain a definition for AddButton' and no extension method 'AddButton' accepting a first agument of type 'object' could be found(are you missing a using directive or an assembly reference?)

8
  • 6
    You need to cast frm to it's actual type. Show where you call this method. Commented Mar 28, 2013 at 8:02
  • frm is System.Windows.Forms Commented Mar 28, 2013 at 8:03
  • @TimSchmelter BUTTON_CAL(object frm ... Commented Mar 28, 2013 at 8:03
  • 4
    Side-note: you should really set OPTION STRICT on in VB.NET as well. Also, Call is a redundant statement. Commented Mar 28, 2013 at 8:05
  • 1
    Another possibility (which doesn't deserve an answer because it encourages excessive late binding) would be to declare frm as dynamic instead of as object. Commented Mar 28, 2013 at 8:07

4 Answers 4

5

Cast 'frm' to the data type you expect it to be (runtime checking if valid is a good idea)

Form form = frm as Form;

if( null == form )
    // error

form.AddButton(...);
Sign up to request clarification or add additional context in comments.

8 Comments

If you're going to throw an error if it's null, just cast instead - it'll be simpler code, and give a clear exception. Also, there's no reason to use if (null == form) rather than if (form == null) in C#. It's not like the typo of if (form = null) would compile.
Yeah, old habits from C++ die hard ;)
System.Windows.Forms does not contain the definition for 'AddButton' ... How to implement ????
@MandeepSingh "Cast 'frm' to the data type you expect it to be" states the answer. Form is just an example
You can not use even one form if it does not have AddButton property. The solution can be to create in interface IAddingButton{ SomeType AddButton {get; set;} } , inherit all the forms from this interface and cast to it IAddingButton form = frm as IAddingButton;
|
0
public static void BUTTON_CAL(object sender, KeyEventArgs e) {
    if(sender is Form) {
        var frm=sender as Form;

        if(e.KeyCode==Keys.A && e.Modifiers==Keys.Control) {
            if(frm.AddButton.Enabled)
                frm.AddButton.PerformClick();

            e.SuppressKeyPress=true;
        }
    }
}
  1. The signature of KeyEventHandler is

    public delegate void KeyEventHandler(object sender, KeyEventArgs e);
    
  2. Rather if(SomeBool) than if(SomeBool==true). Don't be unnecessarily complicated.

  3. put someObject as SomeType in a if(someObject is SomeType) would be safe. It seems a little bit redundant, but more readable than compare to null.

Comments

-1

Thank you all for giving time to solve the problem I have solved the problem by using below :

public static void BUTTON_CAL(object sender, System.Windows.Forms.KeyEventArgs e) 
{
     var frm = sender as Form;

     if(e.KeyCode==Keys.A&&e.Modifiers==Keys.Control) 
     {  
          if(frm.Controls["AddButton"].Enabled==true)
          {
             ((Button)frm.Controls["AddButton"]).PerformClick();
          }
          e.SuppressKeyPress=true;
     }
}

2 Comments

You really should accept one of the proposed answer, instead of writing your (redundant) one.
The original question was how to use Object in C# the same way it can be used in VB. IMHO, this answer is the only one that tries to do some kind of "duck typing" as VB allows so it is not redundant. In fact, other answers were only half complete since they missed that part.
-2

cast frm to the class name of your form

if (((Form1)frm).AddButton.Enabled == true){
   ((Form1)frm).AddButton.PerformClick();
}

I assume Form1 is the name of your form, and it has a button with name AddButton and that button has the proper access modifier.

1 Comment

Forms.Form does not have AddButton field or property. -1

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.