you should first select the project with ".netFramework".

WinForm implements a generic form based on the middle of the screen or the middle of the parent form.
I demonstrate a simple login function and pop up a new window.
The parent form is displayed in the middle of the screen.

The child form is in the middle of the parent form.

The code logic is: Encapsulates the entity class that operates the database and controls the position of the form. The login function is implemented by querying the database, and the sub-form is displayed. And make the parent form in the middle of the screen and the child form in the middle of the parent form.
public Form1()
{
InitializeComponent();
//Set the form screen to center
FormBaseOPC.SetScreenMiddle(this);
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text != "" && textBox2.Text != "")
{
Login();
}
else
{
MessageBox.Show("Enter empty options, please re-enter");
}
}
public void Login()
{
Dao dao = new Dao();//Entity classes encapsulate database operations
string sql = $"select * from t_user where id='{textBox1.Text}' and psw='{textBox2.Text}'";//sql query database
IDataReader dc = dao.read(sql);
if (dc.Read())
{
Data.UID = dc["id"].ToString();
Data.UName = dc["name"].ToString(); //Data entity class encapsulates the data queried from the database
MessageBox.Show("login successful");
user1 user = new user1();
// Make the child form in the middle of the parent form
FormBaseOPC.SetFormMiddle(user);
}
else
{
MessageBox.Show("Login failed");
}
dao.DaoClose();
}
Entity class that encapsulates the position of the manipulation form:
internal class FormBaseOPC
{
/// <summary>
/// 1- Center the form screen
/// </summary>
/// <param name="form">The form that needs to be centered</param>
public static void SetScreenMiddle2(Form form)
{
if (form != null)
{
//Get the screen setting centering effect
form.SetBounds((Screen.GetBounds(form).Width / 2) - (form.Width / 2),
(Screen.GetBounds(form).Height / 2) - (form.Height / 2),
form.Width, form.Height, BoundsSpecified.Location);
}
}
/// <summary>
/// 1-The form screen is centered (the system comes with it)
/// </summary>
/// <param name="form">The form that needs to be centered</param>
public static void SetScreenMiddle(Form form)
{
if (form != null)
{
form.StartPosition = FormStartPosition.CenterScreen;
}
}
/// <summary>
/// 2- Set to center and open relative to the form
/// </summary>
/// <param name="form">The form that needs to be centered</param>
public static void SetFormMiddle(Form form)
{
if (form != null)
{
//Set the form to center relative to the parent form
form.StartPosition = FormStartPosition.CenterParent;
// show the form
form.ShowDialog();
//set focus to current form
form.Focus();
}
}
}
Entity class encapsulation code:
internal class Dao
{
SqlConnection conn;
public SqlConnection connection()
{
// write database connection string
string connStr = "Data source=localhost;Initial Catalog=student;User ID=sa;Password=123456";
conn = new SqlConnection(connStr);
conn.Open();
return conn;
}
public SqlCommand command(string sql)
{
SqlCommand cmd = new SqlCommand(sql, connection());
return cmd;
}
public int Execute(string sql)
{
return command(sql).ExecuteNonQuery();
}
public SqlDataReader read(string sql)
{
return command(sql).ExecuteReader();
}
public void DaoClose()
{
conn.Close();
}
}
internal class Data
{
public static string UID = "", UName = "";
}
Code logic for welcome form:
public user1()
{
InitializeComponent();
label1.Text = $"Welcome to {Data.UName}";
}
WinForm implements a common form based on the middle of the screen or the middle of the parent form: through the login function. The login page is in the middle of the screen, and the child form is in the middle of the parent form.Hope it helps you.