Witam
Mam kod który powinien w miarę zmiany rozmiaru okna (podczas działania programu) zmieniać położenie i rozmiar buttona umieszczonego w oknie, ale gdy uruchomię ten program i zmienię rozmiar okna program wyrzuca wyjątek NullReferenceException
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class win : Form
{
private Rectangle button;
private Size formOrginalSize;
public win()
{
InitializeComponent();
}
private void win_Load(object sender, EventArgs e)
{
formOrginalSize = this.Size;
System.Console.Write(formOrginalSize.ToString());
button = new Rectangle(b.Location.X, b.Location.Y, b.Width, b.Height);
}
private void resize(Rectangle orginal, Control control)
{
float xRatio = (float)(this.Width) / (float)(formOrginalSize.Width);
float yRatio = (float)(this.Height) / (float)(formOrginalSize.Height);
int newX = (int)(orginal.X * xRatio);
int newY = (int)(orginal.Y * yRatio);
int neWidth = (int)(orginal.Width * xRatio);
int newHeight = (int)(orginal.Height * yRatio);
control.Location = new Point(newX, newY); //Tu rzuca wyjątek
control.Size = new Size(neWidth, newHeight);
}
private void resize_child()
{
resize(button, b);
}
private void res(object sender, EventArgs e)
{
// resize_child();
System.Console.Write("zmiana rozmiaru");
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(187, 162);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// win
//
this.ClientSize = new System.Drawing.Size(515, 407);
this.Controls.Add(this.button1);
this.Name = "win";
this.Resize += new System.EventHandler(this.res);
this.ResumeLayout(false);
}
}
}
I problem jest taki że nie wiem dlaczego, ani jak zrobić tak żeby działało, ktoś może ma jakieś cenne uwagi