I have a button inside a form which is itself inside a panel and that panel is also inside a panel. I want to set focus to that button as soon as the page loads. I have tried many different methods to achieve focus but unable to do so. Can anyone please guide me?
private void directionform_Load(object sender, EventArgs e)
{
this.ActiveControl = button1;
button1.Focus();
button1.Select();
}
Related
I am using a tab in my program to switch between two forms. I put the code required to switch between forms within the tabPage1_Click event, but it doesn't trigger when the tab is clicked.
I attached the code and properties of the tab. Please let me know if any other information is required to know the problem. Thanks.
private void tabPage1_Click(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.ShowDialog();
this.Close();
}
There are 2 things involved here. Tab control and Tab pages. Tab Control is the parent object which has multiple Tab pages in it.
You have event handler for Tab Page which is tabpage1_Click and not for Tab Control.
tabpage1_Click will be triggered when you click on tab page 1(not on the tab page header).
If you need to capture an event when you click on tab page header use Tab Control click event, something like below.
private void tabControl1_Click(object sender, EventArgs e)
{
//Your code goes here
}
To access the properties of the tab page use tabControl1.SelectedTab
I've been racking my brain over this for a while, its been a while since I had to do this and know its possible I've done it in another project in the past that I don't have a backup of to refer to.
I have a Login View on a page inside the login view is 2 panels one panel with a login control (to login) and one panel with a createuserwizard (to register) and a second button to click to register.
I'm trying to hide the panel with the login control and show the panel with the register control via a button click but all I end up with is a null reference exception.
this is what I have currently.
protected void Register_Click(object sender, EventArgs e)
{
FindControl("LoginView1").FindControl("LoginPanel").Visible = false;
FindControl("LoginView1").FindControl("RegPanel").Visible = true;
}
I appreciate any help thanks.
I figured out what the problem was so I'll leave the question here for anyone who may have the same problem and stumble across this
I was so used to working with controls from a master page but within a page that's inside the master page you don't need the first findcontrol its simply:
protected void Register_Click(object sender, EventArgs e)
{
LoginView1.FindControl("LoginPanel").Visible = false;
LoginView1.FindControl("RegPanel").Visible = true;
}
Initially i was having a picture box which can be moved on the form by user from one place to another.
I have handled the events for the picture box and it was moving perfectly.
But now user wants to display a text below the picture. So I thought to create a custom control dynamically and add that picture box and a label control inside the user control.
I also set the dock properties of controls to TOP and Bottom. Now my user control is completely covered with the sub controls.
After that i want to handle the mouse events for the user control. But unfortunately that is not working for me.
As per my understanding, now i cannot access the user control instead i am having access to sub controls in user control, so the mouse events for user control are not working.
Correct me if am wrong, and provide any solution.
well, the mouse event like MouseDown and MouseUp occurs only when the mouse is doing something on the specific control. the best offer i can give you is to catch each mouse event in the controls and call a method on the userControl
public UserControl1()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(this.UserControl1_MouseDown);
this.comboBox1.MouseDown += new MouseEventHandler(this.comboBox1_MouseDown);
}
private void UserControl1_MouseClick(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void UserControl1_MouseDown(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void comboBox1_MouseDown(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void UCMouseDown()
{
// Your code
}
I have several tabs in a form. Each tab has one textbox. When I enter tabpage1 I have managed to set the focus on the textBox1. When I press a button in tabpage1 I jump to a random tab in the controller. What I want now is to have the focus set on textBox in the active tabpage. I have tried using tabpage_Enter event, but it does not seem to work. My code look like this :
private void tabPage2_Enter(object sender, EventArgs e)
{
textBox2.Select();
}
Any suggestions?
I think you need to use SelectedIndexChanged event of TabControl instead of _Enter, using Enter event, focus will change to textBox2 every time the cursor enter the tabPage control.
You can use the Focus() method set the focus on a textbox. I would probably set on the tabPage_Enter event.
private void tabPage_Enter(object sender, EventArgs e){
{
var tab = sender as tabPage;
if(!tab.Focused) tab.focus();
}
there are some buttons on the top of the winform, and when I click one of them, the panel below will load different predefined panel, how can I implement this ?
please see this example:
Here's a solution using a standard WinForms TabControl, where the Tabs are hidden at run-time, but of course they are available at design-time.
Assumptions :
You don't want to get into creating OwnerDrawn Tabs, which is possible.
A standard WinForms TabControl will meet all your design-time needs.
Code :
In the Form Load event of the Form that hosts your TabControl use code like this :
tabControl1.Region = new Region(tabControl1.DisplayRectangle);
To hide the Tabs.
Then, "wire" up your buttons to handle selecting the different TabPages in the TabControl. Obviously you could do this in a more elegant way than this :
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages[0];
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages[1];
}
Note : if you want to insert secondary Forms or UserControls into the TabPages of a TabControl : that's not a problem : of course simpler to use UserControls. Insert them into the Controls collection of each TabPage and set their 'Dock Property to 'DockStyle.Fill.
Note : there are fancier ways you can hide the Tabs, like using a derived TabControl as shown here on CodeProject : TabControl on a WinForm without showing the Tab header? There are other solutions out there that use a modified WndProc. They're not hard to find.
I don't know exactly what you're trying to do, but if you've got a Panel on your form named contentArea and a bunch of user controls created (but not on the form), then you could use this as an event handler for a button:
public void myButton_Click(object sender, EventArgs e) {
contentArea.Controls.RemoveAt(0);
contentArea.Controls.Add(new MyUserControl());
}
...though as other people have said, a tab control would be better in this case.
What you can do is have them each in a separate Panel. Set the Visible property to false to each. When the Click event on the button, set the Visible property of all of them to false and set the one you want shown's Visible to true.
For example if you have two form Form1 and Form2 and you want to load form2 inside from1. when you press a button to load form2 the code is like this
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Controls.Clear();
foreach(Control c in this.Controls)
{
this.Controls.Add(c);
}
}
this code will load all the controls in the form2 into form1.