C# add an image (instead of text) to ListViewItem - c#

I'm dynamically updating a ListView like that:
ListViewItem item = new ListViewItem();
item.Text = "Text1";
item.SubItems.Add("Text2");
item.SubItems.Add("Text3");
item.SubItems.Add("Text4");
item.Tag = i;
listView.Items.Add(item);
now I want that instead of Text4 will be a lil icon I will get dynamically from a url. I read a lot of threads and tried many things - but I can't get this to work..

You need to implement ImageList in your function.
Code :
// get picture resource
WebClient _web = new WebClient();
byte[] _data = _wb.DownloadData("http://www.myzony.com/usr/uploads/2017/03/3197402477.png");
MemoryStream _ms = new MemoryStream(_data);
// Loaded to imagelist
ImageList list = new ImageList();
list.Images.Add("pic1",Image.FromStream(_ms));
// bind listview
listView1.SmallImageList = list;
ListViewItem _item1 = new ListViewItem();
_item1.Text = "Test";
_item1.SubItems.Add("Test2");
_item1.SubItems.Add("pic1");
_item1.ImageKey = "pic1";
listView1.Items.Add(_item1);
Effect Image:

Related

How to make list view with Images with swipe?

I have an array of paths to images and I need to show this images. I see that the best way is to use smth like ListView and set as a source my images...
I found such solution https://stackoverflow.com/a/17382282/5709159
public DetailImageTraceForm()
{
InitializeComponent();
Text = FormTitle;
List<string> adress = new List<string>()
{
"http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-9_2351861k.jpg",
"http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-5_2351885k.jpg",
"http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-7_2351893k.jpg",
};
ImageList il = new ImageList();
DownloadImagesFromWeb(adress, il);
il.ImageSize = new Size(92, 92);
int count = 0;
Lv_images.LargeImageList = il;
List<string> names = new List<string>() { "1", "2", "3", "4" };
foreach (string s in names)
{
ListViewItem lst = new ListViewItem();
lst.Text = s;
lst.ImageIndex = count++;
Lv_images.Items.Add(lst);
}
}
private void DownloadImagesFromWeb(List<string> adress, ImageList il)
{
foreach (string img in adress)
{
System.Net.WebRequest request = System.Net.WebRequest.Create(img);
System.Net.WebResponse resp = request.GetResponse();
System.IO.Stream respStream = resp.GetResponseStream();
Bitmap bmp = new Bitmap(respStream);
respStream.Dispose();
il.Images.Add(bmp);
}
}
But I get such result
But actually what I need is : each image need to be alone and take the full screen(container) and something like arrows on sides like left(priv) right(next) and when user click next arrow, so next image swipe and replace current image.
Is there such out of the box solution in winforms or not?
I know that I can put ImageBox at my container and two buttons, but I hope that there is ListView solution because I would like to have kind of swipe animation between images...

ListView items don't appear in the right column

I am trying to create a ListView programmatically and fill it with a list of data that is determined previously from a file. I am trying to set up the column headers and populate the data with the following code but the result is that the data is misaligned by one column as can be seen in the attached image. How can I make it so that the data aligned in the correct columns?
ListView listView1 = new ListView();
ColumnHeader logCountHeader = new ColumnHeader();
logCountHeader.Text = "#";
logCountHeader.Width = 40;[![enter image description here][1]][1]
listView1.Columns.Add(logCountHeader);
ColumnHeader logTypeHeader = new ColumnHeader();
logTypeHeader.Text = "Log Type";
logTypeHeader.Width = 150;
listView1.Columns.Add(logTypeHeader);
ColumnHeader dataHeader = new ColumnHeader();
dataHeader.Text = "Message";
dataHeader.Width = 300;
listView1.Columns.Add(dataHeader);
ColumnHeader timeHeader = new ColumnHeader();
timeHeader.Text = "Time";
timeHeader.Width = 150;
listView1.Columns.Add(timeHeader);
string log_type_name = "";
string log_data = "";
string log_date = "";
string log_number = "";
for (int i = (_debug_entries-1); i >= 0; i--)
{
log_number = i.ToString();
log_type_name = _log_type[i].ToString();
log_data = _log_data[i];
log_date = _log_timestamp[i].ToString();
ListViewItem listviewitem = new ListViewItem();
listviewitem.SubItems.Add(log_number);
listviewitem.SubItems.Add(log_type_name);
listviewitem.SubItems.Add(log_data);
listviewitem.SubItems.Add(log_date);
listView1.Items.Add(listviewitem);
}
return listView1;
The item string should be part of the ListView constructor, it is not a sub item.
ListViewItem listviewitem = new ListViewItem(log_number);
listviewitem.SubItems.Add(log_type_name);
listviewitem.SubItems.Add(log_data);
listviewitem.SubItems.Add(log_date);
listView1.Items.Add(listviewitem);
And you need to set the ListView.View to see the subitems
ListView listView1 = new ListView();
listView1.View = View.Details;
for (int i =0; i <= _debug_entries-1; i++){
log_number = i.ToString();
log_type_name = _log_type[i].ToString();
log_data = _log_data[i];
log_date = _log_timestamp[i].ToString();
ListViewItem listviewitem = new ListViewItem();
listviewitem.SubItems.Add(log_number);
listviewitem.SubItems.Add(log_type_name);
listviewitem.SubItems.Add(log_data);
listviewitem.SubItems.Add(log_date);
listView1.Items.Add(listviewitem);
}
Use above code it will solve your problem.

Retreive stackpanel children from GridViewItem (UWP)

I'm wondering how I can get the contents of a stackpanel which has been added to a GridViewItem (YouTube data API, the stackpanel contains a bitmap image for a thumbnail, the title of the video and a hidden TextBlock with the video ID for reference).
To clarify, I need to be able to obtain the contents of the StackPanel that is contained in the GridViewItem (such as the string of that hidden TextBox) so I can use the data to display the correct video.
Here's the code I use to create the stackpanel.
public async Task SearchByKeyword(string searchTerm)
{
GeneralFunctions gf = new GeneralFunctions();
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = gf.YouTubeAPIKey,
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchTerm; // Replace with your search term.
searchListRequest.MaxResults = 50;
searchListRequest.SafeSearch = SearchResource.ListRequest.SafeSearchEnum.Strict;
// Call the search.list method to retrieve results matching the specified query term.
var searchListResponse = await searchListRequest.ExecuteAsync();
List<string> videos = new List<string>();
List<string> channels = new List<string>();
List<string> playlists = new List<string>();
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach (var searchResult in searchListResponse.Items)
{
switch (searchResult.Id.Kind)
{
case "youtube#video":
// Create a new StackPanel to insert as a ListViewItem
StackPanel sPanel = new StackPanel();
sPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
sPanel.VerticalAlignment = VerticalAlignment.Stretch;
sPanel.Orientation = Orientation.Vertical;
sPanel.Width = 160;
sPanel.Height = 160;
// Create new StackPanel "Child" elements with alignment and width
TextBlock tb1 = new TextBlock();
tb1.Text = searchResult.Snippet.Title;
tb1.TextWrapping = TextWrapping.WrapWholeWords;
tb1.Width = 160;
// Create new StackPanel "Child" elements with alignment and width
TextBlock tb2 = new TextBlock();
tb2.Text = searchResult.Snippet.ChannelId;
tb2.TextWrapping = TextWrapping.WrapWholeWords;
tb2.Width = 160;
// Create new StackPanel child element for a 120x120 thumbnail of the videos from the search results
Image image = new Image();
image.Source = new BitmapImage(new Uri(searchResult.Snippet.Thumbnails.Default__.Url, UriKind.Absolute));
// Add a "hidden" child element to each stackpanel to hold the video identity
TextBlock h1 = new TextBlock();
h1.Text = searchResult.Id.VideoId.ToString();
h1.Visibility = Visibility.Collapsed;
h1.TextWrapping = TextWrapping.WrapWholeWords;
sPanel.Children.Add(image);
sPanel.Children.Add(tb1);
sPanel.Children.Add(tb2);
sPanel.Children.Add(h1);
SearchResults.Items.Add(sPanel);
break;
case "youtube#channel":
//SearchResults.Items.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
break;
case "youtube#playlist":
//playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
break;
}
}
//Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos)));
//Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels)));
//Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists)));
}
Any help is appreciated.
Thanks
I guess I should never code tired right? The answer to my own question is actually very very simple
if (SearchResults.SelectedItems.Count > 0)
{
StackPanel sp = (StackPanel)SearchResults.SelectedItem;
TextBlock tb1 = (TextBlock)sp.Children[1];
TextBlock tb2 = (TextBlock)sp.Children[2];
TextBlock hf1 = (TextBlock)sp.Children[3];
}

How to retrieve the name of a Panorama-Item at runtime?

I am programmatically adding items to my Panorama Control called PanoramaCC.
//function to create the panorama items in our view
private void showPanorama(string panoramaName)
{
//create the panorama item and define it
PanoramaItem genItem = new PanoramaItem();
genItem.Height = 265;
genItem.Width = 440;
genItem.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(PanoramaItem_Tap);
genItem.Name = panoramaName;
//create the stackpanel for the panoramaitem
StackPanel genStack = new StackPanel();
genStack.Orientation = System.Windows.Controls.Orientation.Horizontal;
//margin to be done
genStack.Margin = new Thickness(0, -20, 0, 20);
//load the image
Image genImg = new Image();
genImg.Height = 220;
genImg.Width = 400;
genImg.Stretch = System.Windows.Media.Stretch.Fill;
genImg.Margin = new Thickness(20, 5, 20, 5);
string path = "Assets/AppGraphics/CreditCards/" + panoramaName.ToString() + "Front.png";
Uri uriR = new Uri(path, UriKind.Relative);
BitmapImage imgSource = new BitmapImage(uriR);
genImg.Source = imgSource;
//add image into stackpanel
genStack.Children.Add(genImg);
//add stackpanel to the panoramaitem
genItem.Content = genStack;
//add the panoramaitem to the panoramaview
this.PanoramaCC.Items.Add(genItem);
}
The issue I have is that during runtime I want to retrieve the name of the panoramaItem I am currently looking at and do something with it. I've managed to retrieve the name through the tap event for navigation purposes, string name = ((PanoramaItem)sender).Name; but this is a diffrent scenario. I want to retrieve the name and then delete the item with the corresponding name. Pressing a button should delete the currently selected panoramaItem, is what I'm trying to achieve.
You can get the current PanoramaItem by using the SelectedItem property. You don't need to get the name to delete it.
PanoramaItem currentItem = myPanorama.SelectedItem as PanoramaItem;
if(currentItem != null)
{
//if you want the name for other reasons
string name = currentItem.Name;
//Items returns an ItemsCollection object
myPanorama.Items.Remove(currentItem);
}

New TabItem at run time

Hi I am using TabControl to make Conference System, the private chats would be in a new TabItem. I am using this code to present a new member chatting in private:
TabItem ti = new TabItem();
ti.Header="Name";
MyTabControl.Items.Add(ti);
but the problem with this code is that I am adding a list box in the TabItem but the TabItem doesn't have a function to add an item in it. how can I add items into TabItem?
Second try: I used this code to present a new member chatting in private
ItemsControl it = new ItemsControl();
ListBox lst = new ListBox();
lst.Width = 571;
lst.Height = 301;
it.Items.Add(lst);
tabControlChat.Items.Add(it);
with this code I can add all items I need in the new tab. but the main problem is that I can't name the tab there is no property like (ti.Header) to name the tab. so what is the solution please? and thank you
For short:
ListBox lb = new ListBox();
lb.Items.Add("chat member");
TabItem ti = new TabItem();
ti.Header = "Private Chats";
ti.Content = lb;
TabControl tc = new TabControl();
tc.Items.Add(ti);
Use this:
TabItem ti = new TabItem();
ti.Header = "Name";
tabControl1.Items.Add(ti);
ListBox lst = new ListBox();
lst.Width = 571;
lst.Height = 301;
ti.Content = lst;

Categories

Resources