Visual C # - Open form already created, Windows Forms

0

I have the initial form called "start", and a button called "run". Now I have created an additional form called "runf". How can I open "runf" from the "run" button. I have tried to look on the internet, and the answer is the following ..

Form Formulario = New Form();
Formulario.Show();

But I do not want to create a form from code, I want to open a form that I already have created. In VB.NET, it was done only with Form.Show (); Can someone explain to me why C # is totally different? Or I'm wrong, well, I have no idea.

    
asked by Máxima Alekz 07.07.2016 в 01:14
source

5 answers

2

To be able to call or even form, you must instantiate it VB.Net

Dim frm As New Form2()
frm.Show()

C #

frmBusqueda frm = new frmBusqueda
frm.Show
    
answered by 07.07.2016 в 01:33
1

I think this could help

How to: Display a form from another (Visual C #)

You'll see that there defines

private void button1_Click(object sender, System.EventArgs e)
{
    Form2 frm = new Form2();
    frm.Show();
}
// Create Form2.
public class Form2: Form
{
    public Form2()
    {
        Text = "Form2";
    }
}

use the new to create the instance of the form (remember the form is a class that must be instantiated) and then on the instance use the Show () to show the form visually.

Remember that in c # all objects are included in the form

    
answered by 07.07.2016 в 02:58
1

I present a solution that implements in a main form with links to windows contained.

//Variable global a la clase
 Private ventana As FrmVentana = Nothing


//On click sobre el link en la ventana principal al que despliega el el FrmVentana contenida
If (ventana Is Nothing OrElse Not ventana.CanFocus) Then
    ventana = New FrmVentana
    ventana.MdiParent = Me
    ventana.setearUser(Me.usuario)
    ventana.Show()
    ventana.WindowState = FormWindowState.Maximized
Else
    ventana.Focus()
End If

I hope it serves you, estol makes that when you click on the link if it was not created yet or because of something you can not show it, you create it. Otherwise, it shows the existing one.

Greetings

    
answered by 07.07.2016 в 19:41
1

I suggest this code. Use the form that appears by default without changing the name (remaining as Form1) and create a button, change the name to run1, doubleclick and paste the corresponding content in this text; Accommodate the rest as it appears in the example.

  

public partial class Form1: Form

     

{

public Form1()
  {
  InitializeComponent();
  }

private Form runf;

private void Form1_Load(object sender, EventArgs e)
  {
  runf = new Form()
    {
    Size = new Size (200,100),
    Location = new Point (200,250),
    ShowInTaskbar = false,
    WindowState = FormWindowState.Minimized,
    };

  runf.Show();
  // Resto del codigo del inicio de "start" (Form1) ...
  }

private void run1_Click_1(object sender, EventArgs e)
  {
  if (runf.WindowState == FormWindowState.Minimized)
    runf.WindowState = FormWindowState.Normal;
  else
    runf.Focus();
  }

// Resto de metodos del programa ...
     

}

     

I suggest you not use names like Start or Run, because they can match      with special words, or used for / by C # and give conflicts to      when interpreting them.

    
answered by 10.07.2016 в 16:18
1

If I understood correctly, you are working in Visual C #, your initial Form is "start", and the button called "run" is the one that will take you to the "runf" form, I have the code lines that you need and they are exactly the following:

    private void button_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2 ();
        f.Visible = true;
        this.Dispose(false);
    }

For this instruction to serve you, you have to take the following considerations:

"Form1" should be for your case "Start"; "f" can be any variable with which you want to identify the instruction; "Form2" must be for your case "Runf"; "Button" should be "Run".

Note that if you do not want the "Start" form to close when you open the "Runf" form, you should remove the line of code

    this.Dispose(false);

and ready once you take these considerations you try it, I hope it serves you.

    
answered by 03.10.2016 в 23:12