Does anybody know how to move to a different tab when you click a button? Is there a general statement?
private void button3_Click(object sender, EventArgs e)
{
}
I know that something needs to be entered into the method above but I really don't know where to start.
tabControl1.SelectedTab = DesiredTabPage;
DesiredTabPage is the name of the tabPage you want to move to it
Related
If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.
I insert tab-control in my windows application.It has 4 tab-pages.I wanted to show only relevant tab-page when I click the relevant button..my code as follows
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage1.Hide();
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
first every tab-page removed when form load
Here is the code for button click and I coded for 4 buttons...
private void button2_Click(object sender, EventArgs e){
tabControl1.TabPages.Insert(0, tabPage1);
this.tabPage1.Show();
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
I again used ....
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
these code. if another tabpage is opened when I click the button it should be remove and show relevant tabpage.Its working.
My problem is.. if I click the same button again and again same tabpages adding continuously
Can anyone give me a solution for it..
I founded a one way...In button click I modify like this.
private void button1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
if (tabControl1.TabPages.Count <= 1)
{
tabControl1.TabPages.Insert(0, tabPage1);
this.tabPage1.Show();
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
}
first code is tabControl1.TabPages.Remove(tabPage1).Then if tab-page is open It removed.If statement responsible for when button click relevant tab page is opened.then always show only one tab-page for button click moment
I'm working on Visual Studio, in C# language
I would like to ask if there's someway to go back to get the ButtonX.Text I pressed on the previous page, some sort of Login without a password.
Basically I need a worker to specify which person he is by clicking on their name (button) then it goes foward to the next page I have a label on the MasterPage but it resets everytime it goes on a next page what I would like to do is keep the info there
If you need some code tell me.
Thanks
You could use session variables?
On the button click handler on the first page...
protected void button_Click(object sender, EventArgs e)
{
Session["Worker"] = button.Text;
}
Then on the second page...
Label.Text = Session["Worker"];
Based-off your reply to Zollistic's answer, you could do this...
Apply this event to all your all your worker buttons...
protected void button_Click(object sender, EventArgs e)
{
if (Session["Worker"] == null) Session["Worker"] = "";
Session["Worker"] += button.Text + ",";
}
Now Session["Worker"] has a character-delimited list of all the clicked buttons. The character in this example is a comma; but you can change it to whatever you want (i.e. a pipe "|").
this is my code:
public RegForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label10_Click(object sender, EventArgs e)
{
}
private void label28_Click(object sender, EventArgs e)
{
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void label29_Click(object sender, EventArgs e)
{
}
private void label30_Click(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
I would like to remove all the unnecessary lines of code for the labels that just came up out of nowhere for no reason and do absolutely nothing. But when I delete them - it's all errors. I'm a beginner, so please go easy on me. Thank you in advance.
To remove those empty event handlers:
Switch to the designer view,
Select each label, one at a time,
Look at the properties window (press F4 if it's not visible), and check out the events view
Find the click event, right-click and choose "reset".
Be careful when you're selecting your different controls in the designer.
If you double-click by accident, you end up creating an empty event handler for whatever the default event is for a particular control. In the case of a Label, it happens to be the Click event.
This are auto generated events which are created when you double click
on control or some other action is performed on the controls in the
design view.
Remove the lines from code behind and also remove them from *.designer.cs file.
IF you build the application after the removing of the lines and check errors: You can click on every error and it will leads you to the place which should be deleted !
If those are unnecessary then just go ahead and delete them from your code behind *.cs file. You will also have to delete the event registration from the corresponding *.designer.cs file and because of the same thing you are getting error (that you are not removing the corresponding event registration from designer file).
You can as well do the same from Designer window by select the control -> press F4 -> go to the event pane by clicking the lighting bolt icon -> remove the event registration.
I know its a silly question but, still I wanted to know it. I have two textboxes, textbox1 and textbox2. I entered some text in textbox1. Now I want that the value of textbox one should be displayed on textbox2 when I move from textbox1 to textbox2 using tab index or by clicking my mouse on textbox2. I know I can make us of the mouse over event. But it will be great if I get some good opinion from you. Thanks in advance
private void textBox1_Leave(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
private void textBox2_Enter(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}