Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I control the state of a custom verb in a custom designer?

You have to store a reference to the DesignerVerb instance you create to represent the custom verb. You can then update the state of the verb through this reference.

Here is an example:

public class CustomControlDesigner : ParentControlDesigner 
{ 
  private DesignerVerb removeVerb; 
  private DesignerVerbCollection verbs; 

  public override void Initialize( IComponent component ) 
  { 
    // Update your designer verb whenever ComponentChanged event occurs. 
    iComponentChangeService = 
      (IComponentChangeService) GetService( typeof( IComponentChangeService ) ); 
    if ( iComponentChangeService != null ) 
      iComponentChangeService.ComponentChanged +=
        new ComponentChangedEventHandler( ComponentChanged ); 
  }

  protected override void Dispose( bool disposing ) 
  { 
    // ... 
    if ( iComponentChangeService != null ) 
      iComponentChangeService.ComponentChanged -= 
        new ComponentChangedEventHandler( ComponentChanged ); 
  }

  public override DesignerVerbCollection Verbs 
  { 
    get 
    { 
      if ( verbs == null ) 
      { 
        removeVerb = 
          new DesignerVerb( "Remove Tab", new EventHandler( OnRemove ) ); 
        verbs = new DesignerVerbCollection(); 
        verbs.Add( removeVerb ); 
      } 
      removeVerb.Enabled = ( Control.Controls.Count > 0 ); 
      return verbs; 
    } 
  } 

  private void UpdateVerbStatus() 
  { 
    if ( removeVerb != null ) 
      removeVerb.Enabled = ( Control.Controls.Count > 0 ); 
  }

  private void CheckVerbStatus( object sender, ComponentChangedEventArgs e ) 
  { 
    UpdateVerbStatus(); 
  } 
} 

Contributed from George Shepherd's Windows Forms FAQ



Page view counter