How to switch tabs with button in a TabControl? - c#

I've watched a few tutorials online on how to change tabs using buttons but for some reason all the code I've tried doesn't work. I am currently running Microsoft Visual Studio 2017 and am trying to write some code for a button to change tabs.
I couldn't find any differences between my code and code shown in tutorials, so it may just be a Visual Studio setting that I haven't set up correctly to allow the button correctly, but I couldn't figure out if or where it may be.
Here's my current code:
//Element event handlers
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
tabControl.SelectedTab = DestSelect;
}
private void buttonGotoIntro_Click(object sender, EventArgs e)
{
tabControl.SelectedTab = Intro;
}
//An old computer-generated segment code for the previous button.
//When I try to remove it the computer gets mad at me.
private void GotoIntro_Click(object sender, EventArgs e)
{
}

Please confirm you have subscribed to the Click event for the buttons.
public Form1()
{
InitializeComponent();
buttonStart.Click += buttonStart_Click;
buttonGotoIntro.Click += buttonGotoIntro_Click;
}

Instead of 'tabControl.SelectedTab = DestSelect;" try instead the method 'tabControl.SelectTab(DestSelect);'
I read through this article to find your (hopefully) answer:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/2cf22896-c5bd-4a9b-ab61-34404b55ef01/how-to-jump-to-a-specific-tab-in-the-tabcontrol?forum=vbgeneral

I assume u want to select a tab when a different Button is clicked.
tabControl.SelectedIndex = [Index of tab to switch to];
Code should look like;
tabControl.SelectedIndex = feeTabIndex;
If this is not clear enough, tell me exactly what you want to do.

Related

Click on one link in geckoWebBrowser in C#

I want to click on one link in one page.
Here is my code:
private void Button1_Click(object sender, EventArgs e)//GO
{
if (!Xpcom.IsInitialized) Xpcom.Initialize("Firefox");
geckoWebBrowser1.Navigate("http://www.tsetmc.com/loader.aspx?ParTree=151311&i=67126881188552864");
}
And I want to click on a link:
حقیقی-حقوقی
can anybody help me?
thanks.
I just looked up a Gecko project where I was clicking on the link and while the following might not be the most elegant way the same technique should work for you:
private void geckoWebBrowser1_DocumentCompleted(object sender, EventArgs e)
{
var elements = geckoWebBrowser1.Document.GetElementsByTagName("a");
foreach (GeckoHtmlElement element in elements)
{
if (element.ClassName == "violet")
{
element.ScrollIntoView(false);
element.Click();
}
}
}
I don't think the ScrollIntoView call is actually required, I just did that because it was an animated button and I wanted to see it was working. But you will need to wait until the document has loaded before clicking so I've put it in the DocumentCompleted event so before the Navigate don't forget to add:
geckoWebBrowser1.DocumentCompleted += geckoWebBrowser1_DocumentCompleted;

C# Windows form new page

I'm trying to make a button on my windows form create a new page when clicked, something like when you are going through an installation of a program and you would click "Next" and it would take you to a new page but not bring up an entirely seperate window. I would also have another button that would be pressed to bring up the original form.
I've looked everywhere for a answer to this so any help on how I would create this would be greatly appreciated.
private void Cleaning_Click(object sender, EventArgs e)
{
// executes new page
}
What you want to do is to create a wizard. There are lot of samples on internet regarding that. Take a look;
http://www.codeproject.com/Articles/31770/Wizard-Form-Implementation
Or
You can see that SO question
Creating Wizards for Windows Forms in C#
One simple way is to use panels in a single form. The only problem with panels is that it is hard to edit the layout and also your code would become very messy.
it IS simple BUT has a very bad downside
Simply we can add one groupbox and add your controls, in form load set groupbox visible to false and button click event visible to true something like....
private void Form3_Load(object sender, EventArgs e)
{
groupBox1.Visible = false;
}
private void button1_Click_1(object sender, EventArgs e)
{
if (button1.Text == "&Show")
{
button1.Text = "&Hide";
groupBox1.Visible = true;
}
else if (button1.Text == "&Hide")
{
button1.Text = "&Show";
groupBox1.Visible = false;
}
}

Text not showing in a label when clicking on a button

I am having trouble with a project I am working on that involves clicking on a button, and text is supposed to show up in a label box I created. I know this would be easier using a text box to show the text when I click on the button but my instructor wants us to use a label instead to show the text. I have debugged the project and everything says it is fine, but when I click on one of my buttons at the bottom, the text does not show up in the assigned label. Below is the code I am using. Maybe I am missing something. For example, when I click on customer relations button, it should show the department in one label, the contact name in the next, and the phone number in the next. I hope that is enough information
private void btnCustomerRelations_Click(object sender, EventArgs e)
{
lblDepartment.Text = "Customer Relations";
lblContact.Text = "Tricia Smith";
lblPhone.Text = "500-1111";
}
private void btnMarketing_Click(object sender, EventArgs e)
{
lblDepartment.Text = "Marketing";
lblContact.Text = "Michelle Tyler";
lblPhone.Text = "500-2222";
}
private void btnOrderProcessing_Click(object sender, EventArgs e)
{
lblDepartment.Text = "Order Processing";
lblContact.Text = "Kenna Ross";
lblPhone.Text = "500-3333";
}
private void btnShipping_Click(object sender, EventArgs e)
{
lblDepartment.Text = "Shipping";
lblContact.Text = "Eric Johnson";
lblPhone.Text = "500-4444";
}
Did the project compiled without any errors ?.
By default every Event Handler in C# is declared as void,which I am unable to find in your code.Did you modify the Event Handlers generated by Visual Studio,if this is the case then the issue you are facing is because of this.
Let me explain what would have gone wrong;
Whenever you create a Event Handler for any control using the Properties Window of Visual Studio,for the sake of this explanation let me take example of TextBox.Suppose you have a TextBox (named as textBox1,which is default) and you subscribe to its TextChanged Event (to do that locate TextChanged event in the events window of Visual Studio and double click it,when you do that Visual Studio generates this for you;
private void textBox1_TextChanged(object sender,EventArgs e)
{
}
This is what we programmers call an Event Handler,now locate Solution Explorer Window in Visual Studio click on Form1.Designer.cs you will get a lot of code there,locate the line which says
private System.Windows.Forms.TextBox textBox1;
where textBox1 is the control's name.Locate a plus sign above this point,click it and locate the following code;
//
// textBox1
//
this.textBox1.AcceptsReturn = true;
this.textBox1.Location = new System.Drawing.Point(478, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(359, 23);
this.textBox1.TabIndex = 1;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
PS:The Location , AcceptsReturn , Size and TabIndex property in yours might not be same as mine.
Read the last line of this code,it says;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
where textBox1_TextChanged is the name of event which must be same as that defined in Form1.cs.If you modify this you'll get various compile time errors.
So now I guess you know what is the relationship between Form1.cs(main code file) and Form1.Designer.cs(code behind file).
In one line the conclusion is that make sure ;
Any event handler function in Form1.cs starts with private void ....,and the words after private void are exactly same as defined in the code behind file for that particular control.If you would like to read more about this stuff,have a look here.
Hope it would have helped you to solve the issue.Anything else left please inform me.
Have you tried inserting an Application.DoEvents() statement at the end of each method? Sometimes forms are finicky about updating labels and that method will force the form to redraw itself.

C# Tabless Control Previous/Back/Return Button failing?

I am hoping someone here can help me, i have a Tabless Control on my windows forms application and basically because the tabs are purposely hidden i have added 2 buttons to each tab "Next" and "Back".
This is the code snippet i have for my "Next" button:
private void nextbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage3;
this.toolStripStatusLabel8.Text = System.DateTime.Now.ToString();
}
Which works fine, however when i use the exact same theory on the "Back" button it does not work:
private void backbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabmain;
this.toolStripStatusLabel1.Text = System.DateTime.Now.ToString();
}
So my question is how does one go to a previous tabpage from a button? I have looked through here and tried all of the links that came up but nothing has worked any ideas?
You should use the SelectedIndex property instead of using concrete TabPage instances. This way it will still work when you decide to change the order of the pab pages or add new pages:
private void previousButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex > 0)
{
tabControl1.SelectedIndex--;
}
}
private void nextButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex < tabControl1.TabCount - 1)
{
tabControl1.SelectedIndex++;
}
}
Since there is no "Tabless" tab control in .NET Framework I can only assume that it works similar to the standard TabControl. If the solution doesn't work you should give us some information about the actual class you use.
BTW: There is no need to repeat the buttons on each page. Why don't you just put the buttons outside the TabControl?
Also: I see that you use a ToolStripStatusLabel to show the current time. Instead of updating it each time the user clicks somewhere add a Timer to your form. Set its Interval to 1000 and handle its Tick event. Update the label there:
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString();
}
This way it updates constantly and again there is no need to repeat anything. You need to call timer1.Start() in the form's constructor.

Designing a dynamic panel

I am new to C# and I want to design a GUI for a image processing application in c#. I have a very basic rudimentary layout designed as shown below
Here, the image plane is fixed and it will show a live stream video. I have designed all the buttons frame and the side panel. But I do not know how to dynamically change the side panel for each button I click. For example, If I click button1_1, I want some things in the side panel and for button1_2, some other things in it. How do I go about doing it.
EDIT:
Thanks for the answers. I see tab controls is an option. But I want a new panel evertime a click a button. which can further open forms. Is it possible?
OK, let's see. It's easy to do with "TabControl" or array of "Panel"s.
1.Do it with TabControl.
You can design GUI in TabControl in multiple subTabs(if you don't know how please ask.). Then you change it in button click event, to make subTab you wanna show(which means make it visiable and not visiable for other subTabs.)
2.Do it with array of panel.
You can use panel[] panels. In button click event, you hide other panels and show the one you want.
Hope answer helps you!
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 0;
tabControl1.TabPages[0].Text = "First";
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 1;
}
private void button3_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 2;
}
you may want to add split container in your form.
Create UserControl for each buttons.
Code for the button click event
//Button1Click Event
private void button1_Click(object sender, EventArgs e)
{
UserControl1 m_UserControl = new UserControl1();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(m_UserControl);
}
//Button2Click Event
private void button2_Click(object sender, EventArgs e)
{
UserControl2 m_Usercontrol2 = new UserControl2();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(m_Usercontrol2);
}
you can do this if you want to change what usercontrol display in a panel at run time.
Correct me if i misunderstood your question.
In WinForms, you could use a tab control and just change the selected tabs index when a button is pressed. More specifically, when its click event is fired. Here is a good tutorial on using the TabControl and here is a tutorial on wiring up click events.
EDIT:
This is a better tutorial.
Since you can't hide the tabs of a tabcontrol without using WPF, you may need to use something else, if you don't like the way they look. A good workaround if you only have a couple of buttons and thus views, would be to use panels. When button one is clicked show panel one and hide panel two, etc. Here would be the code:
private void button1_Click(object sender, EventArgs e)
{
pane2.visible = false;
pane1.visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
pane1.visible = false;
pane2.visible = true;
}
Hope this helps you!

Categories

Resources