C# How to make a string function execute active command?

Asked

Viewed 212 times

-1

I’m developing a C# project and I’m stuck in a mistake when it comes to moving from one form to another. The main form is called Main and the new Form1... well... I can send a variable string to another, but then it does not execute anything other than message box.

Form1 = sending to Main

public void Button1_Click(object sender, EventArgs e)
{
    string pid = ltBox.SelectedItem.ToString();
    string valor = ltBox.SelectedItem.ToString();
    pid = pid.Substring(0, pid.IndexOf('-', 0));
    if (!pid.Equals(""))
    {
        Main destino = new Main();
        destino.funcao(valor);
        this.Hide();
    }


}

Main receiving useless form...

public void funcao(string valor) {
    MessageBox.Show("deu certo uhuuu sqn");
    label1.Text = valor;

}

Unfortunately I can get the message that it worked, but the label1 does not change, and nothing else I do besides messagebox seems to work... someone can give a help there???

  • You have prompted a new class object Main, but you didn’t execute the method destino.Show() to display the form.

  • missing put . Show()

  • But then, the Form1 is to be a secondary form, I did not give IDE to open another

2 answers

0

Goodnight,

In case your question was how to pass a parameter to another form. You can pass the parameter by the target form constructor.

Example: //C#

static class Program
{
    static void Main()
    {
        //Codigo no formulario pai
        var frmMsg = new frmMensagem("Ola", "Mundo");
        frmMsg.ShowDialog();
    }
}

//Formulario Alvo.
public class frmMensagem {

    public frmMensagem(string parametroOla, string parametroMundo) 
    {
        InitializeComponent();
        lblOla.Text = parametroOla;
        lblMensagem.Text = parametroMundo;
    }
}

0

Pass a Array parameters, or only the parameters, you want to instantiate your class Main right in the builder of the same:

1. Example using Array parameter

public void Button1_Click(object sender, EventArgs e)
{
    string pid = ltBox.SelectedItem.ToString();
    string valor = ltBox.SelectedItem.ToString();
    pid = pid.Substring(0, pid.IndexOf('-', 0));
    if (!pid.Equals(""))
    {
        string[] parametros = {"Olá mundo!", "Foo Bar"};

        Main destino = new Main(parametros);
        this.Hide();
    }
}

And then, do it in your builder Main.cs:

public Main(string[] parametros) {
    InitializeComponent();

    this.label1.Text = parametros[0];   // Olá, mundo!
}

2. Example using parameters directly as arguments

public void Button1_Click(object sender, EventArgs e)
{
    string pid = ltBox.SelectedItem.ToString();
    string valor = ltBox.SelectedItem.ToString();
    pid = pid.Substring(0, pid.IndexOf('-', 0));
    if (!pid.Equals(""))
    {
        Main destino = new Main("Olá, mundo");
        this.Hide();
    }
}

And then, do it in your builder Main.cs:

public Main(string textoDoLabel) {
    InitializeComponent();

    this.label1.Text = textoDoLabel;   // Olá, mundo!
}

Because my code wasn’t working?

You were calling the method funcao from another Thread from which the Main, and then tried to change a property within it. This is illegal in the . NET Framework, you need to create a Callback to change this variable.

If you don’t want to create a callback, you can leave the property Checkforillegalcrossthread for false and then disable the Runtime check:

Control.CheckForIllegalCrossThreadCalls = false;

And in the end, don’t forget Main.Show().

Browser other questions tagged

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