How to pass values of controls between different windows?

Asked

Viewed 1,729 times

3

and I’m having a question here about programming in C#, which I’m in doubt of how to do.

Well, first I have a program C# of Windows Form Application, in it I created 2 Forms, being the main Form1, where has a menu that would be to choose as if it were a script, in general the complete project would be to log a user and password in a certain internet URL.

But that’s it, and in Form1, I put 4 radiobutton, and in these radiobutton each should contain a registration and password. And I also put a button to start such a script. And when it was clicked on such button to start it calls form2, which in it was placed the Webbrowser component.

Even quoted above is ok. My doubt would be as follows. I wanted to know some method of getting back the value of the radiobutton to the other form when clicking the start button. In the radiobutton part it would be a code in the example below, containing the two values and taking the option that is marked, however I wanted to know how I would return this value to go in another form that would be used in the component Webbrowser

if (radioButton0.Checked)
{
    rb_matricula = 8020137;   //André Venicios
    rb_senha     = "senha0";
}
else if (radioButton1.Checked)
{
    rb_matricula = 7011288;   //Clériston Morais Santos
    rb_senha     = "senha1";
}
else if (radioButton2.Checked)
{
    rb_matricula = 5010940;   //Daniel Ribeiro Bandeira
    rb_senha     = "senha2";
}

My Start Script button just put the same redirecting to form2

private void btn_Iniciar_Click(object sender, EventArgs e)
{
    frmBrowser navegador = new frmBrowser();
    navegador.ShowDialog();
}

Good now going to form2, it will start the Webbrowser component already at the following link

webBrowser1.Navigate("https://172.16.0.47/pessoal/");

Once done, I put the same to get some data from the URL of the existing site. And fill this field, with the value selected on the radiobutton of Form1, that would be this field.

bool pWeb = false;

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if(!pWeb)
    {
        webBrowser1.Document.GetElementById("usu_login").InnerText = "7011288";
        webBrowser1.Document.GetElementById("usu_senha").InnerText = "senha1";
    }
    pWeb = true;
}

And the final question would be to take the value returned from the radiobutton, and put it in the place where the license plate and the password are. after Innertext, by arranging another method to pass it, after it grabs the element by the ID and inserts it.

If anyone can help me in this doubt of mine, I will be very grateful, because I have stopped in this way how I will pass it and what method I could use to put in place of Innertext.

2 answers

4


You can pass on the necessary information to the second form through the constructor and store this information in private fields so that they can then be used by the method webBrowser1_DocumentCompleted:

private readonly string _username;
private readonly string _password;

public frmBrowser(string username, string password)
{
    _username = username;
    _password = password
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if(!pWeb)
    {
        webBrowser1.Document.GetElementById("usu_login").InnerText = _username;
        webBrowser1.Document.GetElementById("usu_senha").InnerText = _password;
    }

    pWeb = true;
 }

This way, in the button click event just build the second form:

frmBrowser navegador = new frmBrowser(rb_matricula, rb_senha);

EDIT:

As regards the radiobuttons, assuming that I understood well, that is, to have a method that finds the radiobutton selected and return the registration and password.

My solution to the problem would be:

private class InformacaoUtilizador
{
    public string Matricula { get; private set; }

    public string Senha { get; private set; }

    public InformacaoUtilizador(string matricula, string senha)
    {
        Matricula = matricula;
        Senha = senha;
    }
}

So with the class you can create a new instance for each of the matricula/password and assign that instance to the property Tag of radiobutton (in the Form constructor or in the event Load):

radioButton0.Tag = new InformacaoUtilizador("matricula", "senha");
radioButton1.Tag = new InformacaoUtilizador("matricula", "senha");
radioButton2.Tag = new InformacaoUtilizador("matricula", "senha");

Next, create a method that finds the radiobutton selected and returns the object in Tag of radiobutton (if there isn’t one radiobutton selected, returns null):

private InformacaoUtilizador SelecionarInformacao()
{
    RadioButton res = Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);

    if (res == null)
        return null;

    return res.Tag as InformacaoUtilizador;
}

Finally, inside the event Click button:

private void button1_Click(object sender, EventArgs e)
{
    var info = SelecionarInformacao();
    if (info == null)
    {
        MessageBox.Show("Tem de seleccionar um botão.");
        return;
    }

    frmBrowser navegador = new frmBrowser(info.Matricula, info.Senha);
    frmBrowser.ShowDialog();
}
  • Right, the issue of the radiobutton I could do this way, creating a new function, however how would return it to go to the button click, and it would only get the return of the value of the radiobutton that is checked?

  • 1

    I got it, it really worked out now, thank you very much

1

A way to access the data from form main in high school is setting the form main as secondary Parent. For this we can use the constructor of the form by passing as argument the form leading. It is more or less like this, in the line where you declare and instance the second form:

private void btn_Iniciar_Click(object sender, EventArgs e)
{
    frmBrowser navegador = new frmBrowser(this);
    navegador.ShowDialog();
}

This way it is possible to access the controls of form main by the object parent. Example:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if(!pWeb)
    {
        webBrowser1.Document.GetElementById("usu_login").InnerText = parent.rb_matricula;
        webBrowser1.Document.GetElementById("usu_senha").InnerText = parent.rb_senha;
    }

    pWeb = true;
 }  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.