Re: I want to a event method to be called in the a form.



You will have to do a delegate and wrap event around the delegate, make the
event public on the grid.. and make sure your Grids in the Form subscribe to
the events... Even before you have to write your own dervied grid... see
below.. The code is to give you a idea of flow.... I hope you can implement
this to your needs...

namespace MyNameSpace
{
public MyGrid : System.Windows.Forms.DataGrid
{
public event MySelectedIndexChangeEventHandler
mySelectedEvent=null;


private void MyGrid_selectedIndexChanged(object sender, EventArgs e)
{
if ( mySelectedEvent!= null )
{
mySelectedEvent(this, new MySelectedIndexEventArgs
("test"));
}
}

}

public delegate void MySelectedIndexChangeEventHandler (Object sender,
MySelectedIndexEventArgs e);

public class MySelectedIndexEventArgs : EventArgs
{
private string _myArg;

public string myArg
{
get { return _myArg ; }
set { _myArg = value ; }
}

public MySelectedIndexEventArgs ( string MyArg1)
{
_myArg = MyArg1;
}
}

}

' In your Form

private void myTest()
{
myGrid1.mySelectedEvent+=new
System.EventHandler(this.myNewSelectedEvent);
}

private void myNewSelectedEvent(object sender, EventArgs e)
{
// e.myArg will give "test"
}

VJ

"tony" <johansson.andersson@xxxxxxxxx> wrote in message
news:eNYFQW7YGHA.3740@xxxxxxxxxxxxxxxxxxxxxxx
Hello!!

Hello Victor!

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.

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.

[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(( StringClass ) value))
{
wse = (IWindowsFormsEditorService )
(provider.GetService(typeof(IWindowsFormsEditorService)));
if (wse != null)
{
ListBox lb = new ListBox();
editedSc = (StringClass) value;
originalValue = editedSc.StrValue;
string[] values = (string [])
(editedSc.ValuesList.ToArray(typeof(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;
}
}

//Tony




.



Relevant Pages

  • I want to a event method to be called in the a form.
    ... I use a product called flygrid to create grid tables. ... There are two classes in the code StringClass and StringClassEditor. ... private void ListBox_KeyDown ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Creating on-the-fly DataGridViews from SQL queries
    ... having a method handle the grid. ... private void LoadMainGrid(string SQL) ... private void populateGrid(DataGridView dg, string row1, string ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: My First C# (warning - long post)
    ... public CodeStatistics(String csServerName, String csElementId) ... private void updateLocInTheCscsDatabase() ... linesOfGuiCode(clLine, ref commentStart, ref ... commentStart, ref String commentEnd, ref String commentSingle) ...
    (comp.lang.cobol)
  • C# Event Handlers For Dynamically Created DropDownLists
    ... Problem - when the user selection is extracted form the dropdownlists ... private void Page_Load ... private void btnLoadDb_Click(object sender, System.EventArgs e) ... string dbFileDestination = Path.Combine; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Retain PlaceHolder ViewState
    ... the dropdownlists. ... Object reference not set to an instance of an object. ... sourceControl, String eventArgument) ... private void Page_Load ...
    (microsoft.public.dotnet.languages.csharp)