cs_cedkat
Messages postés172Date d'inscriptionmardi 7 janvier 2003StatutMembreDernière intervention 3 juin 2011 9 févr. 2007 à 12:11
Je me suis servi de ton code pour essayer de faire une classe générique que voici :
public
class
Enumeration<T>
{
public
struct
EnumItem
{
public T Value;
public
string Description;
public
override
string ToString()
{
return
this.Description;
}
}
public
static
void SetCombobox(
ComboBox cbx)
{
Type t =
typeof(T);
if (!t.IsEnum)
throw
new
Exception(
"Le type n'est pas une ‚num‚ration.");
List<
EnumItem> items =
new
List<
EnumItem>();
EnumItem item;
foreach (T v
in
Enum.GetValues(t))
{
item =
new
EnumItem();
MemberInfo[] mis = t.GetMember(v.ToString());
item.Value = v;
item.Description = v.ToString();
if (mis.Length > 0)
{
DescriptionAttribute[] da = (
DescriptionAttribute[])mis[0].GetCustomAttributes(
typeof(
DescriptionAttribute),
true);
if (da.Length > 0)
{
item.Description = da[0].Description;
}
}
items.Add(item);
}
cbx.DataSource = items;
cbx.DisplayMember =
"Description";
cbx.ValueMember =
"Value";
}
}
Mais voila en exécution, une exception est levée lorsqu'il fait la liaison sur "Value" voici l'exception :
System.ArgumentException was unhandled
Message="Liaison au nouveau membre Display impossible.\r\nNom du paramètre : newDisplayMember"
Source="System.Windows.Forms"
ParamName="newDisplayMember"
StackTrace:
à System.Windows.Forms.ListControl.SetDataConnection(Object newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
à System.Windows.Forms.ListControl.set_ValueMember(String value)
à Arcade.Systèm.Enumeration`1.SetCombobox(ComboBox cbx) dans ligne 54
à S1Xml.frmMain.frmMain_Load(Object sender, EventArgs e) dans ligne 42
à System.Windows.Forms.Form.OnLoad(EventArgs e)
à System.Windows.Forms.Form.OnCreateControl()
à System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
à System.Windows.Forms.Control.CreateControl()
à System.Windows.Forms.Control.WmShowWindow(Message& m)
à System.Windows.Forms.Control.WndProc(Message& m)
à System.Windows.Forms.ScrollableControl.WndProc(Message& m)
à System.Windows.Forms.ContainerControl.WndProc(Message& m)
à System.Windows.Forms.Form.WmShowWindow(Message& m)
à System.Windows.Forms.Form.WndProc(Message& m)
à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
à System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
à System.Windows.Forms.Control.SetVisibleCore(Boolean value)
à System.Windows.Forms.Form.SetVisibleCore(Boolean value)
à System.Windows.Forms.Control.set_Visible(Boolean value)
à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
à System.Windows.Forms.Application.Run(Form mainForm)
à S1Xml.Program.Main() dans ligne 17
à System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
à System.Threading.ThreadHelper.ThreadStart()
Aurais-tu une idées ?
Et encore merci pour ton aide.
CedKat
Vous n’avez pas trouvé la réponse que vous recherchez ?
cs_cedkat
Messages postés172Date d'inscriptionmardi 7 janvier 2003StatutMembreDernière intervention 3 juin 2011 12 févr. 2007 à 13:51
Bonjour,
Je n'ai trouvé aucune solution au problème alors j'ai tester ton code exactement comme tu l'as décrit mais j'ai le meme problème avec l'erreur suivante :
"Liaison au nouveau membre Display impossible.
Nom du paramètre : newDisplayMember"
lorsqu'il veut binder le "ValueMember".
Est-ce un problème de code ou un bug du framwork ?