【发布时间】:2011-10-31 14:03:08
【问题描述】:
我试图通过公开两个方法然后从另一个类调用方法来使 WinForm 上的进度条工作(请参阅下面的代码示例)。但运气不好,条形图不动。
这是两个类:
namespace GP_Avantis_Integration
{
//Class B
public partial class GP_Avantis_Integration_Window : Form
{
public GP_Avantis_Integration_Window()
{
InitializeComponent();
}
DataSet ds = new DataSet();
SqlDataAdapter sqlda = new SqlDataAdapter();
SqlCommand sqlcomm = new SqlCommand();
public static int recno;
public void button1_Click(object sender, EventArgs e)
{
try
{
//Fetch data into memory
//Fill in Header table
//Fill in Line table
//Cleaning open connection
//Creating relationship in the Dataset between Header and Line table
// Instantiating and Crearintg Header and Line source
//Binding the Header source to the Header table
//Binding the Line source to the relationship
}
catch (ApplicationException ae)
{
}
finally
{
}
}
public void button2_Click(object sender, EventArgs e)
{
try
{
//Calling CreateJE Class
//Class Method ProcessData
CreateJE JE = new CreateJE(); --------> Calls the Class B
JE.ProcessData(ds);
MessageBox.Show("Complete!");
}
catch (ApplicationException ae)
{
}
finally
{
}
}
public void progress_Bar_setup()
{
progressBar1.Minimum = 0;
progressBar1.Maximum = CreateJE.max;
}
public void progressBar_updates(int recno)
{
progressBar1.Value = recno;
progressBar1.Update();
}
}
// Class B
class CreateJE
{
static public int max;
public void ProcessData (DataSet ds)
{
//Create an eConnect Trx type object
//POPTransactionType po = new POPTransactionType();
// ***** PO Header and Line
int ln;
ln = 0;
//Setting up ProgressBar
int recno = 1;
max = ds.Tables[0].Rows.Count;
GP_Avantis_Integration_Window w = new GP_Avantis_Integration_Window();
w.progress_Bar_setup();
// Create an eConnect PO Header node object
// Create an array for lineitems
foreach (DataRow dtrHDR in ds.Tables["Header"].Rows)
{
//ProgressBar Updates
w.progressBar_updates(recno);
//Instantiating GetJE object
//Retrieves the next JE from GP
//Create an eConnect PO Header node object
//Add the header node to the trx type object
ln = 0;
foreach (DataRow dtrLine in dtrHDR.GetChildRows("HdrLine"))
{
// Populate the elements of the taPoLIne_ItemsTaPOLine XML node
//Avantis Inv Trx Key
// Avantis GL Trx Type
//Add POLine to an Array
ln ++;
}
// Add the header node to the trx type object
// Add the lineitem node to the trx type object
// ***** Process information only
// Create an eConnect document object
// Create a file on the HD
// Serialize using the XmlTextWriter to the file
// Call the eConnectMethods
// Separate Class
// Instantiating the object for eConnectMethods class
// Passing last JRNENTRY retreived using the GetJE class
// so if there is an error on the eConnectEntry Method of eConnectMethods Class
// I can pass the last JE number back to GP
recno++;
}
}
}
}
【问题讨论】:
-
出了什么问题? progesbar1.value 是否在调试时发生变化?如果是这样,您可能需要在后台线程中启动 foreach 循环。
-
您将无法通过使用对进度条的引用来调用连接到 B 类的属性或方法。发布实际代码,您的示例代码,甚至不是有效代码。这时候除非你贴出可以实际编译的代码,否则帮不上忙,请解释清楚什么是行不通的。
-
我正在设置 min=0,max=2。在 B 类中使用 foreach 循环将 1 和 2 的值传递给 progressbar1.update() 方法。调试时我可以看到正在传递的值。进度条(视觉上)没有移动。
-
我想你在哪里展示你的表格?您在上面为 B 类显示的代码声明了一个 WinForm 的新实例,但它似乎没有调用该表单的 Show 方法。您是否以某种方式将 A 类的实例传递给 B 类?您似乎正在 B 类中创建 WinForm 的新实例并尝试更新该实例而不是现有实例。但是您发布的代码并未显示这一点。这就是为什么展示一个可以编译的简短而完整的程序很重要!
-
我包含了完整的代码和一些编辑。希望这会有所帮助。
标签: c# winforms progress-bar