C# WinC窗体列表框不显示列表的内容
我有一个winforms应用程序,我正在尝试使用MVC编程 在designer.cs中,我有:C# WinC窗体列表框不显示列表的内容,c#,winforms,C#,Winforms,我有一个winforms应用程序,我正在尝试使用MVC编程 在designer.cs中,我有: this.listBox_regApps = new System.Windows.Forms.ListBox(); this.listBox_regApps.Anchor = System.Windows.Forms.AnchorStyles.Top; this.listBox_regApps.FormattingEnabled = true; this.listBo
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;
然后在后面的正常cs文件中,我尝试将listbox数据设置为一个列表应用程序。当我调试列表应用程序时,确实有数据,但它从未出现在表单中。不知道为什么。我尝试添加.update和.refresh,但这两个都不起作用
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;
}
}
}
像这样的
foreach (string app in apps)
{
listBox_regApps.Items.Add(app);
}
记住清除列表框中的旧项目。列表框_regApps.Items.Clear;我也试过了,其实还是空白。我是否在错误的区域中执行此操作?是否为Setapps:method添加了按钮事件?你在哪里调用它?你是怎么发射的?欢迎来到Stack Overflow!请添加一些关于此代码如何/为什么帮助OP的解释。这将有助于提供未来观众可以从中学习的答案。有关更多信息,请参阅。