简体   繁体   中英

Winforms c# listbox not displaying contents of list

I have a winforms application that I am trying to program using MVC

In the designer.cs for the form I have:

    this.listBox_regApps = new System.Windows.Forms.ListBox();
    this.listBox_regApps.Anchor = System.Windows.Forms.AnchorStyles.Top;
    this.listBox_regApps.FormattingEnabled = true;
    this.listBox_regApps.Location = new System.Drawing.Point(62, 115);
    this.listBox_regApps.Name = "listBox_regApps";
    this.listBox_regApps.Size = new System.Drawing.Size(351, 134);
    this.listBox_regApps.TabIndex = 1;

Then later in the normal cs file I try to set the listbox data to be a list (apps). When I debug the list apps does indeed have the data, but it never appears in the form. Not sure why. I've tried adding .update and .refresh but neither of those worked either.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AppCompatDemo1
{
    interface IAppView
    {
        void AddListener(IController controller);
    };



    public partial class AppCompatApp : Form, IAppView
    {
        IController controller;
        public AppCompatApp()
        {

            InitializeComponent();
        }

        public void AddListener(IController controller)
        {
            this.controller = controller;
        }

        //Trying to set my listbox to display this list
        public void SetApps(List<string> apps)
        {
            listBox_regApps.DataSource = apps;
        }




    }

    public interface IController
    {
        void OnTestClick(string currentApp);
    }

    class AppController : IController
    {
        AppCompatApp view;
        IAppModel model;

        public AppController(IAppModel model, AppCompatApp view)
        {
            this.model = model;
            this.view = view;
            this.view.AddListener(this);
            view.SetApps(model.getApps());
        }

    }

    interface IAppModel
    {
        List<string> getApps();
    }

    class AppModel : IAppModel
    {
        List<string> apps;
        public AppModel()
        {
            apps = new List<string>();
            apps.Add("app1");
            apps.Add("app2");
            apps.Add("app3");
        }

        public List<string> getApps()
        {
            return apps;
        }

    }
}

Something like this.

foreach (string app in apps)
{
    listBox_regApps.Items.Add(app);
}      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM