How to pass control values between different windows?

3

And I have a question here about programming in C #, which I'm not sure how to do.

Well, I'm first with a Windows Form Application C # program, in it I created 2 forms, form1 being the main one, where there is a menu that would be to choose as if it were a script, in general the whole project would be for log in to a user and password at a given internet URL.

But that's it, and in form1, I put 4 radiobutton, and in these radiobutton each one should contain a registration and password. And I even put a button to start that script. And when it was clicked on that button to start it calls form2, which was placed in the WebBrowser component.

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

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 only put the same redirect 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 in the following link

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

Once that's done, I've put it in to get some data from the existing site URL. And fill this field with the value selected in the radiobutton of form1, which 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 doubt would be to get the value returned from the radiobutton, and put in the place where the registration and the password are. after InnerText, arranging another method to pass it, after it takes the element by the ID and inserts it.

If someone can help me with this question, I will be very grateful, as I caught in this way how I will pass it and what method I could use to put it in place of InnerText.

    
asked by anonymous 29.07.2014 / 05:20

2 answers

4

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

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;
 }

So, in the click event of the button, just build the second form :

frmBrowser navegador = new frmBrowser(rb_matricula, rb_senha);

EDIT:

For radiobuttons , assuming I understand well, that is, having a method that finds radiobutton selected and returns the enrollment 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 enrollment / password instance and assign this instance to the Tag property of the radiobutton (in the Form constructor or in the Load event):

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

Then, you create a method that finds radiobutton selected and returns the object that is in Tag of radiobutton (if no radiobutton is 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;
}

Lastly, within the% button% event:

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();
}
    
29.07.2014 / 10:52
1

One way to access the data from the form master on the secondary is to set the form master to the parent of the secondary. For this we can use the constructor of form secondary passing as argument form principal. It looks more or less like this, in the line where you declare and instantiate the second form:

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

In this way it is possible to access the controls of form main% by 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;
 }  
    
29.07.2014 / 14:40