help pzhl who than can :) There is a form of authorization

private void btnLogin_Click(object sender, EventArgs e) { if (comboLogin.Text == "Вася" && txtPass.Text == "1111") { this.Hide(); f1.lblRole.Text = comboLogin.Text; f1.Show(); } 

After logging in, the user enters the main form. Also on the main form there is a UserControl (there basically DatagridView lives there). So, on the main form there is a label "lblRole", in the properties it is set Modifiers: public, Text: User None. In lblRole it is displayed who entered, then Vasya. But UserControl does not read the logged in Vasya, but what is displayed in the lblRole properties of the main form, this is UserNone.

Code in UserControl

 private void b1_Load(object sender, EventArgs e)// LOAD { frmMain fm = new frmMain(); label1.Text = fm.lblRole.Text; } 

That's actually the question itself, how to get who entered "Vasya" and not the properties from lblRole in UserControl?

    2 answers 2

    The form for receiving the name and password is called FormLogin Отмена button in the properties of this form is defined as CancelButton , the code of this form is

     public partial class FormLogin : Form { public FormLogin() { InitializeComponent(); } public string Name { get; set; } public string Password { get; set; } private void buttonOk_Click(object sender, EventArgs e) { //считываем значения удаляя пробелы Name = _textBoxName.Text.Trim(); Password = _textBoxPassword.Text.Trim(); //проверяем if (String.IsNullOrEmpty(Name)) { MessageBox.Show("Введите имя.", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (String.IsNullOrEmpty(Password)) { MessageBox.Show("Введите пароль.", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //отдаем результат this.DialogResult = DialogResult.OK; } } 

    The main form of the program has such a code.

     public partial class FormMain : Form { public FormMain() { InitializeComponent(); this.Load += FormMain_Load; } private void FormMain_Load(object sender, EventArgs e) { FormLogin formLogin = new FormLogin(); while (formLogin != null) { if (formLogin.ShowDialog() == DialogResult.OK) { if (CheckUser(formLogin.Name, formLogin.Password)) { //присваиваем лейблу имя юзера lblRole.Text = formLogin.Name; //закрываем форму входа formLogin.Close(); formLogin = null; } else { MessageBox.Show("Неверное имя или пароль!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { if (MessageBox.Show("Закрыть программу?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); break; } } } } private bool CheckUser(string name, string password) { return name.Equals("Вася") && password.Equals("123"); } } 

      Bulson , ATP for the answer and the train of thought, but probably I didn’t express myself correctly. This is how it is. Authorization form code:

       public partial class frmLogin : Form { frmMain f1 = new frmMain(); public frmLogin() { InitializeComponent(); Init_Data(); } private void btnLogin_Click(object sender, EventArgs e) { if (comboLogin.Text == "Вася" && txtPass.Text == "1234") { this.Hide(); f1.lblRole.Text = comboLogin.Text; f1.Show(); } else { txtPass.Clear(); MessageBox.Show("##########"); } Save_Data(); } 

      Here from the comboBox is passed the name of the logged in to the main form in the lbel lblRole. And on the main form shows "Vasya". As I said earlier that there is still a UserControl on the main form, a DataGridView lives in the UserControl.

      Code from UserControl

       private void b1_Load(object sender, EventArgs e)// LOAD { this.addZayavkiTableAdapter.Fill(this.zayavkiDataSet.addZayavki); frmMain fm = new frmMain(); if (fm.lblRole.Text == "Вася") { tsbSave.Enabled = false; btnDelete.Enabled = false; zayavkiDGV.ReadOnly = true; bindingNavigatorAddNewItem.Enabled = false; } } 

      When debugging, you can see that UserControl received not the user "Vasya", but what is specified in the lblRole properties on the main form LblRole properties

      The trouble is that I can’t get a name in UserControl, but I’ll get Label’s properties, UserNone. enter image description here

      • You are using answers that are not intended. Please use the option to edit the question to clarify it; and ask new questions separately. - Pavel Mayorov
      • I didn’t read your code very carefully, so I made my own example in classics, with the input of the name, and not its choice. Essentially this does not change, in my example, it is enough to change the TextBox to ComboBox. The main thing is that you have to run the login form from the main form, and not like you have the opposite. - Bulson
      • Bulson ATP, in the evening I will try to replay the code. PS and in my case, there is no way UserControl can calculate NOT properties? It feels like you need to finish right if ((fm.lblRole.Text == "Vasya") - to go to
      • I tried to use your code, the same story, UserControl reads the properties, at the output of User None - Vvyasya