private void Frm_Resize(object sender, System.EventArgs e)
{
float newx = (this.Width )/ x;
float newy = this.Height / y;
setConstrols(newx, newy, this);
}
private void setTag(Control cons)
{
foreach (Control con in cons.Controls )
{
if (con is PictureBox) continue;
con.Tag = con.Width +":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
if (con.Controls.Count > 0)
{
setTag(con);
}
}
}
private void setConstrols(float newx,float newy,Control cons)
{
foreach (Control con in cons .Controls )
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx; con.Width = (int)a;
a=Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a=Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a=Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle (mytag[4]) * newy;
con .Font =new Font (con.Font .Name ,currentSize,con.Font .Style ,con.Font .Unit );
if(con.Controls .Count >0)
{
setConstrols (newx ,newy ,con );
}
}
}private void Frm_Load(object sender, System.EventArgs e)
{
x=this.Width ;
y=this.Height ;
setTag(this);
}
问题是我不想让我狂口中的PictureBox的Tag改变,所以,我在方法setTag中,让PictureBox的Tag不改变,但是执行过程中 方法setConstrols中的float a = Convert.ToSingle(mytag[0]) * newx;报错,提示信息是“输入的字符串格式不正确,应该怎么针对更改,谢谢!
解决方案 »
- 牛人们看看,这是怎么回事。。。
- 求救vs2005和dev express的问题 机器上面装了vs2003 dev express和vs 2005 devexpress
- 如果一个字符串是一个算式,比如2+3,C#什么函数能算出结果
- 100分求解方程组解法
- 用过Infragistics NetAdvantage 2005 Volume 这个.NET控件既高手进来一下
- 关于数据库视图的问题
- 文本文件读写中遇到的问题
- .net引用了一个activex控件,怎么把它拖到窗口里啊?
- 请问大侠,如何在一个项目里使用另一个项目里的资源
- 在C#中用什么替代VB的模块?(急)
- asp.met 的问题 急用 啊
- PictureBox显示不出图片问题(急)??
但是我还不想让控件picturebox的Tag变化,所以用if (con is PictureBox) continue;限制了一下,但是,限制后出现错误了,就是”输入的字符串格式不正确“,我喜爱那个知道怎么更改方法setConstrols
那在setConstrols也要有对PictureBox的判断估计是碰到PictureBox的tag的问题
一定是,但是我不知道怎么改代码,
一定没有PictureBox的新的tag
private void setConstrols(float newx, float newy, Control cons)
{
foreach (Control con in cons.Controls)
{
if (con is PictureBox) continue;
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx; con.Width = (int)a;
a = Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a = Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a = Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[4]) * newy;
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setConstrols(newx, newy, con);
}
}
}
[\code]同样要在setConstrols中加入if (con is PictureBox) continue;
{
foreach (Control con in cons .Controls )
{
if (con is PictureBox) continue; //加这一句 string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;
con.Width = (int)a;
a=Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a=Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a=Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle (mytag[4]) * newy;
con .Font =new Font (con.Font .Name ,currentSize,con.Font .Style ,con.Font .Unit );
if(con.Controls .Count >0)
{
setConstrols (newx ,newy ,con );
}
}
}
呵呵,大哥,那所有的picturebox的位置和大小就都不能随这窗口的变化而改变了,呵呵
{
int n=0;
PictureBox p = sender as PictureBox;
this.lVw .SelectedItems .Clear ();
p.Image =null;
p.BackgroundImage =Image.FromFile (Application.StartupPath +@"\背景\4.jpg");
foreach(ListViewItem item in this.lVw.Items)
{
if( item.SubItems [0].Text==p.Tag.ToString ())
{
this.lVw.Items[n].Selected=true;
this.lVw.Focus ();
break;
}
++n;
}
}
如果control的位置大小都是固定的,那就没有必要用tag来保存原来的状态,直接从control取位置大小变换就可以了
我也想这么实现,不通过tag来改变控件,不过有N多控件
你着段代码是干什么的呢?tag里面存放的是什么呢?不能用其他方法、自定义属性或是变量来替换吗?
public class newPictureBox : PictureBox
{
public object newTag;
public object NewTag
{
get
{
return NewTag;
}
set
{
NewTag = value;
}
}
}
引用newPictureBox pictureBox1=new newPictureBox();然后在写你自己的代码 private void c_Click(object sender, System.EventArgs e)
{
int n=0;
PictureBox p = sender as PictureBox;
this.lVw .SelectedItems .Clear ();
p.Image =null;
p.BackgroundImage =Image.FromFile (Application.StartupPath +@"\背景\4.jpg");
foreach(ListViewItem item in this.lVw.Items)
{
if( item.SubItems [0].Text==p.vewTag.ToString ()) //这里我不知道你原来picturebox的tag干什么用了
{
this.lVw.Items[n].Selected=true;
this.lVw.Focus ();
break;
}
++n;
}
}
这样picturebox的Tag属性就可以用来记录原先的尺寸了。private void setTag(Control cons)
{
foreach (Control con in cons.Controls )
{
//if (con is PictureBox) continue;这里已经不需要拉。
con.Tag = con.Width +":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
if (con.Controls.Count > 0)
{
setTag(con);
}
}
}
private void setConstrols(float newx,float newy,Control cons)
{
foreach (Control con in cons .Controls )
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx; con.Width = (int)a;
a=Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a=Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a=Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle (mytag[4]) * newy;
con .Font =new Font (con.Font .Name ,currentSize,con.Font .Style ,con.Font .Unit );
if(con.Controls .Count >0)
{
setConstrols (newx ,newy ,con );
}
}
}
我的代码在工具箱里面多了个Newpicturebox控件,这个控件多了个newtag属性.拖到窗口上就可以用拉.
你可以用它来获得选择对应的listview行,而tag用来改变控件大小.