How to add tabpages dynamically to the beginning of the tabcontrol - c#

I have a tab control that I can programmatically add and remove tabs from. It works fine but whenever I add a tab this tab is added end of the tab list. How can I add tabs beginning of the tabList dynamically.
This is the code I use to add tabs:
private void button1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Add("Kare");
}
Thanks from now.

I finally solved it:
you should change your code to that:
tabControl1.TabPages.Insert(0,"Başlık");
let me explain what it means insert is like add but instead of adding tabs to the end of the list it allows the user to choose where the tab will be added. In my case it will be the beginning of the tab list.
0 means beginning of the list or the first tab and "Başlık" stands for the name of my tab.

Related

How to show an ErrorProvider error icon next to a TabPage header?

Edit:
This is not a duplicate of Icons in TabControl C# - How?. The question there is about adding icons to tab pages. Here it is about how the change the error provider error icon position to be inside the header instead of to the right of the tab page itself. Also, the error provider error icon has the functionality that when you hover the mouse on it, you see the error text, which you do not see if you simply add an icon to the header.
I have a form with a TabControl. The form has also an ErrorProvider. When I try to use the following code:
errorProvider1.SetError(tabPage1, "error");
The error icon is shown to the right of the tab page, and it is cut-off by the tab control itself:
I would like the icon to be shown next to the tab page header. Something like this (made with Photoshop):
I do not know where to start, or how to approach this.
Edit:
I have a class responsible for adding errors to a control, and showing them using an error provider. This class is used for TextBoxes, NumericUpDowns etc. I would like to use it also for TabPages. The problem is that when I use it for tab pages I get the result shown above. The trick of adding an error icon to the header using an ImageList and then add a tooltip is not good, because it is specific to tab pages, and I cannot implement it in my class which is general to all controls. So I really need to change the settings of the tab page so when I use errorProvider.SetError(...) it is shown in the header.
ErrorProvider shows error icon of the TabPage in tab page's client area.
By playing with IconAlignment or IconPadding, you can show error icon of TabControl on one of the tab pages' headers, but it's error icon for the whole TabControl.
In a real application each of the tab pages can contain controls which are not valid and you may want to show the validation icon on tab pages not for the tab control.
My suggestion is using tab page icon by setting ImageList containing the error icon as image list of the TabControl and by setting ImageIndex of the TabPage, show or hide the image icon. This way you can show the error icon for every tab page which needs it:
Example
To setup the example, follow these steps:
Create a Form.
Drop a TabControl, an ErrorProvider and an ImageList on the Form.
Set ImageList property of tabControl1 to imageList1.
Drop two TextBox on tabPage1.
I assume, just for example, you are going to validate these two text box controls using Validating event. The key point is here. When you validate any control, check if it's hosted in a TabPage, check validity of all children of TabPage and set the error icon based on that:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.AutoValidate = AutoValidate.EnableAllowFocusChange;
imageList1.ColorDepth = ColorDepth.Depth32Bit;
imageList1.Images.Add(errorProvider1.Icon);
tabControl1.ImageList = imageList1;
textBox1.Validating += textBox_Validating;
textBox2.Validating += textBox_Validating;
}
private void textBox_Validating(object sender, CancelEventArgs e)
{
var textBox = (TextBox)sender;
if (string.IsNullOrEmpty(textBox.Text))
{
this.errorProvider1.SetError(textBox, "Value is required.");
e.Cancel = true;
}
else
this.errorProvider1.SetError(textBox, null);
var tabPage = textBox.Parent as TabPage;
if (tabPage != null)
ValidateTabPage(tabPage);
}
void ValidateTabPage(TabPage tabPage)
{
var tabIsValid = tabPage.Controls.Cast<Control>()
.All(x => string.IsNullOrEmpty(errorProvider1.GetError(x)));
if (tabIsValid)
tabPage.ImageIndex = -1;
else
tabPage.ImageIndex = 0;
}
You can do the following.
Rectangle rc = tabControl1.GetTabRect(0); // Replace with the index of Tab required
errorProvider1.SetIconPadding(tabControl1, -rc.Left-20);;
errorProvider1.SetError(tabControl1, "Error String");
You also need to set
errorProvider1.SetIconAlignment(tabControl1, ErrorIconAlignment.TopLeft);
Sample (With Second Tab Selected - based on comments),
You would need to prepend whitespace to your TabPage Text to ensure there is sufficient space for showing the icon
With Icon on Second Tab

How to Show Other Forms Using DevExpress tileItem in tileControl?

I am new in C# and DevExpress. I'm trying to show another form by clicking a tile in the tileControl group but it doesn't show up. I just right-clicked at the tileControl, clicked view code and manually declared this since this doesn't automatically shows up if you double click a tile.
private void addTile_Click(object sender, EventArgs e)
{
var xForm2 = new XtraForm2();
xForm2.Show();
}
I just right-clicked at the tileControl, clicked view code and manually declared this since this doesn't automatically shows up if you double click a tile.
If I understand this correctly, you did not build your project using the C# code listed above, but only edited the source.
That does not work because what you edit in this way is not being loaded into the app.
Of course, I could be misunderstanding what you wrote.

creating a dynamic "recently opened files" menu

I want to add a feature to my MenuStrip where I want there to be an option where you can hover over or press the menu option to open recently opened projects .
File---> Recently Opened Projects---> {List of projects.....}
The same kind of option/menu that exists in Microsoft office products (e.g. word 2007).
I know how to get an array of the file names. I just need to know how to put the array of the names at the Sub MenuStrip.
You can add them dynamically in code:
private void menuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "your file name";
item.Click += new EventHandler(yourEventHandler);
menuItem.DropDownItems.Add(item);
}
You need to create ToolStripMenuItems in a loop and call DropDownItems.Add to add them to your parent menu item.
In the loop, you should add a handler to their Click event.

ContextMenuStrip renders at top left Windows

When i render contextmenustrip, it gets render at the top left of my PC Screen. I have a listview, which contains 5-6 items and on right click of each item, the context Menu strip gets displayed.Also i need to change the color of context menu strip including backgrounds and text as well.
Thanks in advance!
By far the simplest way is to just set the ListView.ContextMenuStrip property to your CMS, everything is automatic then. You can do so in the designer.
If you need a custom handler for some reason, to check if the right item was clicked for example, then you can call the Show() method property with code like this:
private void listView1_MouseClick(object sender, MouseEventArgs e) {
if (allowContextMenu(listView1.SelectedItems) {
contextMenuStrip1.Show(listView1, e.Location);
}
}
You haven't shown any code, but if you're not calling the Show overload that takes a control as a parameter, the new Point(0, 0) that your obviously passing will put the menu in the upper left of the screen.

Programmatically change the tab order

How do I programmatically reorder the tabs in a TabControl? I need to sort the tabs depending on some conditions.
If it's possible to do the reordering through the designer, i guess we must be able to do it through code at runtime too.
Create a new Form.
Create a new TabControl.
Notice it has two TabPage controls, and TabPage1 is the first tab.
In form's Load event, add
this.TabControl1.TabPages.Remove(this.TabPage2)
this.TabControl1.TabPages.Insert(0, this.TabPage2)
Run the form.
Notice TabPage2 is now the first tab.
Note that if you fail to remove the tab page, it will still show at its old location. In other words, you will have two tabs of the same tab page.
You have to redefine your tab page collection, in order to change the index of your tab pages.
The following lines of code can also do the trick, this kind of solution also works for other kind of data that has no direct way of sorting:
- convert to a list
- sort the list
- put it back
public static void Sort(TabControl tabControl)
{
var tabList = tabControl.TabPages.Cast<TabPage>().ToList();
tabList.Sort(new TabPageComparer());
tabControl.TabPages.Clear();
tabControl.TabPages.AddRange(tabList.ToArray());
}
public class TabPageComparer : IComparer<TabPage>
{
public int Compare(TabPage x, TabPage y)
{
return string.Compare(x.Text, y.Text);
}
}
thelost is right. Below is a quick sample code.
I have a tab control with 2 tabs (tabpage1, tabpag2)
Then I declare two tabpages and store the existing tabs in the tabcontrol in it.
abPage tbp1 = new TabPage();
TabPage tbp2 = new TabPage();
tbp1 = tabControl1.TabPages[0];
tbp2 = tabControl1.TabPages[1];
Then on a button click I removed the tabs using
tabControl1.TabPages.Remove(tabControl1.TabPages[0]);
Now if you want to change the order then you will have top add it to the tab in that order
//Order changed
tabControl1.TabPages.Add(tbp2);
tabControl1.TabPages.Add(tbp1);
Note: This is untested quick code.
Go inside Designer.cs file
There you will find
/// [Your TabControl Name]
yourTabControl.Controls.Add(yourPage1);
yourTabControl.Controls.Add(yourPage2);
yourTabControl.Controls.Add(yourPage3);
The adding order is your tabpages' order in the tabcontrol. Change the order how you wish. Remove and Add functions of TabControl.Controls will help you as Shoban answered.
try this after Initilizacomponent(). this code will give you freedom to change it programmatically in .cs file.
this.tabReceive.Controls.Remove(this.metroTabPage4);
this.tabReceive.Controls.Remove(this.metroTabPage5);
this.tabReceive.Controls.Add(this.metroTabPage4);
this.tabReceive.Controls.Add(this.metroTabPage5);
Sometimes I have tabControls with several tabPages. At runtime tabPages are made invisible (by removing them) an added later again.
After this the tabPages may be in wrong order. I use this code to redorder them again:
public void ReorderTabPages()
{
// Demo code to reorder tabControl with tabPages
// where some tabPages may be unwanted at the moment
// caution: events like "SelectedIndexChanged" does fire!
// List of wanted tab pages
List<TabPage> wantedTabPages = new List<TabPage>();
// remember the current selected tab
TabPage currentTabPage = this.tabControl.SelectedTab;
// check if all possibly active tabs are currently visible
// check it in the order they should be displayed
// after that they are in the correct order in "wantedTabPages"
if (this.tabControl.TabPages.IndexOf(this.tabPage_01) >= 0)
wantedTabPages.Add(this.tabPage_01);
if (this.tabControl.TabPages.IndexOf(this.tabPage_02) >= 0)
wantedTabPages.Add(this.tabPage_02);
if (this.tabControl.TabPages.IndexOf(this.tabPage_03) >= 0)
wantedTabPages.Add(this.tabPage_03);
if (this.tabControl.TabPages.IndexOf(this.tabPage_04) >= 0)
wantedTabPages.Add(this.tabPage_04);
this.tabControl.SuspendLayout();
// remove all currently visible tab pages
for (int i = this.tabControl.TabPages.Count - 1; i >= 0; i--)
this.tabControl.TabPages.RemoveAt(i);
// add the tabPages in the correct order
foreach (var wantedPage in wantedTabPages)
this.tabControl.TabPages.Add(wantedPage);
// restore the currently selected tabPage
this.tabControl.SelectedTab = currentTabPage;
this.tabControl.ResumeLayout();
}

Categories