This is a migrated thread and some comments may be shown as answers.

RadRibbonBar

1 Answer 117 Views
RibbonBar
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 22 Oct 2007, 07:54 AM
I am currently evaluating rad controls for WinForms and wonder if you have any examples of using the RadRibbonBar dynamically.  I am able to add controls to a RibbonBarChunk dynamically based upon a selection from a database, with no problem. However I am struggling  with how to interact with those newly added elements [gallery items].  By querying a database I add a ribbonbarchunk and then add galleryitems based upon the results of the query. I need now to add code which will perform a query on another database based upon the which galleryitem the user has clicked. I suppose my question is this. How do I write code for an event on a dynamically added galleryitem to a ribbonbarchunk? If you have any examples (preferably in VB) I would be most grateful.

Paul

1 Answer, 1 is accepted

Sort by
0
Kiril
Telerik team
answered on 23 Oct 2007, 01:29 PM
Hi Paul,

Thank you for your question.

You can achieve the behavior you're seeking to implement in the following way:
  1. Add a command tab
  2. Add a chunk in the command tab
  3. Add a GalleryElement
  4. Add a GalleryItem, together with data which is used to select the behavior (possibly index an array of delegates, as in the example below) which will be launched once an action is performed on the item (click, for example).
Private Delegate Sub MyDelegate()   
Private delegates As MyDelegate() = New MyDelegate(1) {}   
Private Sub AddItems()   
'declare a type for the delegate we'll be using   
'an array to hold the delegates implementing the different behaviors   
    'add the two delegates to the delegate list - the first shows   
    'a message box with the text "even", the second with the text "odd"   
    delegates(0) = New MyDelegate(AddressOf ConvertedAnonymousMethod1)   
    delegates(1) = New MyDelegate(AddressOf ConvertedAnonymousMethod1)   
      
    'initialize a new command tab and add it to the ribbon   
    Dim commandTab As New RadRibbonBarCommandTab()   
    radRibbonBar1.CommandTabs.Add(commandTab)   
      
    'initialize a new chunk and add it to the command rab   
    Dim chunk As New RadRibbonBarChunk()   
    chunk.Text = "bla bla bla"   
    commandTab.Items.Add(chunk)   
      
    'initialize a new tab item and add it to the command tab area   
    Dim tabItem As New TabItem()   
    tabItem.Text = "bbbbbb"   
    commandTab.Tab = tabItem   
      
    'add a new gallery element container to the chunk   
    Dim radGalleryElement1 As New RadGalleryElement()   
    chunk.Items.Add(radGalleryElement1)   
    For i As Integer = 0 To 3   
          
        'add items to the gallery container, dynamically   
        Dim radGalleryItem1 As New RadGalleryItem()   
        radGalleryItem1.Text = i.ToString()   
        'initialize the tag, which will be used to tell different gallery items apart   
        'and will be used to trigger the appropriate behavior in the click handler   
        'the tag can also be an object, containing the tag which can be used   
        'as an indexer in an array of delegates implementing the desired behavior   
        radGalleryItem1.Tag = i.ToString()   
        radGalleryElement1.Items.Add(radGalleryItem1)   
        'hooking up the dispatcher event handler   
        AddHandler radGalleryItem1.Click, AddressOf radGalleryItem1_Click   
    Next   
End Sub   
 
Private Sub radGalleryItem1_Click(ByVal sender As ObjectByVal e As EventArgs)   
    'use the tag to index in the array of delegates   
    Dim delegate1 As MyDelegate = delegates(Convert.ToInt16(DirectCast(sender, RadGalleryItem).Tag) Mod delegates.Length)   
      
    'start the delegate   
    delegate1()   
End Sub  

I'm not sure the way the above works will apply directly to your case. I think using the Tag of the sender to index an array of delegates is unsafe, as it is not strong-typed and you need to guarantee that the Tag is of the appropriate data type.

Still, I think the general idea of using an array of behaviors which you can select from according to the value of the Tag property is applicable.

Alternatively, to avoid the overhead of declaring separate delegates, you can have a conventional handler of the Click event, and in the handler implement code which executes different methods based on the value of the Tag property of the sender.

I hope this helps. Let me know if you have any further questions.

Sincerely yours,
Kiril
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
RibbonBar
Asked by
Paul
Top achievements
Rank 1
Answers by
Kiril
Telerik team
Share this question
or