V
V
Virtual Programmer2017-02-03 02:05:31
C++ / C#
Virtual Programmer, 2017-02-03 02:05:31

How to use dynamic objects in C# program?

Created a lot of dynamic objects, how to use them
When clicking on the button, it shows the method

private void button_profile_Click(object sender, EventArgs e)
        {
            profile();
        }

The method contains dynamic objects
private void profile()
        {
            Panel panel_profile = new Panel();
            TextBox textbox_username = new TextBox();
            TextBox textbox_userpass = new TextBox();
            Label label_namepage = new Label();
            Label label_username = new Label();
            Label label_userpass = new Label();
            Button button_login = new Button();
            Button button_register = new Button();
            Button button_close = new Button();
            //
            // Параметы - panel_profile
            //
            panel_profile.Size = new Size(400, 400);
            panel_profile.Location = new Point(200, 80);
            panel_profile.BackColor = Color.FromArgb(238, 238, 238);
            panel_profile.TabIndex = 2;
            panel_profile.Visible = true;
            panel_profile.Name = "panel_profile";
            //
            // Параметы - label_namepage
            //
            label_namepage.Size = new Size(400, 40);
            label_namepage.Location = new Point(200, 20);
            label_namepage.TextAlign = ContentAlignment.MiddleCenter;
            label_namepage.ForeColor = Color.Gold;
            label_namepage.Font = new Font("Verdana", 14.25F, FontStyle.Regular, GraphicsUnit.Point, 204);
            label_namepage.Text = "ФОРМА АВТОРИЗАЦИИ";
            label_namepage.Name = "label_namepage";
            //
            // Параметы - label_username
            //
            label_username.Location = new Point(50, 40);
            label_username.Size = new Size(300, 30);
            label_username.TextAlign = ContentAlignment.MiddleLeft;
            label_username.Text = "ЛОГИН";
            label_username.Name = "label_username";
            //
            // Параметы - label_userpass
            //
            label_userpass.Location = new Point(50, 140);
            label_userpass.Size = new Size(300, 30);
            label_userpass.TextAlign = ContentAlignment.MiddleLeft;
            label_userpass.Text = "ПАРОЛЬ";
            label_userpass.Name = "label_userpass";
            //
            // Параметы - textbox_userpass
            //
            textbox_username.Size = new Size(300, 30);
            textbox_username.Location = new Point(50, 70);
            textbox_username.Multiline = true;
            textbox_username.MaxLength = 20;
            textbox_username.Name = "textbox_username";
            textbox_username.Font = new Font("Verdana", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 204);
            //
            // Параметы - textbox_userpass
            //
            textbox_userpass.Size = new Size(300, 30);
            textbox_userpass.Location = new Point(50, 170);
            textbox_userpass.Multiline = true;
            textbox_userpass.MaxLength = 20;
            textbox_userpass.Name = "textbox_userpass";
            //
            // Параметы - button_login
            //
            button_login.Size = new Size(140, 30);
            button_login.Location = new Point(210, 240);
            button_login.Text = "ВОЙТИ";
            button_login.Name = "button_login";
            button_login.FlatAppearance.BorderSize = 0;
            button_login.FlatAppearance.MouseOverBackColor = Color.FromArgb(50, 50, 50);
            button_login.FlatStyle = FlatStyle.Flat;
            button_login.BackColor = Color.FromArgb(40, 40, 40);
            button_login.Font = new Font("Verdana", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 204);
            button_login.ForeColor = Color.Gainsboro;
            //
            // Параметы - button_login
            //
            button_register.Size = new Size(140, 30);
            button_register.Location = new Point(50, 240);
            button_register.Text = "РЕГИСТРАЦИЯ";
            button_register.Name = "button_register";
            button_register.FlatAppearance.BorderSize = 0;
            button_register.FlatAppearance.MouseOverBackColor = Color.FromArgb(50, 50, 50);
            button_register.FlatStyle = FlatStyle.Flat;
            button_register.BackColor = Color.FromArgb(40, 40, 40);
            button_register.Font = new Font("Verdana", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 204);
            button_register.ForeColor = Color.Gainsboro;
            //
            // Параметы - button_close
            //
            button_close.Size = new Size(30, 30);
            button_close.Location = new Point(370, 0);
            button_close.Text = "X";
            button_close.Name = "button_close";
            button_close.FlatAppearance.BorderSize = 0;
            button_close.FlatStyle = FlatStyle.Flat;
            button_close.Font = new Font("Verdana", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 204);
            button_close.ForeColor = Color.FromArgb(40, 40, 40);
            button_close.Click += new EventHandler(button_close_Click);
            //
            // Контроль элементов
            //
            Controls.Add(label_namepage);
            Controls.Add(panel_profile);
            panel_profile.Controls.Add(label_username);
            panel_profile.Controls.Add(label_userpass);
            panel_profile.Controls.Add(textbox_username);
            panel_profile.Controls.Add(textbox_userpass);
            panel_profile.Controls.Add(button_login);
            panel_profile.Controls.Add(button_register);
            panel_profile.Controls.Add(button_close);
        }

Here is a picture of what happened
f2bb0036b0e54e96afcb6fcdd1907963.PNG. The close button has an action "button_close.Click += new EventHandler(button_close_Click);"
private void button_close_Click(object sender, EventArgs e)
        {
            panel_profile.Visible = false; // Вот он ругается что - имя "panel_profile" не существует в текущем контексте.

            // Также будет и с остальными объектами
        }

What needs to be added or changed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Virtual Programmer, 2017-02-03
@kochura_official

I broke the whole brain and found a solution

using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace newprogram
{
    public partial class Form1 : Form
    {
        // Надо было дописать вот этот код
        private Panel panel_profile;
        private TextBox textbox_username;
        private TextBox textbox_userpass;
        private Label label_namepage;
        private Label label_username;
        private Label label_userpass;
        private Button button_login;
        private Button button_register;
        private Button button_close;

        public Form1()
        {
            InitializeComponent();
            profile();
        }

        private void profile()
        {
            panel_profile = new Panel();
            textbox_username = new TextBox();
            textbox_userpass = new TextBox();
            label_namepage = new Label();
            label_username = new Label();
            label_userpass = new Label();
            button_login = new Button();
            button_register = new Button();
            button_close = new Button();
            //
            // Параметы - panel_profile
            //
            panel_profile.Size = new Size(400, 400);
            panel_profile.Location = new Point(200, 80);
            panel_profile.BackColor = Color.FromArgb(238, 238, 238);
            panel_profile.TabIndex = 2;
            panel_profile.Visible = true;
            panel_profile.Name = "panel_profile";
            //
            // Параметы - label_namepage
            //
            label_namepage.Size = new Size(400, 40);
            label_namepage.Location = new Point(200, 20);
            label_namepage.TextAlign = ContentAlignment.MiddleCenter;
            label_namepage.ForeColor = Color.Gold;
            label_namepage.Font = new Font("Verdana", 14.25F, FontStyle.Regular, GraphicsUnit.Point, 204);
            label_namepage.Text = "ФОРМА АВТОРИЗАЦИИ";
            label_namepage.Name = "label_namepage";
            //
            // Параметы - label_username
            //
            label_username.Location = new Point(50, 40);
            label_username.Size = new Size(300, 30);
            label_username.TextAlign = ContentAlignment.MiddleLeft;
            label_username.Text = "ЛОГИН";
            label_username.Name = "label_username";
            //
            // Параметы - label_userpass
            //
            label_userpass.Location = new Point(50, 140);
            label_userpass.Size = new Size(300, 30);
            label_userpass.TextAlign = ContentAlignment.MiddleLeft;
            label_userpass.Text = "ПАРОЛЬ";
            label_userpass.Name = "label_userpass";
            //
            // Параметы - textbox_userpass
            //
            textbox_username.Size = new Size(300, 30);
            textbox_username.Location = new Point(50, 70);
            textbox_username.Multiline = true;
            textbox_username.MaxLength = 20;
            textbox_username.Name = "textbox_username";
            textbox_username.Font = new Font("Verdana", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 204);
            //
            // Параметы - textbox_userpass
            //
            textbox_userpass.Size = new Size(300, 30);
            textbox_userpass.Location = new Point(50, 170);
            textbox_userpass.Multiline = true;
            textbox_userpass.MaxLength = 20;
            textbox_userpass.Name = "textbox_userpass";
            //
            // Параметы - button_login
            //
            button_login.Size = new Size(140, 30);
            button_login.Location = new Point(210, 240);
            button_login.Text = "ВОЙТИ";
            button_login.Name = "button_login";
            button_login.FlatAppearance.BorderSize = 0;
            button_login.FlatAppearance.MouseOverBackColor = Color.FromArgb(50, 50, 50);
            button_login.FlatStyle = FlatStyle.Flat;
            button_login.BackColor = Color.FromArgb(40, 40, 40);
            button_login.Font = new Font("Verdana", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 204);
            button_login.ForeColor = Color.Gainsboro;
            //
            // Параметы - button_login
            //
            button_register.Size = new Size(140, 30);
            button_register.Location = new Point(50, 240);
            button_register.Text = "РЕГИСТРАЦИЯ";
            button_register.Name = "button_register";
            button_register.FlatAppearance.BorderSize = 0;
            button_register.FlatAppearance.MouseOverBackColor = Color.FromArgb(50, 50, 50);
            button_register.FlatStyle = FlatStyle.Flat;
            button_register.BackColor = Color.FromArgb(40, 40, 40);
            button_register.Font = new Font("Verdana", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 204);
            button_register.ForeColor = Color.Gainsboro;
            //
            // Параметы - button_close
            //
            button_close.Size = new Size(30, 30);
            button_close.Location = new Point(370, 0);
            button_close.Text = "X";
            button_close.Name = "button_close";
            button_close.FlatAppearance.BorderSize = 0;
            button_close.FlatStyle = FlatStyle.Flat;
            button_close.Font = new Font("Verdana", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 204);
            button_close.ForeColor = Color.FromArgb(40, 40, 40);
            button_close.Click += new EventHandler(button_close_Click);
            //
            // Контроль элементов
            //
            Controls.Add(label_namepage);
            Controls.Add(panel_profile);
            panel_profile.Controls.Add(label_username);
            panel_profile.Controls.Add(label_userpass);
            panel_profile.Controls.Add(textbox_username);
            panel_profile.Controls.Add(textbox_userpass);
            panel_profile.Controls.Add(button_login);
            panel_profile.Controls.Add(button_register);
            panel_profile.Controls.Add(button_close);
        }
        private void button_profile_Click(object sender, EventArgs e)
        {
            profile();
        }
        private void button_close_Click(object sender, EventArgs e)
        {
            panel_profile.Visible = false;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question