i want that when i click NewToolstripbutton it run active child form newbutton procedure

Note: newbutton is a userdefine Procedure.

Help if any body known

Ammad Iqbal

Dani AI

Generated

used a VB late-bound style that C# does not allow by default. was right to call out method visibility — NewButton must be public — but in C# you also need a compile-time type (or an interface/dynamic) before you can call the method. Iterating Application.OpenForms and comparing Name will work, but it is brittle and less clear than casting the active MDI child or using a shared interface.

Safer, minimal options:

// cast to the concrete child type
var supplier = this.ActiveMdiChild as Frm_Supplier;
if (supplier != null)
{
    supplier.NewButton();
}
// preferred when multiple child types share the same behaviour
public interface INewable { void NewButton(); }
// child forms implement INewable
var newable = this.ActiveMdiChild as INewable;
if (newable != null) newable.NewButton();

Notes and cautions: always check for null (no active child or wrong type). Prefer an interface when several child forms expose the same operation — it gives compile-time safety and keeps the parent decoupled from concrete form classes. Avoid matching on Form.Name strings. Also do not swallow exceptions: if you catch, use catch (Exception ex) and inspect ex.Message or rethrow; creating a new Exception and showing its Message will hide the real error. Dynamic binding is another option (C# dynamic), but it moves errors to runtime and is less safe than the interface approach.

Recommended Answers

All 3 Replies

I dont think its such a hard Question. I am New in C#.
IN Vb i do this in that fashion

Dim FrmChild As Object
FrmChild = Me.ActiveMdiChild
FrmChild.NewButton()

And its work But in C# it generate error on NewButton()

You need to ensure that the MDI Child has a method called NewButton and that the method is declared as Public.

You need to ensure that the MDI Child has a method called NewButton and that the method is declared as Public.

NewButton is a public Method. but i resoled it like this:

try
            {
                FormCollection fc = Application.OpenForms;
                foreach (Form f in fc)
                {
                    if (f.Name == "Frm_Supplier")
                    {
                        ((Frm_Supplier)(f)).NewButton();
                    }
                }
            }
            catch
            {
                Exception Ex = new Exception();
                MessageBox.Show(Ex.Message);
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.