2

Possible Duplicate:
(c# + windows forms) Adding items to listBox in different class

I want to get the combobox value in form1 and use it at form2, because the value will return another data from the registered user

public void povoacboxcliente()
{
    List<SM.BancoDados.BD.Model.Clientes> lstClientes = new List<SM.BancoDados.BD.Model.Clientes>();
    ClienteFlow flow = new ClienteFlow();

    lstClientes = flow.RetornaClientes();

    cboxCliente.DataSource = lstClientes;
    cboxCliente.DisplayMember = "Nome";
    cboxCliente.ValueMember = "Id";    
}

Now the value member (Id) will return the sex of the member, that is on database, this part is ok, but what I want is to do the operation in another form.. Here is the code that I'm trying on form2

public void enviasexo()
    {
        EnviarComando("0238373b3be503");
        idClient = Convert.ToInt32(cboxCliente.SelectedValue); 

        UsuarioFlow usuarioFlow = new UsuarioFlow();

        string combo = cboxCliente.SelectedValue.ToString();

        string sexo = usuarioFlow.RetornaSexo(combo);

        if (sexo == "M")
        {
            Thread.Sleep(2000);
            EnviarComando("0232343b3bdc03");
            Thread.Sleep(200); //envia comando
        }
        else if (sexo == "F")
        {
            Thread.Sleep(2000);
            EnviarComando("0232353b3bdd03");
            Thread.Sleep(200);
        }
    }

the "cboxCliente" was used in form1

Thanks People!

Community
  • 1
  • 1
José Flávio
  • 559
  • 5
  • 11

2 Answers2

4

One way is to make the ComboBox public in Form1.Designer.cs

then access the ComboBox from Form2

Form Form1Object = new Form1();
Form1Object.cboxCliente.SelectedValue.ToString();

See similar answer at

Stack Overflow Answer for other similar question

Community
  • 1
  • 1
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
0

Please use one of State management techniques which are available in Asp.Net when passing values in different forms.

see this
http://www.codeproject.com/Articles/331962/A-Beginner-s-Tutorial-on-ASP-NET-State-Management

Client side state management techniques

View State Control State Hidden fields Cookies Query Strings

Server side state management techniques

Application State Session State

As a best practise form 2 should not be shown to user if one of the control value is strictly dependant on form 1...input from the user. apply page validation on form 2 and redirect user to form1 if the values from the drop down is not selected..

hope this helps [shaz]

Shazhad Ilyas
  • 1,183
  • 8
  • 14