Windows�t�H�[���ŕʃX���b�h����R���g���[���𑀍삷��ɂ́H�F.NET TIPS

» 2005�N06��17�� 05��00�� ���J
[�����F�M�C�f�W�^���A�h�o���e�[�W]

���̋L���ŕ����邱��

  • Windows�t�H�[����UI�R���g���[���́A�쐬���ł���UI�X���b�h���炵�����ڑ���ł��Ȃ�
  • �ʃX���b�h����UI�𑀍삵�����ꍇ�́A�R���g���[����Invoke���\�b�h���g���A������UI�X���b�h�Ɉ˗�����K�v������
  • InvokeRequired�v���p�e�B��Invoke���K�v���ǂ������m�F���邱�ƂŁA���S�ȃR�[�h���L�q�ł���
.NET TIPS
Insider.NET


�u.NET TIPS�v�̃C���f�b�N�X

�A�ږڎ�

�@Windows�t�H�[���ŃX���b�h���쐬�����ꍇ�A�t�H�[����t�H�[����̃R���g���[���ɑ΂��ẮA���̃X���b�h����̑���i�t�H�[����R���g���[�������ƒ��\�b�h�̌Ăяo����v���p�e�B�̓ǂݏ����j�͓��삪�ۏ؂���Ȃ��B�{�e�ł͂��̂悤�ȏ����������S���ɍs�����߂̃v���O���~���O�ɂ‚��ĉ������B

�t�H�[�J�X�̈ړ����s���T���v���E�v���O����

�@�Ⴆ�΁AVisual Studio .NET�i�ȍ~�AVS.NET�j��Windows�A�v���P�[�V�����̃v���W�F�N�g��V�K�쐬���A���̉�ʂ̂悤�ɁA2�‚̃e�L�X�g�{�b�N�X�iTextBox1��TextBox2�j��1�‚̃{�^���iButton1�j��z�u�����Ƃ���B

VS.NET��2�‚̃e�L�X�g�{�b�N�X��1�‚̃{�^����z�u����Windows�t�H�[�� VS.NET��2�‚̃e�L�X�g�{�b�N�X��1�‚̃{�^����z�u����Windows�t�H�[��

�@���̃A�v���P�[�V���������s����ƁA�N�����ɂ̓t�H�[�J�X��TextBox1�ɐݒ肳���B���ɁA�{�^�����N���b�N���ꂽ�Ƃ��ɁATextBox2�Ƀt�H�[�J�X���ݒ肳���悤�ɂ��Ă݂悤�B

�@����ɂ�VS.NET��Ŕz�u�����{�^�����_�u���N���b�N���A����ɂ�萶�����ꂽ�C�x���g�E�n���h���Ɏ��̂悤�ȃR�[�h���L�q����΂悢�B

private void button1_Click(object sender, System.EventArgs e)
{
  textBox2.Focus();
}

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  TextBox2.Focus()
End Sub

TextBox2�R���g���[���Ƀt�H�[�J�X��ݒ肷��R�[�h�i��FC#�A���FVB.NET�j

�@Focus���\�b�h�͂��̃R���g���[���Ƀt�H�[�J�X��ݒ肷�邽�߂̂��̂��B���R�Ȃ���A���̃R�[�h�͐��������s�����B

�ʃX���b�h����R���g���[���𑀍삷��������

�@���ɁA�{�^�����N���b�N���ꂽ�Ƃ��ɃX���b�h���N�����A���̃X���b�h��Focus���\�b�h���Ăяo���Ă݂�B���̃R�[�h�͈ȉ��̂悤�ɂȂ�B

private void button1_Click(object sender, System.EventArgs e)
{
  Thread t = new Thread(new ThreadStart(worker));
  t.Start();
}

void worker()
{
  textBox2.Focus(); // ������Ăяo��
}

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim t As New Thread(New ThreadStart(AddressOf worker))
  t.Start()
End Sub

Sub worker()
  TextBox2.Focus() ' ������Ăяo��
End Sub

�ʃX���b�h����R���g���[���𑀍삷�������R�[�h�i��FC#�A���FVB.NET�j
�v���O�����̐擪��System.Threading���O��Ԃ��Q�Ƃ���K�v������B

�@���̃R�[�h�ł́Abutton1_Click���\�b�h�̓R���g���[�����쐬���ꂽ�X���b�h�i�ȉ��A���C���E�X���b�h�j�Ŏ��s�����̂ɑ΂��āAworker���\�b�h�͕ʃX���b�h�Ŏ��s�����B���̂��߁Aworker���\�b�h���ŌĂяo���Ă���TextBox2��Focus���\�b�h�̎��s�́A���̓��삪�ۏ؂���Ȃ��B���ۂɃv���O���������s���{�^�����N���b�N���Ă��t�H�[�J�X�͈ړ����Ȃ��͂����B

Invoke���\�b�h�ɂ�郁�\�b�h�̌Ăяo��

�@Control�N���X�iSystem.Windows.Forms���O��ԁj�ɂ́A�ʃX���b�h����R���g���[���𑀍삷��ꍇ�Ɏg�p�ł���Invoke���\�b�h���p�ӂ���Ă���i�t�H�[�����n�߂Ƃ��邷�ׂẴR���g���[���͂��̃��\�b�h���p�����Ă���j�BInvoke���\�b�h���g���ƁA�R���g���[���ɑ΂��鑀������C���E�X���b�h�Ŏ��s�����邱�Ƃ��ł���B

�@��̓I�ɂ́A���C���E���\�b�h��Ŏ��s���������\�b�h�i�R���g���[���ւ̑�����܂ށj�ɑΉ������f���Q�[�g���쐬���A���̃f���Q�[�g�̃C���X�^���X��Invoke���\�b�h�̃p�����[�^�Ŏw�肵�ČĂяo���΂悢�BInvoke���\�b�h���R���g���[�������ƒ��\�b�h�ł��邪�A���̌Ăяo���͕ʃX���b�h����ł����삪�ۏ؂���Ă���B

�@���̃R�[�h�́A��L�̌�����R�[�h��Invoke���\�b�h���g���ď������������̂��B

private void button1_Click(object sender, System.EventArgs e)
{
  Thread t = new Thread(new ThreadStart(worker));
  t.Start();
}

delegate bool FocusDelegate();

void worker()
{
  // textBox2.Focus()�̎��s
  Invoke(new FocusDelegate(textBox2.Focus));
}

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim t As New Thread(New ThreadStart(AddressOf worker))
  t.Start()
End Sub

Delegate Function FocusDelegate() As Boolean

Sub worker()
  ' textBox2.Focus()�̎��s
  Invoke(New FocusDelegate(AddressOf TextBox2.Focus))
End Sub

�ʃX���b�h����Invoke���\�b�h�ɂ��R���g���[���𑀍삷��R�[�h����1�i��FC#�A���FVB.NET�j

�@���̃R�[�h�ł́ATextBox2��Focus���\�b�h�����C���E�X���b�h��Ŏ��s����邽�߁A�v���O�����͐��������삷��B

�@�Ȃ���L�̃R�[�h�ł́A�e�L�X�g�{�b�N�X��Focus���\�b�h�ɑ΂��ăf���Q�[�g���쐬���Ă��邪�A�ʏ�͎��̂悤�Ƀ��\�b�h��1�쐬���i���̏ꍇ�ɂ�SetFocus���\�b�h�j�A������f���Q�[�g�o�R�ŌĂяo���悤�ɂ���̂���ʓI���B

private void button1_Click(object sender, System.EventArgs e)
{
  Thread t = new Thread(new ThreadStart(worker));
  t.Start();
}

delegate void SetFocusDelegate();

void SetFocus()
{
  textBox2.Focus();
}

void worker()
{
  Invoke(new SetFocusDelegate(SetFocus));
}

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim t As New Thread(New ThreadStart(AddressOf worker))
  t.Start()
End Sub

Delegate Sub SetFocusDelegate()

Sub SetFocus()
  TextBox2.Focus()
End Sub

Sub worker()
  Invoke(New SetFocusDelegate(AddressOf SetFocus))
End Sub

�ʃX���b�h����Invoke���\�b�h�ɂ��R���g���[���𑀍삷��R�[�h����2�i��FC#�A���FVB.NET�j

Invoke���\�b�h���K�v���ǂ���������InvokeRequired�v���p�e�B

�@Control�N���X�ɂ́AInvoke���\�b�h���g���K�v�����邩�ǂ������`�F�b�N���邽�߂�InvokeRequired�v���p�e�B���p�ӂ���Ă���B���̃v���p�e�B�́A������Ăяo�����X���b�h�����C���E�X���b�h���ǂ����𒲂ׁAtrue���邢��false��Ԃ��B

�@���̃R�[�h�́A��L�̃R�[�h��InvokeRequired�v���p�e�B�𗘗p���ď������������̂��B���̃R�[�h��SetFocus���\�b�h�́A���C���E�X���b�h����ł��ʃX���b�h����ł��Ăяo�����Ƃ��ł���悤�ɂȂ��Ă���i�ʃX���b�h����SetFocus���\�b�h���Ăяo�����ꍇ�ɂ́A���ʓI��SetFocus���\�b�h��2�x���s����邱�ƂɂȂ�j�B

private void button1_Click(object sender, System.EventArgs e)
{
  Thread t = new Thread(new ThreadStart(worker));
  t.Start();
}

delegate void SetFocusDelegate();

void SetFocus()
{
  if (InvokeRequired)
  {
    // �ʃX���b�h����Ăяo���ꂽ�ꍇ
    Invoke(new SetFocusDelegate(SetFocus));
    return;
  }
  textBox2.Focus();
}

void worker()
{
  SetFocus();
}

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim t As New Thread(New ThreadStart(AddressOf worker))
  t.Start()
End Sub

Delegate Sub SetFocusDelegate()

Sub SetFocus()
  If InvokeRequired Then
    ' �ʃX���b�h����Ăяo���ꂽ�ꍇ
    Invoke(New SetFocusDelegate(AddressOf SetFocus))
    Return
  End If
  TextBox2.Focus()
End Sub

Sub worker()
  SetFocus()
End Sub

InvokeRequired�v���p�e�B�𗘗p�����R�[�h�i��FC#�A���FVB.NET�j

�@�����X���b�h���ł����Ă��AInvoke���\�b�h�𗘗p�����R���g���[���̑���͓��ɖ��Ȃ��Ǝv���邪�A����X���b�h����̃R���g���[���̑����Invoke���\�b�h�ōs���͖̂��ʂł���BInvokeRequired�v���p�e�B���g���΁A���̂悤�Ȗ��ʂȏ������Ȃ����Ƃ��ł���B

�J�e�S���F�N���X�E���C�u�����@�����ΏہF�R���g���[��
�J�e�S���F�N���X�E���C�u�����@�����ΏہF�}���`�X���b�h
�g�p���C�u�����FControl�N���X�iSystem.Windows.Forms���O��ԁj
�g�p���C�u�����FThread�N���X�iSystem.Threading���O��ԁj


�u.NET TIPS�v�̃C���f�b�N�X

�u.NET TIPS�v

Copyright© Digital Advantage Corp. All Rights Reserved.

�A�C�e�B���f�B�A����̂��m�点

�X�|���T�[����̂��m�点PR

���ڂ̃e�[�}

���́uAI�R�[�f�B���O�v�͖{���ɕK�v���H
Microsoft �� Windows�őO��2026
4AI by ��IT - AI�����A�������A���A������
���[�R�[�h�^�m�[�R�[�h �Z���g���� by ��IT - IT�G���W�j�A���r�W�l�X�̒��S�Ŋ��􂷂�g�D��
Cloud Native Central by ��IT - �X�P�[���u���Ȕ\�͂�g�D��
�V�X�e���J���m�E�n�E �y�����i�r�zPR
���Ȃ��ɂ������߂̋L��PR

RSS�ɂ‚���

�A�C�e�B���f�B�AID�ɂ‚���

���[���}�K�W���o�^

��IT�̃��[���}�K�W���́A �������A���ׂĖ����ł��B���Ѓ��[���}�K�W�������w�ǂ��������B