StrataFrame Forum

Passing a Parameter to the LaunchDialog Method

http://forum.strataframe.net/Topic26964.aspx

By Bill Cunnien - 4/22/2010

I would like to present a list of orders to the user. If the user double-click an order in the list, the order maintenance window will open and the BO will be populated with the primary key being passed to it. I have done this in a number of situations outside of the StratFlix-like application. Now, I would like to do the same but take advantage of the code already existing. The LaunchDialog looks like this:





private void LaunchDialog(Type dialogType)

{

    //-- Establish Locals

    Form f = (Form)(Activator.CreateInstance(dialogType));



    //-- Add the handlers to the form

    AddFormHandlers(f);



    //-- Set the parent MDI

    f.MdiParent = this;



    //-- Show the dialog

    f.Show();

}





How would I pass the primary key via the Activator.CreateInstance method? I'll start researching it and testing. Hopefully, someone here will be able to shave off some of my R&D time (so I can get to R&R quicker!).



Thanks!!

Bill
By Greg McGuffey - 4/22/2010

Bill,



One of the overloads of the CreateInstance accepts an array of objects. CreateInstance then looks for the constructor of the indicated type that best matches that array of objects:



private void LaunchDialog(Type dialogType, object args[])

{

  //-- Establish Locals

  Form f = (Form)(Activator.CreateInstance(dialogType, args));



  //-- Add the handlers to the form

  AddFormHandlers(f);



  //-- Set the parent MDI

  f.MdiParent = this;



  //-- Show the dialog

  f.Show();

}





Now say you have a dialogType of WidgetMaintenanceForm:



public class WidgetMaintenanceForm

  : MicroFour.StrataFrame.UI.Windows.StandardForm

{

  public WidgetMaintenanceForm() : base() { InitializeComponents(); }



  public WidgetMaintenanceForm(int parentWidget) : this()

  {

    this.ParentWidget = parentWidget;

  }

}




Now you could call LaunchDialog in one of two ways:



//-- Launch using default constructor

LaunchDialog(typeof(WidgetMaintenanceForm), new object() {})



//-- Launch and provide a parent ID

LaunchDialog(typeof(WidgetMaintenanceForm), new object() {1})




Hope that helps with the R&D...on to the R&R! Cool
By Bill Cunnien - 4/22/2010

I was just wrapping up my testing and thought I would check back on the forums. Greg...thanks for responding so quickly! You and I were thinking a lot alike this afternoon. I pretty much did what you provided as an example. I did a couple of things differently. The first was the LaunchDialog method syntax. Mine looks like this:



public void LaunchDialog(Type dialogType, params object[] pParameters)




The second was to (as you see in the snippet above) was to make the method public. From the MDI child dialog window, I need to call this method in the MDI parent form. That was the easiest way for me to do it. So, my resulting method call in the dialog looks like this:





private void OpenMaintenanceWindow()

{

    if (gridView1.SelectedRowsCount > 0)

    {

        int mIndex = (int)gridView1.GetFocusedRowCellValue("DEX_ROW_ID");

        PurchasingMain mForm = (PurchasingMain)ParentForm;

        mForm.LaunchDialog(typeof(VendorMaintenance), mIndex);

    }

}





I am using a DevEx grid to display the vendors. Either a double-click on the vendor in the grid, or a click of the dynamic action menu item will call this method and open the maintenance window directly to the vendor ready to be maintained.



I did handle the actual maintenance window's constructor slightly differently, too. Here is what I put together:





private int _vendorindex;



public VendorMaintenance() : this(0) { }



public VendorMaintenance(int pVendorIndex)

{

    _vendorindex = pVendorIndex;

    InitializeComponent();

}



private void VendorMaintenance_Load(object sender, EventArgs e)

{

    CreateSaveUndo();

    if (_vendorindex > 0)

    {

        vendorsBO1.FillByPrimaryKey(_vendorindex);

    }

}





I am trying to override the Save/Undo auto creation in the action menu. Haven't quite got that right, yet. The rest seems to be working out quite well.



Almost time for that R&R...
By Greg McGuffey - 4/22/2010

I like the params approach. This means you don't have to have n overloads for all possibilities. I.e. if you then use LaunchDialog some other form (oh, say WidgetMaintenanceForm) and it has an overloaded constructor:



public WidgetMaintenanceForm(string itemName, int itemIdex, WidgetFormMode mode)



there are no changes to LaunchDialog:



LaunchDialog(typeof(WidgetMaintenanceForm), "Furry Widget", 342, WidgetFormMode.Edit);



Cool! I need to go update my LaunchDialog method! BigGrin
By Bill Cunnien - 5/21/2010

I made one more tweak to my code to handle the launching of various forms. Instead of a different method for each ToolStripMenuItem (it was getting a tad messy in my project!), I decided to create a common method. In order for that to happen, I added a string to the Tag property of the ToolStripMenuItem. The string is the full name of the form that is being launched (e.g. "MyNamespace.MyFolder.MyForm"). I left the LaunchDialog alone (except for the earlier modification for passing some parameters). Here is the method I created:





private void OpenForm(object sender, EventArgs e)

{

    LaunchDialog(Type.GetType(((ToolStripMenuItem)sender).Tag.ToString()));

}





Now, in the Click handler, all I need to do is point to this method. Works like a charm. At least, it made my day!! w00t



Bill
By Greg McGuffey - 5/21/2010

Nice Bill!



Another way this could be handled would be to use a property provider, like a tooltip provider or a error provider. This would allow you to drop a component on a form that would manage using buttons (or any other UI elements desired) to open a form via the LaunchDialog method. You'd just set a property or two (type name, maybe some optional parameters, maybe an enable property). It would then manage the click event for you. If I get a minute, I'll do up a sample. I've used this sort of thing a bunch and it really makes life easy.
By Edhy Rijo - 5/21/2010

Thanks Bill.



Greg McGuffey (05/21/2010)
If I get a minute, I'll do up a sample. I've used this sort of thing a bunch and it really makes life easy.




Greg, please take 2 minutes Hehe you got me intrigued now, my LaunchDialog() is working fine, but I am always looking for better ways to incorporate and learn.
By Greg McGuffey - 5/21/2010

OK, here's the sample. Notice that there is almost no code in the forms. The only code is in the MainForm to initialize the launcher to use the MainForm as the MDIForm.



Also notice that I am using delegates to deal with adding handlers to the form. This allows for a more flexible design, as you can swap out the handlers you add to each form opened as needed by each app.



The Yellow form is a blank form, ready for you to experiment. Drop on the LauncherProvider, add a button/link/toolstripbutton and set the FormType for the button/link/Toolstripbutton. Done. Cool



You might want to experiment with extending the LauncherProvider to support menu items as well. If you check out the CanExtend method, you'll see how do that. One line of code should do it. BigGrin (Though you'll want to see what is happening in the SetFormType method also, which is were the click event is setup for each extended control).



Another thing you might want to experiment with is to add another property to manage what happens if the form needs arguments. I was thinking that a cool way to handle this would be to add an extended property to indicate if the form needs arguments. Then raise an event, passing out the form type. Then on the form with the control being extended, you'd handle this event of the LauncherProvider, test for form type, and provide the arguments....kind of like ListView/ComboBox items do in SF (ListPopulating event).



Have fun!


By Greg McGuffey - 5/24/2010

Edhy, you might have missed this with the servers being down....
By Edhy Rijo - 5/24/2010

Greg McGuffey (05/24/2010)
Edhy, you might have missed this with the servers being down....


Hi Greg,



Yeap, I did and still on my browser I get the yellow/red warning about forum maintenance. w00t



Will download now. I am reviewing my 1st SF project and found a lot of things to change Tongue will see if I can incorporate your technique in this project. Will report back later today....



Thank!
By Edhy Rijo - 5/24/2010

Hmmmm, this is C# w00t, are you sending me some hidden message so I finally make the move to C#? BigGrin
By Greg McGuffey - 5/24/2010

The message was hidden! I didn't mean for it to hidden. Tongue



No, I just that Bill started this and he uses C#, so my brain was going that way. I could translate it to VB.NET, but not likely until next week. Hopefully you can see how things work (all just .NET stuff) and how extender providers can be really cool. BigGrin
By Russell Scott Brown - 5/24/2010

I wish everyone would just switch to C# and forget about it! BigGrin

It would be helpful to always post solutions in both C#/VB.NET but I know that isn't really practical and too time consuming.

By Edhy Rijo - 5/24/2010

Russell Scott Brown (05/24/2010)
I wish everyone would just switch to C# and forget about it! BigGrin




Agree, but too many things in my plate right now to start switching language Sad I feel pretty comfortable with VB and a decision factor to go with SF was the plenty VB sample code while IdeaBlade my 2nd choice have plenty C#, go figure!
By Greg McGuffey - 5/24/2010

I usually try to post examples in the preferred language of the original poster, which in this case was C#. I usually don't have time to do both, but I'll be sure that when I start doing official examples, I do it in both. Also, it is really, really good to work in both languages, IMHO.



My approach to learning C# was first to read C# examples. Pretty quickly you pick up on the essence of the language. The next step is to translate a small C# projects to VB (then you're going in known direction). Then I started doing samples in C#, things like this. Finally, I created a small app from scratch. I chose to do something that wasn't database intensive (or a data app at all) so I could focus on the language and log some time with it. By the time I was done with that, I was really comfy with C#.



This assumes you have any time to learn a new language. BTW, I was a total VB guy to start with, and it wasn't horrible to learn C#. The good news is that most of what we do is .NET/SF, so learning C# is just learning a language, which is considerably easier than learning a framework (or at least takes less time).



In any case, I hope the sample provides is helpful. BigGrin
By Russell Scott Brown - 5/24/2010

Actually it hasn't been too much of a problem. 

I was really just giving Edhy a hard time!Smile

I'm sure everyone on the forum appreciates Edhy's huge contributions! 

By Edhy Rijo - 5/24/2010

Russell Scott Brown (05/24/2010)
Actually it hasn't been too much of a problem.



I was really just giving Edhy a hard time!Smile




Greg / Russel,



Don't worry, not hard time at all. I am able to read and sometimes get deeper with C#, just that I am still fairly new too .Net and learning everyday and now focusing on getting my applications to rock and roll and not time to sit down and learn C#, I still need to start working on my first ASP.net application using SF of course so time will come for C# as I have expressed here.