Hi all,
How to set default focus on button in winform using C#?
Thanks and regards,
Swapnil.
Hi all,
How to set default focus on button in winform using C#?
Thanks and regards,
Swapnil.
Please search forums before you post, 99.9% of questions have been answered before.
http://www.daniweb.com/techtalkforums/showthread.php?t=49872&highlight=set+focus
I need to set default focus (i.e. after enter user name and password when I press enter key the submit button must get click) to submit button in windows base programme not web base.
Please search forums before you post, 99.9% of questions have been answered before.
http://www.daniweb.com/techtalkforums/showthread.php?t=49872&highlight=set+focus
Thanks and regards,
Swapnil.
Ah I see ok. You need to Implement the IMessageFilter in your form to catch key events and process them.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace KeyHooks
{
public partial class Form1 : Form, IMessageFilter
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Application.AddMessageFilter(this);
}
public bool PreFilterMessage(ref Message msg)
{
Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
if (msg.Msg == WM_KEYDOWN && keyCode == Keys.Return)
{
DoButtonClick("You pressed return");
return true;
}
return false;
}
private void Form1_Load()
{
}
private void DoButtonClick(string message)
{
label1.Text = message;
}
private void button1_Click(object sender, EventArgs e)
{
DoButtonClick("you clicked me");
}
}
}
We can do it by AcceptButton property of Form.
Thanks and regards,
Swapnil.
Ah very good, much simpler yes.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.