C# design a windows program interface

It is required to create new groups, create new contacts, view contacts, and design shortcut keys.

Problems encountered

1. Data information transmission

Since this was my first contact, and the teacher skipped sections in class and did not remind me, the program required the main window to transfer data to the sub-window, and the sub-window transferred back to the main window. This has been bothering me for a long time. The Internet is either charged or the instructions are sloppy. bad mood

2.Table

When checking contacts, I planned to use a table, but I had to look at it myself. It was really confusing. It didn’t matter. I almost learned Python by myself. This self-study is not bad 🙁

Solutions

1. Solve data transmission

Here are the examples of main window-Form1 and sub-window-new grouping.

Form1 related procedures

private void Create a new group ToolStripMenuItem_Click(object sender, EventArgs e) //Create a new group
{
    CreateGroup cg = new CreateGroup(this);
    cg.AfterTextChange(sender, group);
    cg.Show();
}

public void AfterTextChangeGroup(object sender, string e) //Receive data group name
{
    string line = e.ToString();
    if (line.Length > 0)
    {
        group.GroupAdd(line);
        group.Number + + ;
    }
}

Procedure for creating a new group

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _11._7Pro_2
{
    public partial class CreateGroup : Form
    {
        int number;
        Form1 MainForm = null; //Create a main window object, set it to empty, and put it into the main window after waiting.
        public CreateGroup(Form1 _mainForm) //The _mainform here is the location of the main window
        {
            InitializeComponent();
            MainForm = _mainForm; //can be called to the main window
        }



        private void buttonCan2_Click(object sender, EventArgs e) //Close the window
        {
            this.Close();
        }


        private void buttonCre_Click(object sender, EventArgs e) //Add group name or not
        {
            if (textBoxGN.Text.Trim() == "")
            {
                DialogResult dr = MessageBox.Show("No group name entered", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    return;
                }
                else
                {
                    this.Close();
                }
            }
            else
            {
                MainForm.AfterTextChangeGroup(sender, textBoxGN.Text); //Transfer data back to the main window
                this.Close();
            }

        }

        public void AfterTextChange(object sender, EventArgs e) //Receive data
        {
            if (e is Group)
            {
                var group = e as Group;
                number = group.Number;
            }
        }
    }
}
1. Main window data and methods can be called in sub-windows

Adjust the class CreateGroup to CreateGroup(Form1 _mainform) and assign it in MainForm = _mainForm;

You can write CreateGroup cg = new CreateGroup(this); when the main window creates its object and shows it, so that the method of the main window can be called in the newly created grouped window.

2. Transfer data back to the main window

Write a method in Form1, AfterTextChangeGroup(object sender, string e), and use it in the new group

MainForm.AfterTextChangeGroup(sender, textBoxGN.Text); you can call the value back

Read in AfterTextChangeGroup in the main window

3. Make the sub-window call the class object of the main window

We have created and initialized the group (Group class) object in the main window. We need to call it in the sub-window. We need to add :Eventargs after Group in the class so that the class uniformly inherits Eventargs. It can pass parameters

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._7Pro_2
{
    public class Group:EventArgs
    {
        private string[] groupname = new string[100];
        private int number;

        public int Number { get { return number; } set { number = value; } }
        public string[] Groupname { get { return groupname; } set { groupname = value; } }
        public void GroupAdd(string name) //Add group name
        {
            groupname[number] = name;
        }
    }
}

In the new group, var group = e as Group; The group here is the global variable in the new group, so that it can call the group data in the main window. In order to force e to Group type, e as Group

2. Make a table

This is not very difficult, just read one or two articles by some great people on the Internet and it will be ok. I originally wanted to learn how to create a database, but after thinking about it I was too bored, so I just learned this.

dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
//Automatically adjust the height of the row based on the contents of the Header and all cells
dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//Set content alignment
dataGridView1.ReadOnly = true;
//Set all cells to be non-editable
dataGridView1.RowHeadersWidth = 15;
//Set the title column width
dataGridView1.AllowUserToAddRows = false;
//Cannot add blank lines


for (int i = 0; i < 7; i + + ) //Add header
{
    dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
}
dataGridView1.Columns[0].Width = 60;
dataGridView1.Columns[1].Width = 150;
dataGridView1.Columns[2].Width = 80;
dataGridView1.Columns[3].Width = 150;
dataGridView1.Columns[4].Width = 150;
dataGridView1.Columns[5].Width = 150;
dataGridView1.Columns[6].Width = 150;
//Specify title column width

dataGridView1.Columns[0].HeaderText = "Name";
dataGridView1.Columns[1].HeaderText = "Mobile Phone";
dataGridView1.Columns[2].HeaderText = "Group";
dataGridView1.Columns[3].HeaderText = "Email";
dataGridView1.Columns[4].HeaderText = "Company phone number";
dataGridView1.Columns[5].HeaderText = "Home Phone";
dataGridView1.Columns[6].HeaderText = "QQ";
//The name of the title

//adding data
for (int i = 0; i < Peo_number; i + + )
{
    dataGridView1.Rows.Insert(i, peoples_pool[i].Name);
    dataGridView1.Rows[i].Cells[1].Value = peoples_pool[i].PhoneNumber;
    dataGridView1.Rows[i].Cells[2].Value = peoples_pool[i].Group;
    dataGridView1.Rows[i].Cells[3].Value = peoples_pool[i].EmailNumber;
    dataGridView1.Rows[i].Cells[4].Value = peoples_pool[i].ComNumber;
    dataGridView1.Rows[i].Cells[5].Value = peoples_pool[i].FamilyNumber;
    dataGridView1.Rows[i].Cells[6].Value = peoples_pool[i].Qqnumber;
}

this.dataGridView1.ClearSelection(); //Remove selection

Don’t write dataGridView1.Columns.Clear(); in the middle with your hands. This will cause the program to go wrong. If the columns are cleared, the following will not be added. I was stuck like this for a long time before I discovered this problem, and when adding rows, It must be there first, you cannot assign the value directly, just write dataGridView1.Rows[i].Cells[6].Value = peoples_pool[i].Qqnumber; the system will warn you that the row does not exist, but here I am adding the row, Insert them all once so that the row appears, and then fill in the remaining data

Summary

This is what I learned on my own as a beginner. There may be a better way. I will continue to study. I feel that there are very few things about C# here. Hey, it’s difficult.