Search code examples
c#apitwittertweetinvi

How would I pass the selected user string from a listbox to Tweetinvi?


Some of Tweetinvi's functions, like UnFollowUser, accepts an argument to know which user is the one to unfollow, etc. The problem is the user list can only return a string and I can't convert that to something its IUser interface can use.

The code I'm using to get the selected user is this:

userList.Click += (s0, e0) =>
{
    var selected = userList.SelectedItem;

    selectedUser = selected.ToString();
    Console.WriteLine(selectedUser);
};

I couldn't find anything on the project's discussion pages that could help me (or anyone trying to do the same thing). How would I go about doing this?


Solution

  • A userlist does not have to store string. It can store items and you can select to show the user name of the items it stores.

    Is your project in WPF or Winform?

    Otherwise to get a user from the screen name you can do :

    var user = User.GetUserFromScreenName("USER_SCREEN_NAME")
    

    WPF Example: MainWindow.xaml.cs

    var u1 = User.GetUserFromScreenName("hi");
    var u2 = User.GetUserFromScreenName("hi2");
    
    UsersListBox.Items.Add(u1);
    UsersListBox.Items.Add(u2);
    UsersListBox.SelectionChanged += (sender, args) =>
    {
        SelectedUser = (User)UsersListBox.SelectedItems[0];
        // Code logic...
    };
    

    MainWindow.xaml

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox x:Name="UsersListBox" DisplayMemberPath="Name" SelectionMode="Single" />
    </Grid>
    </Window>