积极答复者
文本框更新的问题

问题
-
我在form1打开form2,form2有四个文本框数组(代码生成的),当第一次以按钮打开的时候这个文本框数组时可以实时更新数据的,然后呢我点了一下form1让form2失去焦点,然后再点击原来打开form2窗口的按钮重新打开form2,此事问题就发生了。用代码生成的这四个文本框数组数据不会更新了,我用代码调试发现有给它赋值,但是始终是显示失去焦点之前的数据不会再更新了,而其它直接拖放在窗体上的文本框的就能更新数据。而如果最小化然后再放大就不会出现问题了。
我是想问,是不是我少写了那个属性?生成文本框数组如下。
for (int i = 0; i < 4; i++)
{
txtbox[i] = new TextBox();
txtbox[i].Location = new System.Drawing.Point(77, 113+i*52);
txtbox[i].Name = "txtbox" + i.ToString();
txtbox[i].Size = new System.Drawing.Size(167, 30);
txtbox[i].Text = "";
txtbox[i].ReadOnly = true;
txtbox[i].Font = new System.Drawing.Font("Arial", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
txtbox[i].TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.Controls.Add(txtbox[i]);
}
can you help me?
答案
全部回复
-
然后呢我点了一下form1让form2失去焦点,然后再点击原来打开form2窗口的按钮重新打开form2,
我用代码调试发现有给它赋值,但是始终是显示失去焦点之前的数据不会再更新了,而其它直接拖放在窗体上的文本框的就能更新数据
txtbox[i] = new TextBox();
this.Controls.Add(txtbox[i]);
很明显你的控件实例化和添加的位置不对 因为其它控件拖入的没有问题
你可以对比拖入的控件的构造函数位置和你
txtbox[i] = new TextBox();
this.Controls.Add(txtbox[i]);这些操作的函数的位置
应该是代码动态添加的控件的构造函数和添加到当前控件的位置不对
-
用的是委托。下面是全部的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Collections;
namespace ATMP_7022A
{
public partial class ATMP : Form
{
private static string Equindex;
public ATMP()
{
InitializeComponent();}
static config conf = new config();
static TextBox[] txtbox = new TextBox[4];
static string[] rec;
string[] recvlotage;
string[] reccurrent;
string[] recpower;
string[] recvar;
string[] recsvar;
ArrayList VoltageA = new ArrayList();//电压A相
ArrayList VoltageB = new ArrayList();//电压B相
ArrayList VoltageC = new ArrayList();//电压C相
ArrayList CurrentA = new ArrayList();//电流A相
ArrayList CurrentB = new ArrayList();//电流B相
ArrayList CurrentC = new ArrayList();//电流C相
ArrayList PowerA = new ArrayList();//有功功率A相
ArrayList PowerB = new ArrayList();//有功功率B相
ArrayList PowerC = new ArrayList();//有功功率C相
ArrayList VarA = new ArrayList();//无功功率A相
ArrayList VarB = new ArrayList();//无功功率B相
ArrayList VarC = new ArrayList();//无功功率C相
ArrayList sVarA = new ArrayList();//视在功率A相
ArrayList sVarB = new ArrayList();//视在功率B相
ArrayList sVarC = new ArrayList();//视在功率C相
public delegate void Deleshow();
Deleshow Dshow;public ATMP(string Itemindex)
{
InitializeComponent();
Equindex = Itemindex;
CreateTextbox();
Dshow = new Deleshow(ShowDetai);
rec = conf.GetVoltage();
}private void button_Click(object sender, EventArgs e)
{
switch (((Button)sender).Name)
{
case "button1":
rec = conf.GetVoltage();
return;
case "button2":
rec = conf.GetCurrent();
return;
case "button3":
rec = conf.GetHz();
return;
case "button4":
rec = conf.GetCos();
return;
case "button5":
rec = conf.GetPower();
return;
case "button6":
rec = conf.GetVar();
return;
case "button7":
rec = conf.GetSvar();
return;
case "button8":
rec = conf.GetBB();
return;
default:
rec = conf.GetVoltage();
return;
}}
private void ShowDetai()
{
int i;
try
{
conf.SeparateArray((string)Device.HasContain[Equindex]);
for (i = 0; i < 3; i++)
{
InsertValueIntoHastable(i);
}
for ( i= 0; i < 4; i++)
{
txtbox[i].Text = rec[i];
ShowInfoToTextbox(VoltageA, MaxVoltageA, MinVoltageA);
ShowInfoToTextbox(VoltageB, MaxVoltageB, MinVoltageB);
ShowInfoToTextbox(VoltageC, MaxVoltageC, MinVoltageC);
ShowInfoToTextbox(CurrentA, MaxCurrentA, MinCurrentA);
ShowInfoToTextbox(CurrentB, MaxCurrentB, MinCurrentB);
ShowInfoToTextbox(CurrentC, MaxCurrentC, MinCurrentC);
ShowInfoToTextbox(PowerA, MaxPowerA, MinPowerA);
ShowInfoToTextbox(PowerB, MaxPowerB, MinPowerB);
ShowInfoToTextbox(PowerC, MaxPowerC, MinPowerC);
ShowInfoToTextbox(VarA, MaxVarA, MinVarA);
ShowInfoToTextbox(VarB, MaxVarB, MinVarB);
ShowInfoToTextbox(VarC, MaxVarC, MinVarC);
ShowInfoToTextbox(sVarA, MaxsVarA, MinsVarA);
ShowInfoToTextbox(sVarB, MaxsVarB, MinsVarB);
ShowInfoToTextbox(sVarC, MaxsVarC, MinsVarC);
}
}
catch
{
}
}//将数据放入arrylist,超过1千的时候删除2-999的数据
private void ShowInfoToTextbox(ArrayList list, TextBox maxtext, TextBox mintext)
{
list.Sort();
list.Reverse();
maxtext.Text = (string)list[0];
mintext.Text = (string)list[list.Count - 1];
if (list.Count >= 1000)
{
list.RemoveRange(1, 998);
}
}
private void InsertValueIntoHastable(int i)
{
recvlotage = conf.GetVoltage();
reccurrent = conf.GetCurrent();
recpower = conf.GetPower();
recvar = conf.GetVar();
recsvar = conf.GetSvar();
switch (i)
{
case 0:
VoltageA.Add(recvlotage[i]);
CurrentA.Add( reccurrent[i]);
PowerA.Add(recpower[i]);
VarA.Add( recvar[i]);
sVarA.Add(recsvar[i]);
return;
case 1:
VoltageB.Add(recvlotage[i]);
CurrentB.Add(reccurrent[i]);
PowerB.Add( recpower[i]);
VarB.Add(recvar[i]);
sVarB.Add( recsvar[i]);
return;
case 2:
VoltageC.Add(recvlotage[i]);
CurrentC.Add(reccurrent[i]);
PowerC.Add(recpower[i]);
VarC.Add(recvar[i]);
sVarC.Add(recsvar[i]);
return;
default:
return;
}}
//创建控件数组
private void CreateTextbox()
{
for (int i = 0; i < 4; i++)
{
txtbox[i] = new TextBox();
txtbox[i].Location = new System.Drawing.Point(77, 113+i*52);
txtbox[i].Name = "txtbox" + i.ToString();
txtbox[i].Size = new System.Drawing.Size(167, 30);
txtbox[i].Text = "";
txtbox[i].ReadOnly = true;
txtbox[i].Font = new System.Drawing.Font("Arial", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
txtbox[i].TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.Controls.Add(txtbox[i]);
}
}private void ATMP_Load(object sender, EventArgs e)
{
//禁止窗口被拖拽放大。
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
timer1.Interval = 1000;
timer1.Enabled = true;
}private void ATMP_FormClosed(object sender, FormClosedEventArgs e)
{
this.Dispose();
this.Close();
}private void timer1_Tick(object sender, EventArgs e)
{Dshow();
}
}
}
can you help me? -
如果是 static System.Timers.Timer t = new System.Timers.Timer(); 不就就要求是静态的成员变量了吗?这个事我当初看system.timers.timer的时候,无论是书上还是网上找的几乎全是在定义timer的时候加上static,所以我也加了。但其实我并不知道加了这个static有什么用,依葫芦画瓢了,但是后来调用的时候老是要求用静态的,所以就没用了,用回窗体拖拽的timer控件了。
你能解释下为什么他们都在前面加static呢?
can you help me?你好!
我上面解释了,Timer并没有要求一定定义成static!你完全可以定义成非静态的Timer!
如果你的Timer是所有窗体实例公用的,可以定义为static,如果不是,定义成实例成员就可以了!
周雪峰