Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

listvew to combobox

Hi,
i need help.
How to when select item from listview from Form1 and click button, to open it Form2 and focus that item to combobox.

Thank you in advance !
You will need to pass a reference to the listview item collection to the form2 instance (for example through the constructor).
(Jan 6, 2018 05:08 AM)Ninja Wrote:  You will need to pass a reference to the listview item collection to the form2 instance (for example through the constructor).

Which code for example ?
(Jan 6, 2018 10:37 AM)PeraPeric Wrote:  Which code for example ?

Put the selected item in a variable, for example like this:

Code
string item = listView.SelectedItems[0].Text;

Then you can pass it to the other form using a constructor, wich you could approach like this

Code
private string itemForm1;
public Form2(string selected item)
{
    itemForm1 = item;
    InitializeComponent();  
}

Then all you have to do is add the string itemForm1 to the combobox.
(Jan 6, 2018 11:04 AM)Tommy Chong- Wrote:  Put the selected item in a variable, for example like this:

Code
string item = listView.SelectedItems[0].Text;

Then you can pass it to the other form using a constructor, wich you could approach like this

Code
private string itemForm1;
public Form2(string selected item)
{
   itemForm1 = item;
   InitializeComponent();  
}

Then all you have to do is add the string itemForm1 to the combobox.

It is best to send the item not as a string but as its original type, as all a combobox does is call the ToString method on it.
Something doesn't work.

This is database table:

CREATE TABLE [dbo].[Klijent] (
    [klijentId]      INT          IDENTITY (1, 1) NOT NULL,
    [naziv_klijenta] VARCHAR (50) NOT NULL,
    [telefon]        CHAR (15)    NOT NULL,
    PRIMARY KEY CLUSTERED ([klijentId] ASC)
);

Code Form1: 

private void buttonPrikazi_Click(object sender, RoutedEventArgs e)
        {          
            PostojeciKorisnik korisnik = new PostojeciKorisnik();
            korisnik.itemForm1 = listViewPregled.SelectedItem.ToString();
            korisnik.Show();
        }

Code Form2:

public string itemForm1;

        public PostojeciKorisnik(string item)
        {
            InitializeComponent();
            this.itemForm1 = item;
        }

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            comboBoxKlijent.Items.Add(itemForm1);
        }
If you want to focus on combobox there's .focus method so put in on the on_load of your form2.
[Image: signaturetwo.png]
@OP, pass a reference to the collection to the second form, then bind it as the datasource to the combobox, then focus on the item you need.
This really depends on what you have for your data sources and how you populate the listview and combobox. It's not the best idea to expose more than you need through any kind of references being passed around. If your collection is the same between the two UI components too, you could probably just get away with passing an int for the index to focus... If you need to modify that item, then you need a reference of some sort or some kind of callback. Many ways to do it but this is not enough information to provide a solution towards the best approach.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Where you say something doesn't work, what actually isn't working??
- Valeas quam optime. -


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by adenfrMar 13, 2017 02:17 PM 6 202 Mar 13, 2017 02:17 PM
Last Post: adenfr
[.NET] by GørillaFeb 20, 2017 01:30 PM 13 343 Feb 20, 2017 01:30 PM
Last Post: Gørilla
[C#] by DucaRiiDec 13, 2016 09:54 AM 10 265 Dec 13, 2016 09:54 AM
Last Post: newmecha