tabPage1_Click event not triggering - c#

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

Related

Set focus on the child control inside a panel

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();
}

Event for everything on a form C#

I am trying to make a click counter for everything on my form.
My form consists of textboxes, buttons, pictureboxes and labels.
What my problem is that my picutrebox is covering the whole form because I want it as a background picture. So when I have the private void Form1_Click(object sender, EventArgs e) event on it doesn't register the clicks that are on the picture box. And when I have private void pictureBox2_MouseClick(object sender, MouseEventArgs e) event the code it doesn't register any of the clicks I do on the buttons or text boxes. Is there an event for a click on absolutely everything on the form?
There are various ways you can handle this.
One way is to send the picture box back so your buttons come on top like this:
pictureBox.SendToBack();
Or you can attach an event handler to all of your controls like this:
foreach (Control control in Controls)
{
control.Click += Control_Click;
}

Insert existing Buttons into Context Toolstrip - Winforms

I have a GUI and some buttons in WinForms. Now I want to create a Toolstrip Menu and move the buttons into the menu. Is it possible without copying the whole code into the new Toolstrip entries? I just want to link the buttons somehow.
Thanks
Yes and no. It is not possible to move the existing buttons into the ToolStrip but you can create new ones and rewire them:
first: look for the name of the current event handler by selecting the old button, press F4 and switch to the action menu (the lightning bolt in the properties window):
second: select the new button and pick the same event handler as for the old button:
finally you can remove the old button.
Buttons click events code can be linked directly to toolstrip buttons.
All availables events can be showed in Action->Click in design time.
EDIT:
I assume that you have a form in your project with some buttons, eg.: button1, button2.
For each Button you wrote a click action, in your code something like:
private void button1_Click(object sender, EventArgs e)
{
// YOUR CODE FOR BUTTON1
}
private void button1_Click(object sender, EventArgs e)
{
// YOUR CODE FOR BUTTON2
}
Then you can use those methods/action for toolbar's buttons.
The easiest way to do is to add buttons to tollbar and use Events (with property panel, the little bolt icon) to choose the correct method to assign to each tolbar's button Click event/action.

how to autofill textboxes when switching from one tab to another in c#

I am developing an application (in c# using Visual studio 2012) where i have to read a config file which is in .txt format. I am also using tab control to switch between different tab.
what i want to do is that, when i click on config tab, my application should read the config file and place the data that i require into the text boxes that are present under that tab.
Any help would be appreciated.
i have tried placing the code to autofill my textboxes in the form constructor and it works, but i don't want to do it through constructor because it will only read the config file when the form is created and not when i switch from one tab to another.
You need to use the SelectedIndexChanged event of tab control.
tabContrl1.TabControl.SelectedIndexChanged += tabControl1_SelectedIndexChanged;
Add the event handler. Name your config tab as "ConfigTab" (Or whatever you want).
private void tabControl1_SelectedIndexChanged(Object sender, EventArgs e) {
if (tabControl1.SelectedTab.Name.Equals("ConfigTab")) {
//Fill textbox here
}
}
In your design view select the entire tabControl.
Then click the events button (lightning bolt) on the properties window.
Then double click SelectedIndexChanged
here is a sample code:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
// "First Tab Page" is the text of the tab page.
if ((sender as TabControl).SelectedTab.Text == "First Tab Page")
{
string filecontents = File.ReadAllText(#"path\to\configFile.txt");
textBox1.Text = filecontents;
}
}
Simple?

Devexpress Navigation Menu makes screen to freez

I've a devexpress Navigation menu item that opens a data entry form (user control). The user control has validation rules that compel users not to leave textboxs blank. And, it works pretty good so far.
But, the problem comes when I click on other menu items while the data entry user control is already displayed. This time, the screen just freezes and stucks, and I've to restart the system. What are the possible causes and solutions? Thanks in advance
Here are some code snapshots:
//Here is what I've on the main form. It has a panel control called mainPanel to display the user controls
private XtraUserControl uc;
private void MainForm_Load(object sender, EventArgs e)
{
displayUserControl("Data Entry");
//...
}
private void navigationBar_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
{
displayUserControl(e.Link.Caption);
}
private void displayUserControl(string link)
{
switch (link)
{
case "Data Entry":
uc = new ucDataEntry(); //the data entry user control that freezes the system
break;
case "Setting":
uc = new ucSetting();
break;
case "Chart":
uc = new UCReportChart();
break;
}
mainPanel.Controls.Clear();
mainPanel.Controls.Add(uc);
uc.Dock = DockStyle.Fill;
uc.Show();
}
Any time a menu item is clicked, you are clearing the previous control out of your main panel, and replacing it with a new one. Perhaps it is the validation logic in the ucDataEntry control that is causing the application to hang? (You haven't posted code for that control, so I can't be sure.)
As an aside, by calling mainPanel.Controls.Clear(), you are leaking memory. The documentation for this function states that you must explicitly call the Dispose() method for any controls that are cleared in this manner.

Categories