0

Estoy haciendo una aplicación en Visual Studio 2017, en C#, quisiera saber como puedo mandar el valor de la variable

public static DialogResult BandaOption(string title, string promText, ref string value, int tipo)
    {
        Form frm = new Form();
        Label label = new Label();
        System.Windows.Forms.ComboBox combo = new System.Windows.Forms.ComboBox();
        Button buttonOK = new Button();
        Button buttonCancel = new Button();

        frm.Text = title;
        label.Text = promText;
        combo.Text = value;
        combo.Items.Add("Banda 1");
        combo.Items.Add("Banda 2");

        buttonOK.Text = "Aceptar";
        buttonCancel.Text = "Cancelar";
        buttonOK.DialogResult = DialogResult.OK;
        buttonCancel.DialogResult = DialogResult.Cancel;           

        label.AutoSize = true;
        combo.Anchor = combo.Anchor | AnchorStyles.Right;
        buttonOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        frm.ClientSize = new Size(396, 107);
        frm.Controls.AddRange(new Control[] { label, combo, buttonOK, buttonCancel });
        frm.ClientSize = new Size(Math.Max(300, label.Right + 10), frm.ClientSize.Height);
        frm.FormBorderStyle = FormBorderStyle.FixedDialog;
        frm.StartPosition = FormStartPosition.CenterScreen;
        frm.MinimizeBox = false;
        frm.MaximizeBox = false;
        frm.AcceptButton = buttonOK;
        frm.CancelButton = buttonCancel;

        DialogResult dialog = frm.ShowDialog();
        value = Convert.ToString(combo.SelectedItem); //--> el valor de esta variable
        return dialog;
    }

a otro método dentro del mismo windows forms, para poder guardar unos datos. Ya lo he intentado con otros códigos, pero no me funciona

2 respuestas 2

1

a ver si entendi tu problema, tienes un WinForms el cual tiene 2 metodos BandaOption y el metodo donde quieres recibir el valor de la variable, ejemplo MetodoRecepcionParametro y quieres utilizar el valor de la variable value dentro del segundo metodo. Si es asi, puedes hacerlo muy sencillo utilizando una variable global. Ejemplo:

        static string valorGlobal; //Variable global declaracion

    public static DialogResult BandaOption(string title, string promText, ref string value, int tipo)
    {
        Form frm = new Form();
        Label label = new Label();
        System.Windows.Forms.ComboBox combo = new System.Windows.Forms.ComboBox();
        Button buttonOK = new Button();
        Button buttonCancel = new Button();

        frm.Text = title;
        label.Text = promText;
        combo.Text = value;
        combo.Items.Add("Banda 1");
        combo.Items.Add("Banda 2");

        buttonOK.Text = "Aceptar";
        buttonCancel.Text = "Cancelar";
        buttonOK.DialogResult = DialogResult.OK;
        buttonCancel.DialogResult = DialogResult.Cancel;

        label.AutoSize = true;
        combo.Anchor = combo.Anchor | AnchorStyles.Right;
        buttonOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        frm.ClientSize = new Size(396, 107);
        frm.Controls.AddRange(new Control[] { label, combo, buttonOK, buttonCancel });
        frm.ClientSize = new Size(Math.Max(300, label.Right + 10), frm.ClientSize.Height);
        frm.FormBorderStyle = FormBorderStyle.FixedDialog;
        frm.StartPosition = FormStartPosition.CenterScreen;
        frm.MinimizeBox = false;
        frm.MaximizeBox = false;
        frm.AcceptButton = buttonOK;
        frm.CancelButton = buttonCancel;

        DialogResult dialog = frm.ShowDialog();
        value = Convert.ToString(combo.SelectedItem); //--> el valor de esta variable
        valorGlobal = value;  //Se asigna a la variable global
        return dialog;
    }

    public void Metodo2() {
        //Usar variable global 
        var valor = valorGlobal;
    }

Espero te sirva.

1
  • si, de hecho lo resolví así, de todos modos gracias por la ayuda
    – Macx
    el 24 abr. 2019 a las 18:41
0

la clase Form contiene una propiedad Tag de tipo object donde puedes asignarle cualquier objeto

frm.Tag = combo.SelectedItem;

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.tag?view=netframework-4.8

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.