c#更新listView来自另一个类 [英] c# update listView from another class
问题描述
您好,
因为我是c#的新手,所以当我开始使用类并试图尝试时遇到问题在该类中更新listView(调用 UpdateLogList())。
正如您在此处所见,我在Form1中使用UpdateLogList(),这是有效的,因为我不能让它静止。
任何人都可以向我展示如何以最简单的方式做到这一点,我看了很多视频和google很多但没有找到解决我的问题的办法。
这是一个代码
public partial class Form1:Form
{
private class Custom:S
{
public override void Init()
{
base.Init();
尝试
{
UpdateLogList(" update in here"); //不工作
}
catch(例外e)
{
UpdateLogList(" EXCEPTION" + e.Message); //不工作
}
}
}
//这是我在程序
公共Form1中的唯一形式()
{
InitializeComponent();
}
private void myMethod(){
//我在这里也使用UpdateLogList()//工作
}
public void UpdateLogList(字符串数据)//无法使其在私有类中工作自定义:S
{
if(InvokeRequired)
{
logList.Invoke(new Action(()=> logList.Items.Add(DateTime.Now +" - " + data)));
}
else
{
logList.Items.Add(DateTime.Now +" - " + data);
}
}
}
你好dgadev,
对于迟到的回复感到抱歉。
从上面的代码中,我注意到了你调用了"UpdateLogList()"覆盖函数名为Init()的方法,但你在哪里调用了Init?
>> 任何人都可以向我展示如何以最简单的方式执行此操作
以下是一个简单的内容演示用于您的目的:
公共部分类Form3:表格
{
public Form3()
{
InitializeComponent ();
}
public void UpdateLogList(字符串数据)//无法使其在私有类中工作自定义:S
{
if(InvokeRequired)
{
logList.Invoke(new Action(()=> logList.Items.Add(DateTime.Now +" - " + data)));
}
else
{
logList.Items.Add(DateTime.Now +" - " + data);
}
}
private void button1_Click(object sender,EventArgs e)
{
Custom custom = new Custom(this);
}
私有类Custom
{
// public override void Init()
// {
// base.Init( );
//尝试
// {
// UpdateLogList(" update in here"); //无效
//}
// catch(例外e)
// {
// UpdateLogList(" EXCEPTION" + e.Message); //无效
//}
//}
public Custom(Form3 f3)
{
try
{
f3.UpdateLogList(" update in here"); //无效
}
catch(例外e)
{
f3.UpdateLogList(" EXCEPTION" + e.Message); //无效
}
}
}
}
问候,
Stanly
Hello,
Since i am new in c# i run to a problem when i start to use class and tried to update listView ( call UpdateLogList() ) in that class.
As you can see in here i am using UpdateLogList() in Form1, this works and because of that i can't make it static.
Can anyone show me example how to do it in easiest way, i watched many videos and googled a lot but didn't found solution to mine problem.
Here is a code
public partial class Form1 : Form
{
private class Custom : S
{
public override void Init()
{
base.Init();
try
{
UpdateLogList("update in here"); // don't works
}
catch (Exception e)
{
UpdateLogList("EXCEPTION " + e.Message); // don't works
}
}
}
// this is my main and only form in program
public Form1()
{
InitializeComponent();
}
private void myMethod() {
// i am using UpdateLogList() here too // works
}
public void UpdateLogList(string data) // cant make this to work in private class Custom : S
{
if (InvokeRequired)
{
logList.Invoke(new Action(() => logList.Items.Add(DateTime.Now + " - " + data)));
}
else
{
logList.Items.Add(DateTime.Now + " - " + data);
}
}
}
Hi dgadev,
Sorry for the late reply.
From your code above, I noticed that you called the "UpdateLogList()" method in the override function named Init(), but where did you call the Init?
>>Can anyone show me example how to do it in easiest way
The following is a simple demo for your purpose:
public partial class Form3 : Form { public Form3() { InitializeComponent(); } public void UpdateLogList(string data) // cant make this to work in private class Custom : S { if (InvokeRequired) { logList.Invoke(new Action(() => logList.Items.Add(DateTime.Now + " - " + data))); } else { logList.Items.Add(DateTime.Now + " - " + data); } } private void button1_Click(object sender, EventArgs e) { Custom custom = new Custom(this); } private class Custom { //public override void Init() //{ // base.Init(); // try // { // UpdateLogList("update in here"); // don't works // } // catch (Exception e) // { // UpdateLogList("EXCEPTION " + e.Message); // don't works // } //} public Custom(Form3 f3) { try { f3.UpdateLogList("update in here"); // don't works } catch (Exception e) { f3.UpdateLogList("EXCEPTION " + e.Message); // don't works } } } }Regards,
Stanly
这篇关于c#更新listView来自另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!