Code highlighting

Monday, April 28, 2008

SysFormEnumComboBox - class allowing to show only some of the values for a comboBox

In Microsoft Dynamics AX 2009, a new class has been created, that got my attention recently after yet another question about this sort of thing was asked by a fellow Dynamics AX developer.

Often enough, we need to restrict the user selection from a particular ComboBox. Creating a new BaseEnum specifically for this purpose is a really cumbersome solution, which later might lead to problems of maintenance.

Originally, the SysFormEnumComboBox was designed to be used on forms to provide for this behavior. I modified the class slightly to allow for usage in RunBase Framework classes.
To demonstrate, how this class can be used, I modified the tutorial_RunBaseForm class and form, showing both scenarios: adding the control from the dialog method of the class, as well as adding it manually in the form design and methods.

To use this class, you need to know only one static method:
public static SysFormEnumComboBox newParameters(
FormRun _formRun,
int _comboBoxControlId,
enumId _enumId,
Set _allowedEnumValuesSet,
Form _form = null)

And here is an example of how you would use it:
sysFormEnumComboBox = SysFormEnumComboBox::newParameters(element, 
control::ComboBoxOnForm,
enumnum(InventTransType),
enumSet);

which means that the control ComboBoxOnForm will be bound to BaseEnum InventTransType, containing only values, found in the Set enumSet.

You can download the project for versions 4.0 and 2009 of Microsoft Dynamics AX through the following link:
download

P.S. It is also worth mentioning, that this class does not provide support for grids. It requires a stand-alone control, not bound to a database table field.

14 comments:

  1. hi,

    thanks for ideas.

    But I try similar things on form CustTable and didn't work:(.
    Field Blocked from table CustTable is enum CustVendorBlocked extend about new 2 elements.
    I want show only original elements +1 new.
    At init method I use your way(seup at general tabpage AutodataGroup administration -> no and autodeclaration to yes.

    btw. Did I understand correctly that this way is used only enum interval? exapmle I have 0,1,2,3,4 elements and only 0,1,2 range not 0,1,4?

    Thank you very much for ideas

    Martin Mikes
    martin.mikes@circon.cz

    ReplyDelete
  2. What exactly did not work? Are you sure that you followed the guidelines correctly?
    What AX version do you have?

    The set of values you can use does not have to be sequential, so you can as well specify 0,1,4 as the range to display in the combo box.

    If you export your xpo and send it to me, I might figure out what is wrong.
    Or simply describe your problem in more details - what error message you get, or what behavior is incorrect?

    ReplyDelete
  3. I need to do that in a specific event. i.e. I have transaction main type and transaction subtype, when selecting a main type I want to filter the conbo to show specific fieds for each main type.
    I tried it but it works only at the init event of the form.
    thanks

    ReplyDelete
  4. I'm a AX beginner. Actually I don't know where should I start even no any book for reference!
    I just go through with the website and search the limited info!
    Any idea to guide me?
    Thanks

    ReplyDelete
  5. Well, there is a number of books available on Dynamics AX nowadays.
    If you are interested in the development part, I would first look at Inside Dynamics AX 2009 (http://www.amazon.com/Inside-Microsoft-Dynamics%C2%AE-AX-2009/dp/0735626456)
    If you are interested in the application part, I would start with "Introduction to Materials Management by APICS" (http://www.amazon.com/Introduction-Materials-Management-J-R-Arnold/dp/0132337614), continued by the specific application module functional training (http://www.microsoft.com/learning/en/us/course.aspx?ID=80024A&Locale=en-us)

    ReplyDelete
  6. I'm a beginner also.
    Actually I don't know when I used the specify classes, object, module functions, even whether its existing or not! How?

    ReplyDelete
  7. Well, if I understood your question at all, you were asking about a way to determine which class/functino to use when? Right?

    If that's the case, I am sorry to say there is no easy answer here. This you learn with experience and real-life projects and scenarios that you work with.

    Some information for very common scenarios is available in the Development training, part IV.

    ReplyDelete
  8. Hi Vanya,

    First, great post. Question: If I don't need SysFormEnumComboBox class to extend RunBase, can I still use the newParameters method on the form level to limit the selection of a combo box (instead of creating another enum of course) without modifying the class?

    I am looking to implement this functionality on one of my forms.

    Great work.

    Regards.

    ReplyDelete
  9. Well, I don't remember the specifics of the implementation, but I think that yes, you should be able to use newParameters method without any issues.
    It's very easy to try, I think that would be the best way to go for you.

    ReplyDelete
  10. Hi!

    I'm trying to implement this solution, but has a problem, the comboBox is attached to a field in a DataSource, so it doesn't work, it works if the comboBox doesn´t use any DataSource. Exists some way to implement this when the ComboBox uses a DataSource? Thanks

    ReplyDelete
  11. Well, I don't know of a way to implement that for a data source field, because the kernel tightly binds the elements to the possible field values.

    ReplyDelete
  12. Hai Kashperuk-

    How are you going to implement this on Form, as we dont have control id before the super() call in the init method of the Form. I cannot find the control enum on the Form and it is throwing compile error. Do you know any other way. this only works for the run base

    ReplyDelete
  13. Not of the top of my head, sorry.
    But are you sure it throws an error when you try to access that specific FormBuildStringControl? This should exist before super(). And that's what you need, as far as I remember from the implementation of this class

    ReplyDelete
  14. Just to clarify - you are probably accessing the FormStringControl instead of its "Build" version

    ReplyDelete

Please don't forget to leave your contact details if you expect a reply from me. Thank you