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;报错,提示信息是“输入的字符串格式不正确,应该怎么针对更改,谢谢!

解决方案 »

  1.   

    我想让我窗口中的控件随窗口的变化而变化
    但是我还不想让控件picturebox的Tag变化,所以用if (con is PictureBox) continue;限制了一下,但是,限制后出现错误了,就是”输入的字符串格式不正确“,我喜爱那个知道怎么更改方法setConstrols
      

  2.   

    我之前的做法是设Anchor,但是有的控件显示的位置不正确,我的页面控件是一层一层嵌套的,只能写代码
      

  3.   

    我也问过类似的问题C#也有类似JAVA吧布局管理器的类 功能强大   不一定 “只能写代码”http://topic.csdn.net/u/20080326/13/a9e689af-8c24-4dcd-9fdc-89566f4be46c.html
      

  4.   

    在setTag里没有设PictureBox的tag
    那在setConstrols也要有对PictureBox的判断估计是碰到PictureBox的tag的问题
      

  5.   


     一定是,但是我不知道怎么改代码,
    一定没有PictureBox的新的tag
      

  6.   

    你的PictureBox的Tag中并没有写入数据当然在setConstrols会出错了.
      

  7.   

    [code=C#]
    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;
      

  8.   

    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 );
                    }
                }
            }
      

  9.   


    呵呵,大哥,那所有的picturebox的位置和大小就都不能随这窗口的变化而改变了,呵呵
      

  10.   

    能不能实现   picutrebox 的大小位置还随着窗口改变,而我还想获取他原来的tag,因为我需要用tag进行判断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.Tag.ToString ()) 

    this.lVw.Items[n].Selected=true;
    this.lVw.Focus ();
    break; 

    ++n; 

    }
      

  11.   


    如果control的位置大小都是固定的,那就没有必要用tag来保存原来的状态,直接从control取位置大小变换就可以了
      

  12.   


    我也想这么实现,不通过tag来改变控件,不过有N多控件
      

  13.   


    你着段代码是干什么的呢?tag里面存放的是什么呢?不能用其他方法、自定义属性或是变量来替换吗?
      

  14.   

    提示为picutrebox添加一个新的属性newTag
        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 );
                    }
                }
            }
      

  15.   

    Tag是对应的listview中的item.SubItems [0].Text(第一列),我的目的是想点击图片(代表listview中的一行信息),就选择对应的listview行,难道不通过PictureBox 的tag行能通过什么
      

  16.   

    你现在可以把tag换成newTag了.
    我的代码在工具箱里面多了个Newpicturebox控件,这个控件多了个newtag属性.拖到窗口上就可以用拉.
    你可以用它来获得选择对应的listview行,而tag用来改变控件大小.