Home - Forums-.NET - FlyGrid.Net (Windows Forms) - I want to a event method to be called in the a form

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

I want to a event method to be called in the a form
Link Posted: 19-Apr-2006 02:36
Hello!!

I use a product called flygrid to create grid tables.
In many of my forms I create such grid tables.
Some columns in these grid tables is of type drop down list where I
can select a value from the list.
Much of the code here is from the example that exist from the flygrid.

Below I have some code which exist in a file named StringClass.cs
There are two classes in the code StringClass and StringClassEditor.

When the drop down list is about to open method EditValue is called
and a ListBox is created and filled with values see the code below.
When a value is selected from the drop down list method
SelectedIndexChanged is called see code below.

Now to my problem I don't want to put all code that exist in
StringClass.cs in every form I use.
What I do want to do if it's possible is to have the event method in
this case SelectedIndexChanged to be put in the form class.

So for example when I have form named form1 and select a value from a
drop down list that exist in a flygrid an event metod that exist in the
form1 should be called. For example method SelectedIndexChanged.

One of my problems here is how do I modify class StringClassEditor so
the event metod can be called which exist in my form.

//moderator - I've moved your code to the next message in this thread.
Link Posted: 19-Apr-2006 08:20
[c#]
    [TypeConverter(typeof(StringClass.StringClassConverter))]
    [Editor(typeof(StringClass.StringClassEditor), typeof(UITypeEditor))]
    public class StringClass
    {
      private string strValue;
      protected ArrayList valuesList;
  
      public StringClass(string value)
      { strValue = value; }

      public StringClass()
      { strValue = null; }

      public StringClass(string value, string[] values) : this
        (value)
      { valuesList = new ArrayList(values); }
      
      public StringClass(string value, ArrayList values) : this
        (value)
      { valuesList = values; }

      public class StringClassEditor : UITypeEditor
      {
        private IWindowsFormsEditorService wse = null;
        private StringClass editedSc         = null;
        private string originalValue           = null;

        private bool StringClassHasValueList(StringClass sc)
        { return sc != null && sc.ValuesList.Count > 0; }

        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
          return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {      
          Type type = value.GetType(); //get type for value
                  
          if (StringClassHasValueList(value as StringClass))
          {
            wse = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            if (wse != null)
            {    
              ListBox lb = new ListBox();
              editedSc = (StringClass) value;
              originalValue = editedSc.StrValue;
              string[] values = editedSc.ValuesList.ToArray(typeof(string)) as string[];
                
              lb.Items.AddRange(values);
              int selectedIndex = editedSc.
                StrValue != null ? lb.Items.IndexOf(editedSc.StrValue) : -1;
              if (selectedIndex != -1)
                lb.SelectedIndex = selectedIndex;
                        
              lb.KeyDown              += new KeyEventHandler(ListBox_KeyDown); //anger en delegate
              lb.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);
              lb.Click                += new EventHandler(ListBox_Click);
                                
              wse.DropDownControl(lb);
              wse = null;
              return editedSc;
            }
          }
          return base.EditValue (context, provider,
            value);
        }

        private void ListBox_KeyDown(object sender, KeyEventArgs
          e)
        {
          if (wse != null && (e.KeyCode == Keys.Escape || e.
            KeyCode == Keys.Return))
          {
            if (e.KeyCode == Keys.Escape)
              editedSc.StrValue = originalValue;
            //return to original value
            wse.CloseDropDown();
          }
        }

        private void ListBox_Click(object sender, EventArgs e)
        {  wse.CloseDropDown(); }

        private void ListBox_SelectedIndexChanged(object sender,
          EventArgs e)
        {
          ListBox lb = (ListBox) sender;
          editedSc.StrValue = (string)lb.SelectedItem;
        }
      }
    }
Link Posted: 19-Apr-2006 08:37
You can create event in StringClass:

public class StringClass
{
  //...
  public delegated void SelectedIndexChangedHandler(object sender, int index);
  public event SelectedIndexChangedHandler SelectedIndexChanged;
  public void OnSelectedIndexChanged(int index)
  {
    if (SelectedIndexChanged != null)
      SelectedIndexChanged(this, index);
  }
  //.....
}


and fire this event from StringClassEditor:

private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
  ListBox lb = (ListBox) sender;
  editedSc.StrValue = (string)lb.SelectedItem;
  editSc.OnSelectedIndexChanged(lb.SelectedIndex);
}


But more simply will be following:

public class StringClass
{
  //......
  private string strValue;
  public string Value
  {
    get {return strValue;}
    set
    {
      if (strValue != value)
      {
         strValue = value;
         OnChanged();
      }
    }
  }
  public EventHandler Changed;
  public void OnChanged()//notify about changes
  {
    if (Changed != null)
      Changed(this, EventArgs.Empty);
  }
}


and in StringClassEditor:

private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
  ListBox lb = (ListBox) sender;
  editedSc.Value = (string)lb.SelectedItem; //after value changing StringClass.Change event will fire.
}
private void ListBox_KeyDown(object sender, KeyEventArgs e)
{
  if (wse != null && (e.KeyCode == Keys.Escape || e.KeyCode == Keys.Return))
  {
    if (e.KeyCode == Keys.Escape)
      editedSc.Value = originalValue;
    //return to original value
    wse.CloseDropDown();
  }
}


in this case you can connect to StringClass.Changed event to be notified about changes.