control

Page update date :
Page creation date :

Controls are often used in tools and the like, so I would like to briefly explain how to use them. We'll use the previous Tips project to illustrate it.

First, since the control is basically placed in a form, right-click MainForm.cs from Solution Explorer and click Show Designer. (You can also double-click MainForm.cs to open it.)

デザイナの表示

フォームのデザイナ

Then open the toolbox on the left. (If there is none, select "Toolbox" from "View" in the menu)

ツールボックス

You will then see a list similar to the one below. This will be a list of controls and components that can be placed.

コントロールのリスト

Let's use the "Button" at the top. A Button is a button out of its own state, and is basically a control that allows you to do something by pressing it. First, click on "Button". The mouse cursor should be the icon for the button.

ボタン

Then try clicking or dragging the form to the size you want to place. You should be able to place the button on the form.

ボタン配置

Now, try running it once. Maybe the error shouldn't happen, so you should be able to start normally. You should be able to press the button properly.

実行

By the way, you can press the button, but nothing happens when you press it. You haven't written what you're going to do yet. Still, the basic mechanics are in place from the start, so the programmer doesn't have to worry about extra work.

Close the launched form. It is very easy to do if you just arrange it this way.


Now let's use the controls to create a simple event. Processing with controls is basically called an event.

From the toolbox, place "Label" and "TextBox" in the form respectively. The size and position can be appropriate.

テキストボックスとラベル配置

The process to be performed here is "When you enter in the text box and press the button, the contents entered in the label are displayed". Therefore, the only event used here is "when the button is pressed". Since the text box is used for input and the label is only used for output, there is no need to create these two events.

Now, click the button to select it. The selected control has a border. By the way, you can change the size by dragging the square in the frame.

選択

Then open the properties. Right-click the button on the right side of the window, or if it doesn't exist, and choose Properties.

ボタンのプロパティ

You can make some small settings about the button, but for the time being, you won't change anything, so open the event. The event opens by clicking the Lightning Bolt at the top of the Properties window.

When it opens, look for the "Click" event and double-click on it. This should add a method to the MainForm.cs that describes what happens when the button is clicked.

Click イベント

コード追加

Let's describe the process here. NET Framework is very well done, and this time it only takes one line to add it. Enter the following code with additional red parts:

private void button1_Click(object sender, EventArgs e)
{
  this.label1.Text = this.textBox1.Text;
}

What we're doing here is copying (assigning) "string displayed in text box" to "text displayed in label". That's it. Let's try running it.

テキスト入力

Enter the characters in the text box as shown above. Then press the button and the label should change to the same string as the text box as shown below.

文字列コピー


This time, I explained how to use the controls briefly. There are many other controls, so please give it a try. The instructions on how to use and explain the controls are explained in detail on other websites, so you may want to search and find them.

DirectX Tips also use controls from time to time, so if you learn how to use them, you should be able to apply them quite a bit.