5 Replies - 529 Views - Last Post: 23 January 2013 - 10:04 AM Rate Topic: -----

#1 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

ListView1 Subitem[3] - Copy to ListView2 Subitem[3] - how?

Posted 23 January 2013 - 08:57 AM

Hi,

I have a Windows forms project with 2 tabs. Both tabs have a ListView control. The first tab shows the contents of one folder (Checked Out) and the other contains the contents of another folder (checked in).

Despite my limited knowledge of c# (still learning) i've managed to make both ListView controls behave pretty much as i want them too.

However, what i need to be able to do, is when i click the 'Check Out' button on tab 1, the selected files in the listView1 control are removed (along with the file in the checked in folder being moved to the checked out folder). Then the listView2 in the second tab should display the files that were moved. This, somewhat surprisingly, i have managed to do with some success.

My first problem is that the listview2 in the second tab doesn't automatically refresh - so i effectively have to close the program and re-open it in order to see the updated listView2 with the new files.

My second problem (and this has been doing my head in for hours) is this: I have 4 columns in my ListViews (name, size, date modified and notes). The first 3 colums come accross into my listView2 control without problem (presumably because the data is stored in the files themselves). However, what i need is for the 'Notes' data which is stored in column 4 (index 3) in listView1 to come accross into listView2 when the data moves accross.

Any pointers would be greatly appreciated.

Please see my code (some previous attempts have been commented out):

// ListView1
            string[] checkedINfileList = Directory.GetFiles("O:\\TestDaws\\CSDB\\CheckedIN");


            foreach (string file in checkedINfileList)
            {
                FileInfo f = new FileInfo(file);

                long fileSize = f.Length;
                DateTime file_date = f.LastWriteTime;
                string fileName = Path.GetFileName(file);

                ListViewItem item = new ListViewItem(fileName);

                listView1.Items.Add(item);
                item.SubItems.Add(fileSize.ToString() + " Kb");
                item.SubItems.Add(file_date.ToString());

            }
            //ListView2
            string[] checkedOUTfileList = Directory.GetFiles("O:\\TestDaws\\CSDB\\CheckedOUT");


            foreach (string file in checkedOUTfileList)
            {
                FileInfo f = new FileInfo(file);

                long fileSize = f.Length;
                DateTime file_date = f.LastWriteTime;
                string fileName = Path.GetFileName(file);

                ListViewItem item2 = new ListViewItem(fileName);

                listView2.Items.Add(item2);
                item2.SubItems.Add(fileSize.ToString() + " Kb");
                item2.SubItems.Add(file_date.ToString());



                //NEED TO ADD THE VALUE STORED IN THE 4TH COLUMN (POS 3) OF LISTVIEW1 IN HERE
                //THE 4TH COLUMN IS POPULATED FURTHER DOWN THE CODE WITH THE BUTTON_2 CLICK EVEN HANDLER

                
                
            }
             
        }


       //Check Out Button Click
        public ListView.SelectedListViewItemCollection mySelectedItems;

        private void button1_Click(object sender, EventArgs e)
        {
            mySelectedItems = listView1.SelectedItems;

            string toDir ="O:\\TestDaws\\CSDB\\CheckedOUT";
            string fromDir = "O:\\TestDaws\\CSDB\\CheckedIN";

            if (mySelectedItems.Count > 0)
                
            {
                foreach (ListViewItem items in mySelectedItems)
                {
                    try
                    {
                        File.Move(fromDir + "\\" + items.Text, toDir + "\\" + items.Text);
                        MessageBox.Show("Checking Out: \n" + items.Text);
                        listView2.Refresh();

                    

                        //for (int i = 0; i < listView1.Items.Count; i++)
                        //{

                        //    ListViewItem newList = new ListViewItem();

                        //    if (listView1.Items[i].SubItems[3].ToString() != null)
                        //    {
                        //        string test = listView1.Items[i].SubItems[3].ToString();
                                
                        //        newList.SubItems.Add(test);
                        //    }
                           
                        //}

                        listView1.Items.Remove(items);
                        

                    }
                    catch (Exception ex)
                        {
                        MessageBox.Show("Error" + ex);
                        }
                }
                
            }

        }

       
        //Add Note Button Click
        public void button2_Click(object sender, EventArgs e)
        {
            mySelectedItems = listView1.SelectedItems;
            

            if (textBox1.Text != null && mySelectedItems.Count > 0)
            
                try
                {
                    foreach (ListViewItem itemsForNote in mySelectedItems)
                        itemsForNote.SubItems.Add(textBox1.Text);
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex);
                }

                else
                {
                    MessageBox.Show("Please select DMs and write note in the box", "Forgotton Something?");
                }
                
            
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            
        }

        //Remove Note
        private void removeNoteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            
            try
                {
                    foreach (ListViewItem myItem in mySelectedItems)
                        myItem.SubItems.Remove(myItem.SubItems[3]);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex);
                }


Is This A Good Question/Topic? 0
  • +

Replies To: ListView1 Subitem[3] - Copy to ListView2 Subitem[3] - how?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: ListView1 Subitem[3] - Copy to ListView2 Subitem[3] - how?

Posted 23 January 2013 - 09:13 AM

A change I'd make is to NOT move the file *and* add to the listview.

If you used FileSystemWatcher to monitor the folders then your program could react to the addition or remove of the folder on its own. This way a file could come in from ANYPLACE not just your program and it would be available in your program. For example if there were 3 computers monitoring this library it wouldn't matter which one checked in the file and moved it, the others would still see the file appear in a directory and update their GUI.

Next: For the sake of your own sanity, name your controls as soon as you drop them on a form. TextBox1 and Button4 sucks!

Next: Get used to breaking down the purposes of your methods a bit more. You have the moving of the file and the addition of that file to the listview in one method. That means if you want to just add to the listview elsewhere you have to duplicate code.

bool MoveFile(string form, string to)
{
     // Move code here
}

bool CheckInFile(string FilePath)
{
    // Check int he file here
}

Was This Post Helpful? 1
  • +
  • -

#3 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: ListView1 Subitem[3] - Copy to ListView2 Subitem[3] - how?

Posted 23 January 2013 - 09:38 AM

Thanks for the tips, they're appreciated. I'm learning mainly by playing around and Googling problems, which makes for slow progress.

I'm still unsure on how to copy column 4 ('notes' in this case) from listView1 on the first tab to listView2 on the second. And where i should put the code for it.

I'll work on implementing your suggestions, but if you could also point me in the right direction with the above that would be great.

Cheers,
Jake
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: ListView1 Subitem[3] - Copy to ListView2 Subitem[3] - how?

Posted 23 January 2013 - 09:51 AM

View PostDaedalusAero, on 23 January 2013 - 10:38 AM, said:

I'm learning mainly by playing around and Googling problems, which makes for slow progress.


I beg you to stop. There are so many good resources out there - there is no reason to make each every little thing you do a major headache.


My standard beginner resources post - Updated DEC 2012

You can try to learn C# by dismantling snippets and googling terms - basically you can take a hit-n-miss, shotgun approach. Or you could just follow a proper training program whether it be a couple intro books, school, on-line course or series of tutorials like the ones we offer. I'll tell you from experience that just fumbling around in the dark and trying to teach yourself with no guidance doesn't work. Its like stumbling across a Harrier Jump Jet and trying to teach yourself how to fly with no background in piloting: You simply lack any groundwork to start from. How can you lay out your own training course if you don't already know the material? Would you go to a university where the teacher says "I don't know any of this but we'll fumble through it together?"

We have a tutorials section and a learning C# series of articles.

First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover. Do a couple hundred on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.

There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Oct 2012
Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 DaedalusAero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-July 12

Re: ListView1 Subitem[3] - Copy to ListView2 Subitem[3] - how?

Posted 23 January 2013 - 10:02 AM

Yeah i hear you. I have Head First C# which i pick up when i can and that's where i've learned the fundamentals. I'm very much hands on when it comes to learning, and i always learn much more through trial and error than by simply reading alone. I haven't taken a complete scatter gun approach i assure you.

I'll check out your tutorials though.

But in the mean time, any chance of a pointer regarding my index 3 subitem? :)
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: ListView1 Subitem[3] - Copy to ListView2 Subitem[3] - how?

Posted 23 January 2013 - 10:04 AM

I'm not sure why you would be having issues with the 4th column.
If you can copy the other three, and you seem to understand that columns are just .subitem of the ListView item... The 4th column is just another subitem.

Also, we try to alway avoid code duplication.
do you notice how your checkin and checkout are all but identical except for the choice of ListView control? If you wanted to change the look of the listviews you would have to make sure you updated both sections the same. Not a good maintenance design.

But you could have a single method that adds your entry...

void AddEntry(string file, ListView InOrOut)
{
                FileInfo f = new FileInfo(file);

                long fileSize = f.Length;
                DateTime file_date = f.LastWriteTime;
                string fileName = Path.GetFileName(file);

                ListViewItem item = new ListViewItem(fileName);

                InOrOut.Items.Add(item);
                item.SubItems.Add(fileSize.ToString() + " Kb");
                item.SubItems.Add(file_date.ToString());
}


Now your checkin/out routines get simplified

            string[] checkedINfileList = Directory.GetFiles("O:\\TestDaws\\CSDB\\CheckedIN");

            foreach (string file in checkedINfileList)
            {
                AddEntry(file, ListViewIN);
            }
            

            string[] checkedOUTfileList = Directory.GetFiles("O:\\TestDaws\\CSDB\\CheckedOUT");


            foreach (string file in checkedOUTfileList)
            {
                AddEntry(file, ListViewOUT);
            }
             
        }


This why I often tell new coders they need to work through books and tutorials more before they try to jump in with both feet by trying to design a program from scratch. You simply haven't seen enough examples of the simple foundation concepts like this to be ready to build a program from scratch.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1