Form Controls Not Updating

dr-snipe

Member
Joined
Jan 25, 2012
Messages
7
Programming Experience
3-5
Hey guys I got quite a strange problem:
I created a form in #Develop that looks like so:
10x4ojs.png


As you can see, there is a listView there. My goal is to load up contents from a file and display them into that listview. I do that, no errors or messages and then create a new listview with all of the data inside of it, but when I replace it, nothing happens. This is the form after it has loaded up and created the new listView:
723x1y.png



Here is the code for the manage window:
C#:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FeedMe
{
 public partial class Manage : Form
 {
  public Manage()
  {    
   InitializeComponent();
  }
  
  void ManageLoad(object sender, EventArgs e)
  {      
   this.listView = new Utility().getFeedList();
   this.Refresh();
   this.Update();
  }
  
  void Button3Click(object sender, EventArgs e)
  {
   // save, then
   this.Close();
  }
 }
}

the ManageLoad function is being called upon startup and I have tested it and it does execute that function when the form is opened. The getFeedList function is as follows:
C#:
StreamReader sr = new StreamReader(Stream.Null);
  ListView listView = new ListView();
  int feedCount = 0;
  
  string[] feedNames = new string[feedCount+1];
  string[] feedLinks = new string[feedCount+1];
  string[] feedImgs = new string[feedCount+1];
  
  try{
   sr = new StreamReader(".\\resources\\feeds.cfg");
  } catch (Exception e) {
   MessageBox.Show("Could not open feeds.cfg. Please make sure that it is available. You can create a new one using Feeds > Manage");
   new Logger().write("Could not open feeds.cfg: "+e.ToString());
   return new ListView();
  }
  
  string line = string.Empty;
  
  while((line = sr.ReadLine()) != null)
  {
   try{
    string newFeedName = line.Substring(0, line.IndexOf(' '));
    line = line.Substring(line.IndexOf(' ')+1);
    
    string newFeedLink = line.Substring(0, line.IndexOf(' '));
    line = line.Substring(line.IndexOf(' ')+1);
    
    string newFeedImg = line;
    
    try{
     feedNames[feedCount] = newFeedName;
     feedLinks[feedCount] = newFeedLink;
     feedImgs[feedCount] = newFeedImg;
     
     feedCount++;
     
     string[] new_feedNames = new string[feedCount+1];
     string[] new_feedLinks = new string[feedCount+1];
     string[] new_feedImgs = new string[feedCount+1];
     
     feedNames = new_feedNames;
     feedLinks = new_feedLinks;
     feedImgs = new_feedImgs;
    } catch (Exception e) {
     new Logger().write("Exception while adding to feeds: "+e.ToString());
    }
   } catch (Exception e) {
    MessageBox.Show("Corrupt line in feed list. Please create a new one using Feeds > Manage");
    new Logger().write("Error with line '"+line+"':"+e.ToString());
   }
  }
  
  ColumnHeader feedNameHeader = new ColumnHeader();
  ColumnHeader feedLinkHeader = new ColumnHeader();
  ColumnHeader feedImgHeader = new ColumnHeader();
  
  feedNameHeader.Text = "Name";
  feedNameHeader.Width = 150;
  
  feedLinkHeader.Text = "URL";
  feedLinkHeader.Width = 300;
  
  feedImgHeader.Text = "Image";
  feedImgHeader.Width = 100;
  
  listView.Columns.Add(feedNameHeader);
  listView.Columns.Add(feedLinkHeader);
  listView.Columns.Add(feedImgHeader);
  
  for(int c = 0; c < feedCount; c++)
  {
   ListViewItem item = new ListViewItem(feedNames[c]);
   item.SubItems.Add(feedLinks[c]);
   item.SubItems.Add(feedImgs[c]);
   
   listView.Items.Add(item);
  }
  
  return listView;

I'm thinking that even if I failed to load the data, the column headers would still show up. Any ideas of why it's not changing? I'm not getting any message boxes popping up either so it's not hitting any snags in the function.
 
It would be a better option to pass the ListView to the method and have it populate it:
new Utility().getFeedList(this.listView);

Make it static if is has not other instance dependencies (usage indicates so), call would then be:
Utility.getFeedList(this.listView);

If you wonder how the designer generated code manages to add a control to the form you can have a look at the code in InitializeComponent method.

How is #Develop these days? Don't think I've tried it since v1 or 2 perhaps, and it fell rather short to VS Express back then.
 
#Develop is quite nice. No problems at all and it's only about 50 MB and does VB as well so I'm quite pleased with it.

I tried all that you said and sadly still didn't get it to update. :/ Itried change it to Utility.getFeedList(listView) but got an error so I just made an instance of Utility and set it to null when I was done.
Manage.cs:
C#:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FeedMe
{
 public partial class Manage : Form
 {
  public Manage()
  {    
   InitializeComponent();
  }
  
  void ManageLoad(object sender, EventArgs e)
  {      
   Utility util = new Utility();
   
   this.listView = util.getFeedList(listView);
   this.Refresh();
   this.Update();
   
   util = null;
  }
  
  void Button3Click(object sender, EventArgs e)
  {
   // save, then
   this.Close();
  }
 }
}

Utility
C#:
public ListView getFeedList(ListView listView)
  {
   StreamReader sr = new StreamReader(Stream.Null);
   int feedCount = 0;
   
   string[] feedNames = new string[feedCount+1];
   string[] feedLinks = new string[feedCount+1];
   string[] feedImgs = new string[feedCount+1];
   
   try{
    sr = new StreamReader(".\\resources\\feeds.cfg");
   } catch (Exception e) {
    MessageBox.Show("Could not open feeds.cfg. Please make sure that it is available. You can create a new one using Feeds > Manage");
    new Logger().write("Could not open feeds.cfg: "+e.ToString());
    return listView;
   }
   
   string line = string.Empty;
   
   while((line = sr.ReadLine()) != null)
   {
    try{
     string newFeedName = line.Substring(0, line.IndexOf(' '));
     line = line.Substring(line.IndexOf(' ')+1);
     
     string newFeedLink = line.Substring(0, line.IndexOf(' '));
     line = line.Substring(line.IndexOf(' ')+1);
     
     string newFeedImg = line;
     
     try{
      feedNames[feedCount] = newFeedName;
      feedLinks[feedCount] = newFeedLink;
      feedImgs[feedCount] = newFeedImg;
      
      feedCount++;
      
      string[] new_feedNames = new string[feedCount+1];
      string[] new_feedLinks = new string[feedCount+1];
      string[] new_feedImgs = new string[feedCount+1];
      
      feedNames = new_feedNames;
      feedLinks = new_feedLinks;
      feedImgs = new_feedImgs;
     } catch (Exception e) {
      new Logger().write("Exception while adding to feeds: "+e.ToString());
     }
    } catch (Exception e) {
     MessageBox.Show("Corrupt line in feed list. Please create a new one using Feeds > Manage");
     new Logger().write("Error with line '"+line+"':"+e.ToString());
    }
   }
   
   ColumnHeader feedNameHeader = new ColumnHeader();
   ColumnHeader feedLinkHeader = new ColumnHeader();
   ColumnHeader feedImgHeader = new ColumnHeader();
   
   feedNameHeader.Text = "Name";
   feedNameHeader.Width = 150;
   
   feedLinkHeader.Text = "URL";
   feedLinkHeader.Width = 300;
   
   feedImgHeader.Text = "Image";
   feedImgHeader.Width = 100;
   
   listView.Columns.Add(feedNameHeader);
   listView.Columns.Add(feedLinkHeader);
   listView.Columns.Add(feedImgHeader);
   
   for(int c = 0; c < feedCount; c++)
   {
    ListViewItem item = new ListViewItem(feedNames[c]);
    item.SubItems.Add(feedLinks[c]);
    item.SubItems.Add(feedImgs[c]);
    
    listView.Items.Add(item);
   }
   
   return listView;
  }

Also, here's the designer. Not sure if there's anything in here that you might need to see:
C#:
/*
 * Created by SharpDevelop.
 * User: MURPHYC1
 * Date: 1/24/2012
 * Time: 7:28 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
namespace FeedMe
{
 partial class Manage
 {
  /// <summary>
  /// Designer variable used to keep track of non-visual components.
  /// </summary>
  private System.ComponentModel.IContainer components = null;
  
  /// <summary>
  /// Disposes resources used by the form.
  /// </summary>
  /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  protected override void Dispose(bool disposing)
  {
   if (disposing) {
    if (components != null) {
     components.Dispose();
    }
   }
   base.Dispose(disposing);
  }
  
  /// <summary>
  /// This method is required for Windows Forms designer support.
  /// Do not change the method contents inside the source code editor. The Forms designer might
  /// not be able to load this method if it was changed manually.
  /// </summary>
  private void InitializeComponent()
  {
   System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Manage));
   this.listView = new System.Windows.Forms.ListView();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   // 
   // listView
   // 
   this.listView.LabelEdit = true;
   this.listView.Location = new System.Drawing.Point(12, 12);
   this.listView.Name = "listView";
   this.listView.Size = new System.Drawing.Size(550, 270);
   this.listView.TabIndex = 0;
   this.listView.UseCompatibleStateImageBehavior = false;
   // 
   // button1
   // 
   this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
   this.button1.Location = new System.Drawing.Point(12, 295);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(75, 23);
   this.button1.TabIndex = 2;
   this.button1.Text = "Add";
   this.button1.UseVisualStyleBackColor = true;
   // 
   // button2
   // 
   this.button2.Location = new System.Drawing.Point(93, 295);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(75, 23);
   this.button2.TabIndex = 3;
   this.button2.Text = "Remove";
   this.button2.UseVisualStyleBackColor = true;
   // 
   // button3
   // 
   this.button3.Location = new System.Drawing.Point(487, 295);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(75, 23);
   this.button3.TabIndex = 4;
   this.button3.Text = "Save";
   this.button3.UseVisualStyleBackColor = true;
   this.button3.Click += new System.EventHandler(this.Button3Click);
   // 
   // Manage
   // 
   this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
   this.ClientSize = new System.Drawing.Size(577, 330);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.listView);
   this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
   this.MaximizeBox = false;
   this.Name = "Manage";
   this.Text = "Manage Feeds";
   this.TopMost = true;
   this.Load += new System.EventHandler(this.ManageLoad);
   this.ResumeLayout(false);
  }
  private System.Windows.Forms.ListView listView;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button1;
 }
}
 
Last edited:
public static void getFeedList(ListView listView)
 
I got it working! Took me a bit but after some random tweaking but it turns out that I had the view type set to ImageLarge and not details which was apparently what I needed for what I was trying to do. The columns are now showing up. Some more tweaking should get the datas in there.

Thanks for all your help. I'm still a noob when it comes to understanding static/nonstatic variables but I still have much to learn :)
Oh, and do you have any other comments on my coding style? I always wonder if it is good or bad.
 
Didn't look much at it until now, but one thing you should look at is using xml for data storage instead of your cfg string.
And these three lines are all pointless:
   this.Refresh();
   this.Update();   
   util = null;

The local variable util and its instance is not necessary, but if it was it would have gone out of scope at that point.
Update and Refresh are graphics operations that both is not relevant here.

'button1/button2/button3' - give them sensible names, that could also be said for 'listView'.
 
#Develop is quite nice. No problems at all and it's only about 50 MB and does VB as well so I'm quite pleased with it.
I will try latest version some day, is currently using VS Express for both VB and C#. I remember I liked the integrated code conversion for those languages in SharpDevelop, this was before online converters came about, nowadays this is not so important feature for me.
 
Back
Top Bottom