Home / Internet / Link two shapes c. Adding forms. Interaction between forms. Create a Single Item Form or Details Form

Link two shapes c. Adding forms. Interaction between forms. Create a Single Item Form or Details Form

Last update: 31.10.2015

To add another form to the project, right-click on the project name in the Solution Explorer window and select Add (Add)->Windows Form...

Let's give the new form some name, for example, Form2.cs:

So, we have added a second form to the project. Now let's try to implement the interaction between the two forms. Let's say the first form will call the second form when the button is clicked. First, let's add a button to the first form Form1 and double-click on the button to go to the code file. So, we will get into the button click event handler, which is created by default after double-clicking the button:

Private void button1_Click(object sender, EventArgs e) ( )

Now let's add the call code of the second form to it. Our second form is called Form2, so first we create an object of this class, and then we call the Show method to display it on the screen:

Private void button1_Click(object sender, EventArgs e) ( Form2 newForm = new Form2(); newForm.Show(); )

Now let's do the opposite - so that the second form affects the first. While the second form does not know about the existence of the first. To fix this, you need to somehow transfer information about the first form to the second form. To do this, we will use passing a link to the form in the constructor.

So let's move on to the second form and move on to its code - right-click on the form and select View Code (View code). While it is empty and contains only a constructor. Since C# supports method overloading, we can create multiple methods and constructors with different parameters and call one of them depending on the situation. So, let's change the code file of the second form to the following:

Using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HelloApp ( public partial class Form2: Form ( public Form2() ( InitializeComponent(); ) public Form2(Form1 f) ( InitializeComponent(); f.BackColor = Color.Yellow; ) ) )

In fact, we just added here new constructor public Form2(Form1 f) , where we get the first form and set its background to yellow. Now let's go to the code of the first form, where we called the second form and change it to the following:

Private void button1_Click(object sender, EventArgs e) ( Form2 newForm = new Form2(this); newForm.Show(); )

Because in this case the this keyword represents a reference to the current object - the Form1 object, then when creating the second form, it will receive it (the link) and manage the first form through it.

Now, after clicking on the button, we will create a second form, which will immediately change the color of the first form.

We can also create objects for the current form:

Private void button1_Click(object sender, EventArgs e) ( Form1 newForm1 = new Form1(); newForm1.Show(); Form2 newForm2 = new Form2(newForm1); newForm2.Show(); )

When working with multiple forms, keep in mind that one of them is the main one - which is launched first in the Program.cs file. If we have a bunch of forms open at the same time, then when the main one is closed, the entire application closes, and with it all other forms.

If you have a long form for multiple items, you may sometimes want to drill down quickly to see more information about one of the items. In Access, this requires establishing a link between a multi-item form and a single-item form. When you select an item in a multi-item form, the single item form opens and displays additional details.

Notes:

Create Forms

In most cases, two forms in a granular configuration are based on the same table or query. If you already have a couple of forms you need to use (one is a table or multi-item form and the other is a single item or details form), you can skip this section. Otherwise, use the following procedures to create them.

Create a Table Form or Form with Multiple Elements

    In the Navigation Pane, select the table or query that contains the data you want to display in tabular form.

    Select items Creation > Other forms, and then - Multiple elements or table depending on the desired form type.

    Save and close the form.

For more information about multi-item forms, see Create a form using the Multiple Items tool .

Create a Single Item Form or Details Form

    In the Navigation Pane, click the table or query that contains the data you want to display in the details form. It will most likely be the same table or query that you used for the first form.

    Select items Creation > The form.

    Make the necessary changes, save and close the form.

For more information, see Create a form with the Shape tool .

Adding logic to open the details form

Now let's look at how to add a small macro to a multi-item or table form that will open the details form and display the correct item.

Don't worry if you don't have much experience with macros. You can simply copy and paste the XML from the procedure in this article.

    If you are using a table form, double-click it in the Navigation Pane to open it. For a multi-item form, right-click it in the Navigation Pane and select Layout mode.

    Press the F4 key to open the properties window if it is not already open.

    Click the column where you want to add the macro. This should be the column that, when clicked, will open the details form after running the macro. In the image above, the macro is added to the frmContactList column.

Advice:(TIP) You can add macros to multiple columns.

For more information about macros in computer databases, see


It is clear that if you create a type pointer<что там>::Form will work, but I need a form already created, with buttons etc. (in general, different controls), but manually (i.e. programmatically) I don’t want to prescribe them on a clean form (a lot of writing and not the fact that you get it right). This is what still confuses me ....... When I added the form to an already created project, in the new header file Form2.h it was written like this:

#pragma once
#include "Form1.h"

using namespace System;



using namespace System::Data;

namespace My1(

///
/// Summary for Form2



public ref class Form2: public System::Windows::Forms::Form
{
public:
Form2(void)
{
InitializeComponent();

}

Protected:

~Form2()
{
if (components)
{
delete components;
}
}

Private:



///
{
this->components = gcnew System::ComponentModel::Container();
this->Size = System::Drawing::Size(300,300);
this->Text = L"Form2";
this->Padding = System::Windows::Forms::Padding(0);
this->
}
#pragmaendregion
};
}

although in Form1.h it is written like this:

#pragma once
#include "Form2.h"

namespace My1(

Using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

///
/// Summary for Form1
/// WARNING: If you change the name of this class, you will need to change the
/// "Resource File Name" property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
public ref class Form1: public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//TODO: Add the constructor code here
}

Protected:
/// Clean up any resources being used.
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:

Private:
/// Required designer variable.
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
// button1
this->button1->Location = System::Drawing::Point(60, 76);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
// Form1
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);

}
#pragmaendregion

Static System::Windows::Forms::Form ^frm = gcnew System::Windows::Forms::Form();
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) (
frm->Show();
}
};
}

IN DIFFERENT PLACES THE CONNECTION OF THE NAMESPACE IS REGULATED!