Ribbon XML - get selected index or item ID in a comboBox control - combobox

I'm working on a comboBox XML Ribbon Control and I'm going crazy to obtain the index of the selected item.
This is the Ribbon XML code with the comboBox:
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon>
<tabs>
<tab id="SearchCustomerTab" insertAfterMso="TabAddIns" label="Cliente" visible="true">
<group id="SearchCustomerGroup" label="Cliente" autoScale="true">
<comboBox id="CustomerComboBox" getItemCount="GetItemCountCallback" getItemLabel="GetItemLabelCallback" getItemID="GetItemIDCallback" onChange="OnChangeCallback" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
With getItemCount and getItemLabel callback I correctly fill che comboBox (oTabCustomersList is a List of a custom class):
Public Function GetItemCountCallback(ByVal control As Office.IRibbonControl) As Integer
Return oTabCustomersList.Count
End Function
Public Function GetItemLabelCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
Return oTabCustomersList(index).NomeCompleto
End Function
With getItemId callback I set the index of every item in the ID:
Public Function GetItemIDCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
Return index.ToString
End Function
but with onChange callback I can obtain the item label but not the ID or the selected index:
Public Sub OnChangeCallback(ByVal control As Office.IRibbonControl, text As String)
Debug.WriteLine("OnChangeCallback text: " & text) 'text = item label
End Sub
Is there a way to obtain the index of the selected item with the Ribbon comboBox control?
Thanks in advance,
Simone

Unfortunately it's not possibile to get the index of the selection in a ribbon comboBox (source)
Whenever the value of the combo box is selected, the onChange callback
receives the text. However, it is not possible to get the index of the
selection.
I've solved using a Dictionary (of String, CustomClass) where string is the text parameter of OnChangeCallback:
Private customClass As CustomClass
Private customDictionary As Dictionary(Of String, CustomClass)
Public Sub Ribbon_Load(ByVal ribbonUI As Office.IRibbonUI)
Dim customList As List(Of CustomClass)
customList = FunctionToPopulateMyList()
customDictionary = customList.ToDictionary(Function(p) p.MyText, Function(p) p)
End Sub
Public Function GetItemLabelCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
Return oCustomDictionary.ElementAt(index).Value.MyText
End Function
Public Function GetItemCountCallback(ByVal control As Office.IRibbonControl) As Integer
Return oCustomDictionary.Count
End Function
Public Function GetItemIDCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
Return "Item" & index.ToString & control.Id
End Function
Public Sub OnChangeCallback(ByVal control As Office.IRibbonControl, text As String)
If (customDictionary.ContainsKey(text)) Then
customClass = customDictionary(text)
End If
End Function

Related

How to pass value from one window to another

How do I pass a value from one a variable to a textbox after it's set? On winforms, I used to use form1.textbox1.text = variable in winforms.
I set, and get the variable from this...
Public Shared Property containerstring() As String
Get
Return m_containerstring
End Get
Set(value As String)
m_containerstring = value
End Set
End Property
Private Shared m_containerstring As String
Basically, I have a window... where the user chooses a variable, this variable is then set # containerstring. When this form closes, I wanted to push this variable to the currently open window's textbox.
I'm new to WPF, forgive the noobness.
This is how I do it for a window, and this works perfectly... for windows. I'm looking to do the same thing with a control.
Dim strWindowToLookFor As String = GetType(MainWindow).Name
Dim win = ( _
From w In Application.Current.Windows _
Where DirectCast(w, Window).GetType.Name = strWindowToLookFor _
Select w _
).FirstOrDefault
If win IsNot Nothing Then
DirectCast(win, MainWindow).Title = SelectedContainer
End If
You can make a Window closing Event like :
this.Closed += MyWindow_Closed;
and then set your variable in the corresponding method.
private void MyWindow_Closed()
{
TextBox1.Text = a;
}
You could use a PubSubEvent which is available in Prism.Events. This will allow you to subscribe to events.
Using Prism.Events;
define your Event.
public class MyEvents : PubSubEvent<object>
{
public MyEvents();
}
In your first window or code behind
[Import]
public IEventAggregator EventAggregator
{
get;
set;
}
and you can use this property in your program to send whatever value you want to send.
For example
private void MyWindow_Closed()
{
MyEvents myEvents = EventAggregator.GetEvent<MyEvents>();
myEvents.Publish(yourvalue);
}
Once you have publised you can Subscribe to the same event in any other part of your program like this.
MyEvents myEvents = EventAggregator.GetEvent<MyEvents>();
myEvents.Subscribe(MyEventMethod, ThreadOption.UIThread, true);
and get your data here
void MyEventMethod(object obj)
{
// do wharever you want
}

How do I set values for the entries in a Windows Forms ComboBox?

I want to have a drop down list with 12 choices.
I found that ComboBox is what I need (if there is a better control kindly tell me).
I dragged and drop a combo box into a panel using VS2012 and then clicked on the left arrow that appears on the combo box. The following wizard shows:
As you can see, I am just able to type the name of the choice but not the value of it.
My question is how to get the value of these choices?
What I have tried
I built an array with the same length as the choices, so when the user selects any choice, I get the position of that choice and get the value from that array.
Is there a better way?
You need to use a datatable and then select the value from that.
eg)
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Description", typeof(string));
dt.Load(reader);
//Setting Values
combobox.ValueMember = "ID";
combobox.DisplayMember = "Description";
combobox.SelectedValue = "ID";
combobox.DataSource = dt;
You can then populate your datatable using:
dt.Rows.Add("1","ComboxDisplay");
Here, the DisplayMember(The dropdown list items) are the Descriptions and the Value is the ID.
You need to include a 'SelectedIndexChanged' Event on your combobox (If using VS then double click the control in Design Mode) to get the new values. Something like:
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int ID = Combobox.ValueMember;
string Description = ComboBox.DisplayMember.ToString();
}
You can then use the variables in the rest of your code.
You cannot use the wizard to store values and text. To store DisplayText/Value pair the combobox needs to be connected to some data.
public class ComboboxItem
{
public string DisplayText { get; set; }
public int Value { get; set; }
}
There are two properties on the combobox - DisplayMember and ValueMember. We use these to tell the combobox that - show whats in DisplayMember and the actual value is in ValueMember.
private void DataBind()
{
comboBox1.DisplayMember = "DisplayText";
comboBox1.ValueMember = "Value";
ComboboxItem item = new ComboboxItem();
item.DisplayText = "Item1";
item.Value = 1;
comboBox1.Items.Add(item);
}
To get the value -
int selectedValue = (int)comboBox1.SelectedValue;

Java JDialog: How to return item (object) selected from a combobox

I have created a jdialog which contains a combobox (see
photo
).
The combobox contains items of Delivery class.
The date you see in the photo is the value returns from Delivery.toString().
When i press OK, i need to return the Delivery object that is selected from the combobox, to the parent form.
You could use something along the lines of:
public BillatoDialog extends JDialog(){
private Delivery selectedDelivery; // Declare selectedDelivery variable
String selectedDate = comboBox.getSelectedItem(); // Returns the current selected item.
for (Delivery currentDelivery: deliveryList){ // Loop over a list with all the deliverys.
if (currentDelivery.toString()==selectedDate;){ // and break the for when match found.
selectedDelivery = currentDelivery; // assign it to selectedDelivery
break;
}
}
public Delivery getSelectedDelivery(){
return selectedDelivery;
}
}
And then in your JPanel
public BillatoPanel extends JPanel(){
openBillatoDialog();
getSelectedDelivery();
}

Label with different font sizes

Basically I want to achieve something like this:
but I have no idea how to do it, I tried with 2 labels to combine them but the result isn't that great..
You need to draw the text from a different point of view, namely, the baseline:
Public Class MyLabel
Inherits Label
<Browsable(False)> _
Public Overrides Property AutoSize As Boolean
Get
Return False
End Get
Set(value As Boolean)
'MyBase.AutoSize = value
End Set
End Property
Protected Overrides Sub OnPaint(e As PaintEventArgs)
'MyBase.OnPaint(e)
Dim fromLine As Integer = Me.ClientSize.Height * 0.75
Dim g As Graphics = e.Graphics
Dim fontParts() As String = Me.Text.Split(".")
Using bigFont As New Font(Me.Font.FontFamily, 20)
TextRenderer.DrawText(g, fontParts(0), bigFont, _
New Point(0, fromLine - GetBaseLine(bigFont, g)), _
Me.ForeColor, Color.Empty)
If fontParts.Length > 1 Then
Dim bigWidth As Integer = TextRenderer.MeasureText(g, fontParts(0), bigFont, _
Point.Empty, TextFormatFlags.NoPadding).Width
Using smallFont As New Font(Me.Font.FontFamily, 8)
TextRenderer.DrawText(g, "." & fontParts(1), smallFont, _
New Point(bigWidth + 3, fromLine - GetBaseLine(smallFont, g)), _
Me.ForeColor, Color.Empty)
End Using
End If
End Using
End Sub
Private Function GetBaseLine(fromFont As Font, g As Graphics) As Single
Dim fontHeight As Single = fromFont.GetHeight(g)
Dim lineSpacing As Single = fromFont.FontFamily.GetLineSpacing(fromFont.Style)
Dim cellAscent As Single = fromFont.FontFamily.GetCellAscent(fromFont.Style)
Return fontHeight * cellAscent / lineSpacing
End Function
End Class
The code basically measures the height of the font from a line. In my example, I used the bottom 25% of the Label's client space to say, start drawing from this line: Me.ClientSize.Height * 0.75.
For each font you use, you would have to measure that font's baseline and subtract that from your drawing line in order to offset your drawing position of the text.
Measuring an individual character's dimensions is not easy due to aliasing and glyph overhangs. I added a small padding between the big text and the small text: bigWidth + 3 to try to make it look good. If the big number ends in a 7, the distance looks a little off because the stem of the 7 is angled.
Result:
Create a new class inherited from Label, and override the void OnPaint(PaintEventArgs e) method to change the default rendering behavior:
public class MyLabel : Label
{
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawString("A", Font, new SolidBrush(ForeColor), 10, 10);
e.Graphics.DrawString("B", new Font(Font.FontFamily, 20), new SolidBrush(ForeColor), 50, 10);
}
}
As a result, "B" will be two times larger the "A". You can achieve your goal in the same way, but you have to calculate the position of your sub-strings ("145", ".", "54") and draw them.
Use devexpress LabelControl.AllowHtmlString property to true and use the supported <size> tag within the LabelControl's Text property as detailed in the HTML Text Formatting documentation.
you can use user control WPF in windows form. to do that do this step.
1. add user control to the windows form
2.from xml of usercontrol name grid like t1
3. add this function to the usercontrol.wpf.cs
public void Actor(string text)
{
StringBuilder sb = new StringBuilder();
sb.Append(#"<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> ");
sb.Append(text);
sb.Append(#"</TextBlock>");
TextBlock myButton = (TextBlock)XamlReader.Parse(sb.ToString());
this.t1.Children.Clear();
t1.Children.Add(myButton);
}
4. after that from form1.css add this function in every where you want.
userControl11.Actor("<Run Text='Hi ' FontWeight='Bold'/><Run Text='Hitler ' FontWeight='Bold'/>");
userControl11.Actor(" < Run FontWeight = 'Bold' FontSize = '14' Text = 'This is WPF TextBlock Example. ' />");
you can mange the write code "" of Actor function by using xml wpf.

et Return the Whole Subdocument Arrary from MongoDB

I need to get all of the subdocuments array from the Courses Class where the User.UserId = whatever and Courses.Status=active
Public Class User
Public Property UserId As String 'this is unique so i would like to index this, unless you think otherwise
Public Property CourseData() As List(Of Courses) '
Public Property Groups As List(Of String)
Public Property BU As List(Of String)
End Class
Public Class Courses
Public Property id As String 'this can be dynamic
Public Property description As String
Public Property CompletionDate As String
Public Property Hours As String
Public Property Status As String
End Class
Using vb.net , I tried a few ways, I only want the courses returned that have a Status="Active" to be dumped into Ienumberable
I tried (_users is a collection of User) (_uid is a variable passed into it)
Return _users.FindAs(Of User)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid))))
Return _users.FindAs(Of User)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid)))).SetFields("Courses", "1")
Return _users.FindAs(Of Courses)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid))))
Return _users.FindAs(Of Courses)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid)))).SetFields("Courses", "1")
None seem to work, they usually come back with the fields from Class User or both Class User and Class Course, but the Course fields are blank
I even am trying linq.. this works - but only returns 1 row result
Dim uc = From _u In _users.AsQueryable(Of User)()
Where _u.userid = _userid _
Select _
CourseID = _u.Courses.Where(Function(c) c.State = "Submitted").Select(Function(c) c.CourseId)(0), _
CourseDescription = _u.Courses.Where(Function(c) c.State = "Submitted").Select(Function(c) c.CourseDescription)(0)
Seems easy enough to do, just cant get it
Got It, I think I was over thinking it
Once I declare an instance of the class, I can iterate through the subdocument
Dim _u as new User
For Each c In _user.Courses.Where(Function(cs) cs.Status= "Active").Select(Function(cs) cs)
console.writeline(c.id & c.description & "so on...")
Next

Resources