Solved

Populate ComboBox in Custome Ribbon

Posted on 2011-09-30
14
3,776 Views
Last Modified: 2013-11-10
How do I populate a combobox that is in a custom ribbon. When I try to use the .Items.Add() method (or whatever it's called) I get an error message that reads:

Cannot convert from 'object' to 'Microsoft.Office.Tools.Ribon.RibbonDropDown'.

I have included the code I have so far.

Any and all help is welcome!
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {   

            OracleConnection cs = new OracleConnection("User Id=MyUserID;Password=MyPassword;Data Source=OracleSID");
            OracleDataAdapter da = new OracleDataAdapter("SELECT JOB_CODE, JOB_NAME FROM JCJOB", cs);

            DataTable dt = new DataTable();

            da.Fill(dt);
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
		//Error is generated at this line!                
		Globals.Ribbons.Ribbon1.cmbProject.Items.Add(dt.Rows[i]["JOB_CODE"]);                
                
            }
        }

Open in new window

0
Comment
Question by:Gary Jones
  • 8
  • 6
14 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36896985
You would need to create a RibbonDropDown, and add instances of RibbonDropDownItems.

foreach (DataRow dr in dt.Rows)
{
     string jobCode = dr["JOB_CODE].ToString();
     dropDown.Items.Add(new RibbonDropDownItem() { Label = jobCode });                
}
0
 
LVL 1

Author Comment

by:Gary Jones
ID: 36897633
I am very new to c# so i am not sure what you mean by create a RibbonDropDown and add instanced of RibbonDropDownItems. Can I have an example of what you men?

Thanks in advance.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36897776
To create an instance of an object, you need to use the constructor (new keyword).  

This form might be a little too much for you, so let's rewrite it:

 
dropDown.Items.Add(new RibbonDropDownItem() { Label = jobCode });    

Open in new window

           

1) Construct a RibbonDropDown:

RibbonDropDown dropDown = new RibbonDropDown();

Open in new window


2) Loop through each of your data rows, using the enumerator, instead of an index variable (i in your case):

foreach (DataRow dr in dt.Rows)
{
}

Open in new window


3) Create RibbonDropDownItems for each row, and add to the RibbonDropDown.  Set the Label fro the drop-down item to the field value.

foreach (DataRow dr in dt.Rows)
{
    RibbonDropDownItem dropDownItem = new RibbonDropDownItem();
    dropDownItem.Label = dr["JOB_CODE].ToString();
}

Open in new window


4) Add the RibbonDropDown to the control:

Globals.Ribbons.Ribbon1.cmbProject.Items.Add(ribbonDropDown);

Open in new window

0
 
LVL 1

Author Comment

by:Gary Jones
ID: 36899555
I now get thefollowing errors:

Error      1      Cannot create an instance of the abstract class or interface 'Microsoft.Office.Tools.Ribbon.RibbonDropDown'

Error      2      Cannot create an instance of the abstract class or interface 'Microsoft.Office.Tools.Ribbon.RibbonDropDownItem'
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 36901067
I guess this is the perfect time to say that I really don't like the ribbon bar, so I don't have much experience with it...but, that doesn't mean that I am walking away from this.

Can you tell me if you feel comfortable trying to work with this (meaning:  can you work with it, and does it work)?

RibbonDropDownItem item 
      = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();

Open in new window

0
 
LVL 1

Author Comment

by:Gary Jones
ID: 36905553
TheLearnedOne, I was able to compile the RibbonDropDownItem item = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem(); statement, what is the next step?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36906024
Next step:

item.Label = dr["JOB_CODE].ToString();

Open in new window


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Gary Jones
ID: 36906247
The code in the "Next step" appears to have worked but now I think I must associate the item.Label to the cmbProject object. How do I add the values to the cmbProject "object" (not sure if that is the correct verbiage). Sorry, but I am extremely new to c# programming (no duh, right ;)).
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 125 total points
ID: 36906283
Next step is to create a RibbonDropDown:

RibbonDropDown dropDown = Globals.Factory.GetRibbonFactory().CreateRibbonDropDown(); 

Open in new window


0
 
LVL 1

Author Comment

by:Gary Jones
ID: 36906426
The RibbonDropDown (dropDown) has been created. I keep trying to add item.Label to the dropDown but I know I am still missing something as nothing is working. So, after the RibbonDropDown and RibbonDropDownItems have been created successfully, what is the next step?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36906461
Next step is to add the dropdown items to the dropdown:

dropDown.Items.Add(item);

Open in new window

0
 
LVL 1

Author Comment

by:Gary Jones
ID: 36906559
Error: Index must be within the bounds of the list. Parameter name: index. Not sure what that means but that is the error I get now.
0
 
LVL 1

Assisted Solution

by:Gary Jones
Gary Jones earned 0 total points
ID: 36906694
TheLearnedOne, I figured out where the syntax has to go, I did not put the RibbonDropDownItem statement in the foreach section of the code so only one dropdown item was getting created and I have multiple items to add. I have attached the final code that works.
foreach (DataRow dr in dt.Rows)
            {
                RibbonDropDownItem item = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
                
                item.Label = dr["JOB_CODE"].ToString();                
                Globals.Ribbons.Ribbon1.cmbProject.Items.Add(item);
            }

Open in new window

0
 
LVL 1

Author Closing Comment

by:Gary Jones
ID: 36935305
I had a difficult time following the code, I did not know where the code needed to go and was confused (the reason for B grade).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
which object should i use (efficiency) 5 47
Recommendation vb6 to vb.net or others 14 114
Remove greater than sign 3 49
scanning dentists xray (the small ones) 3 45
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now