Home > Net >  Adaptive programs run on VS2015 normal screen in VS2019 will object reference not set to an instance
Adaptive programs run on VS2015 normal screen in VS2019 will object reference not set to an instance

Time:12-02

Screen adaptive procedure, in two different versions of the platform,
String [] mytag=con. The Tag. The ToString (). The Split (new char [] {} ', ');//to get control of the Tag attribute values, and splits after storing a string array [/color]
The program error, a great god genuflect is begged solution



# region adaptive window
Private float FormHeight;
Private float FormWidth;
///& lt; Summary>
///will control the width and height, the left and top margins and font size staging to tag attribute
///& lt;/summary>
///& lt; Param name="cons" & gt;
Private void setTag (Control cons)
{
Foreach (Control con in cons. Controls)
{
Con. The Tag=con. Width + ":" + con. Height + ":" + con. Left + ":" + con. Top + ":" + con. The Font. The Size;
If (con. Controls. Count & gt; 0)
{
SetTag (con);
}
}
}
//form according to the size adjustment control size
Private void setControls (float newx, float newy, Control cons)
{
Try
{
//traverse control in the form, reset the control values
Foreach (Control con in cons. Controls)
{
String [] mytag=con. The Tag. The ToString (). The Split (new char [] {} ', ');//to get control of the Tag attribute values, and splits after storing a string array [/color]
Float a=System. The Convert. ToSingle (mytag [0]) * newx;//according to form scale controls the value of the width of the
Con. Width=(int) a;//width
A=System. The Convert. ToSingle (mytag [1]) * newy;//height
Con. Height=(int) (a);
A=System. The Convert. ToSingle (mytag [2]) * newx;//the left distance
Con. Left=(int) (a);
A=System. The Convert. ToSingle (mytag [3]) * newy;//on the edge distance
Con. Top=(int) (a);
Single currentSize=System. The Convert. ToSingle (mytag [4]) * newy;//font size
Con. The Font=new System. Drawing. The Font (con. The Font. The Name, currentSize, con. The Font, Style, con. The Font. The Unit).
If (con. Controls. Count & gt; 0)
{
SetControls (newx, newy, con);
}
}
}
Catch
{

}

}

Private void Form1_Load (object sender, EventArgs e)
{
FormWidth=this. Width;//get the width of the form
FormHeight=this. Height; The height of the//get the form
SetTag (this);//call the method
}

Private void Form1_Resize (object sender, EventArgs e)
{
Float newx=(. This Width)/FormWidth;//window width scaling
Float newy=(. This Height)/FormHeight;//form highly scaling
SetControls (newx, newy, this);//change with form controls size
}

CodePudding user response:

Should be a Tag to null, interrupt the point you see, the setTag (this);//call methods on the end of the initialization is invoked try
In
 
Public _click ()
{
InitializeComponent ();
FormWidth=this. Width;//get the width of the form
FormHeight=this. Height; The height of the//get the form
SetTag (this);//call the method
}


  •  Tags:  
  • C#
  • Related