4 Replies - 4929 Views - Last Post: 11 October 2013 - 04:54 AM Rate Topic: -----

#1 alexharvey52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-October 13

Split ListBox.SelectedItem String

Posted 11 October 2013 - 03:45 AM

I am trying to find the Event Subject value from the selected Item in a WPF list box that uses binding data. I currently have it so it displays the selected item string however it displays everything about that selected Item. I need that string to be split up into each individual variables (Levent, Lsubject, Lbody). What route should I take into doing this? I considered using a trim however that could get complicated as the strings can changed size massively.

string curItem = LstEvents.SelectedValue.ToString();
MessageBox.Show(curItem);


Displays:

{ LBackground = #99FFFFFF, Levent = 6569, Lsubject = Holiday, Lbody = Holiday }


Here is my Listbox XAML if you also need that:

     <ListBox x:Name="LstEvents" SelectedValue="{Binding Path=Lsubject}" HorizontalAlignment="Left" Margin="10,228,0,21" Width="524" BorderBrush="Black" BorderThickness="2,2,1,1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Canvas x:Name="EventItem" HorizontalAlignment="Left" Height="76" VerticalAlignment="Top" Width="502" Background="{Binding Path=LBackground}">
                            <Label Content="Event:" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="97" FontWeight="Bold" FontSize="14" Canvas.Left="1" Canvas.Top="2"/>
                            <Label Content="Subject:" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="97" FontWeight="Bold" FontSize="14" Canvas.Left="1" Canvas.Top="26"/>
                            <Label Content="Body:" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="97" FontWeight="Bold" FontSize="14" Canvas.Left="1" Canvas.Top="49"/>
                            <Button x:Name="BtnSelectEvent" Content="Select" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="74" Background="#FF333333" Foreground="White" FontWeight="Normal" FontSize="14" Canvas.Left="418" Canvas.Top="24" Click="BtnSelectevent_Click"/>
                            <Label Content="{Binding Path=Levent}" Height="25" Canvas.Left="71" Canvas.Top="4" Width="107"/>
                            <Label Content="{Binding Path=Lsubject}" Height="25" Canvas.Left="71" Canvas.Top="28" Width="107"/>
                            <Label Content="{Binding Path=Lbody}" Height="25" Canvas.Left="71" Canvas.Top="53" Width="107"/>
                        </Canvas>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>


Is This A Good Question/Topic? 0
  • +

Replies To: Split ListBox.SelectedItem String

#2 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: Split ListBox.SelectedItem String

Posted 11 October 2013 - 04:15 AM

There's a String.Split() method that lets you split a string into an array of strings by a separating character. However, if each item is an object of a class, you could try overriding the ToString() method for that class and see what happens....

[Note: I see you ignored my suggestion and stuck with a canvas instead of a resizeable layout panel. For shame...]

This post has been edited by MrShoes: 11 October 2013 - 04:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimzcoder   User is offline

  • D.I.C Regular

Reputation: 57
  • View blog
  • Posts: 351
  • Joined: 14-November 12

Re: Split ListBox.SelectedItem String

Posted 11 October 2013 - 04:33 AM

just to add on what MrShoes said. you can use the existing ',' commas and '=' equal as sign as your delimiters. Obviously the first set of splitted string will be your variable and the second set will be your values.

you can get values of each variable on the second set will be retrieved using the corresponding indeces of your first set of string array.

hope that helped.
happy coding.
Was This Post Helpful? 1
  • +
  • -

#4 alexharvey52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-October 13

Re: Split ListBox.SelectedItem String

Posted 11 October 2013 - 04:38 AM

I have managed to solve the problem by Indexing the string by like you said using existing signs in the string. If I want to find the Levent value I do:

string curItem = LstEvents.SelectedValue.ToString();
int evNoIndex = curItem.IndexOf("Levent = ");
string evNoString = curItem.Substring(evNoIndex + 9);
int commaIndex = evNoString.IndexOf(',');
evNoString = evNoString.Substring(0, commaIndex);
MessageBox.Show(evNoString);


Although the code is chunky it seems to work great.

[P.S MrShoes I went with the Canvas Method because for what we want to do it seems to work the best way. I tried the other methods and they worked but caused a few other problems with some other methods.]

Once again thanks for your help guys! :)
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 24,003
  • Joined: 05-May 12

Re: Split ListBox.SelectedItem String

Posted 11 October 2013 - 04:54 AM

I'll ask the obvious question. You are in a object oriented environment. Why not use the class properties like a modern C# programmer instead of doing string manipulation like an old school PHP scripter?

Assuming the list contains MailEvent objects.
MailEvent curItem = (MailEvent) LstEvents.SelectedValue;
MessageBox.Show(curItem.Levent);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1