tabControlPages remove picture - c#

Try to find solution for next - on mouseEnter - show picture on TabControl page and on MouseLeave - clear it.
Currently done - just show pic on tab.
There are a lot of question refered to "How to show" picture - this and this or this ant also a lot of others.
For this I use next way - add imageList and just show pic on mouseEnter -
private void tabControlPages_MouseEnter(object sender, EventArgs e)
{
tabPageAdd.ImageIndex = 0;
}
For removing - try to read about some methods on MSDN - but have found nothing (maybe not found).
As variant think, can put in collection transparent icon and just change it on mouseLeave, but think it's not a perfect solution. If use imageListIcons.Images.Clear(); - it's fully clear ImageList - as and expected.
So, are ther is some solution avaliable for correct way to hide/show icon on tab from tabControl ?

Just set ImageIndex = -1 to remove the tab icon:
private void tabControlPages_MouseLeave(object sender, EventArgs e)
{
tabPageAdd.ImageIndex = -1;
}

Related

Tab system using panels

I'm trying to make it to where a panel becomes visible and it sent to the front so it can be seen and interacted with, like this.
private void SettingsButton_Click(object sender, EventArgs e)
{
SettingsPanel.Visible = true;
SettingsPanel.BringToFront();
}
The problem is that after clicking a few of the buttons, it will either display the wrong panel or none at all. Is there a way to fix this?
EDIT: Before y'all ask, i'm using WinForms.
OK, so I was wrong, WinForms is smarter than I thought. Here's a test you can do. I'm not exactly sure what you are trying to do, but this should help you along. To start, we're going to build a small WinForms app. With one exception, we aren't going to set any properties of the controls we drop on the screen:
Create a new WinForms app
In the designer, drop a Panel (which will be named panel1) on the form
In the properties pane, set the BorderStyle to FixedSingle (that's the only property we are going to set)
Make two copies of that panel (panel2 and panel3). Position them so that panels do not overlap at all.
On each panel drop a couple of controls (I put labels (labels 1-3) and textboxes (also 1-3)) on each one
Beside each panel (arranged so that there is no overlap) drop three buttons on the form (buttons 1-3) make it so that visually, each button is associated with the similarly numbered panel
Duplicate panel3 including its contained controls (so that you get panel4, label4 and textbox4). Position the duplicate so that it significantly overlaps panel 3
Now we're going to look at the code that the designer creates for your form. Don't mess with this code (you can, but if you don't know what you are doing, it can turn out bad - and, we're keeping this simple).
In the Solution Explorer click the unfilled triangle to the left of Form1.cs. Note that it rotates and turns solid. Also note that Form1.Designer.cs is displayed. That's a normally hidden source file that contains all the designer created code that corresponds with the form and the controls you dropped on it.
Open Form1.Designer.cs
Click the little grey plus sign icon next to Windows Form Designer generated code
Inspect the file. Note that every action you did in the designer has a corresponding line of code in the Designer.cs file (more or less)
Look at the code for one of the panels (say panel1).
See that it includes:
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.label1);
Scroll all the way down to the Form1 code and see that the panels and buttons get added to the Form's Controls collection:
Like:
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel4);
this.Controls.Add(this.panel3);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
Note that the order is reversed. The order is important, it sets the Z-Order (i.e., what overlaps what) for the form and the controls on the form.
Wiring up the buttons
Select all three buttons and press <Enter>. This will open the Form1.cs file and generate three button click handlers that you can fill in.
Use this code for the three button handlers:
private void button1_Click(object sender, EventArgs e) {
var wasVisible = panel1.Visible;
panel1.Visible = !wasVisible;
}
private void button2_Click(object sender, EventArgs e) {
panel2.BringToFront();
}
private void button3_Click(object sender, EventArgs e) {
panel3.BringToFront();
}
The first one will toggle the first panel's visibility (I put in an extra variable so you can set a breakpoint and see what's going on). The second one brings panel2 to the front, changing its Z-Order (it's called Z-Order because the position on the screen is measure in X and Y, which the overlap position is related to the "depth" of the screen, or the Z-coordinate). The last one does the same thing to panel3.
Run the program.
When you press the first button, the first panel and its controls disappear (this surprised me, WinForms is smarter than I thought)
When you press the second button, nothing appears to happen. This is because the only thing that panel2 intersects is the form, and panel2 already covers the form, so you don't see any effect. (and because WinForms is smarter than I thought)
When you press the third button, panel2 (and it's controls) jump to the front of the stack of controls, covering the intersecting part of panel4.
Does this help you understand how Visible and BringToFront() work?
What you're describing is similar to a TabControl alternative. Here's an example:
You can manage the current panel simply by making it visible and docked to fill. Hide the other panels.
public partial class FormTabsAlternative
: Form
{
int m_current = 0;
List<Panel> m_tabs = new List<Panel>();
public FormTabsAlternative()
{
InitializeComponent();
AddTab(pnl1);
AddTab(pnl2);
AddTab(pnl3);
AddTab(pnl4);
SetUpTabsAndButtons();
}
private void AddTab(Panel pnl)
{
m_tabs.Add(pnl);
pnl.Dock = DockStyle.Fill;
}
private void OnLeftClick(object sender, EventArgs e)
{
if (m_current > 0)
{
m_current--;
SetUpTabsAndButtons();
}
}
private void OnRightClick(object sender, EventArgs e)
{
if (m_current < m_tabs.Count - 1)
{
m_current++;
SetUpTabsAndButtons();
}
}
private void SetUpTabsAndButtons()
{
for (int index = 0; index < m_tabs.Count; index++)
{
var panel = m_tabs[index];
panel.Visible = index == m_current;
}
btnLeft .Enabled = m_current > 0;
btnRight.Enabled = m_current < m_tabs.Count - 1;
}
}

c# - Manually scroll Control with AutoScroll=true

I have a Panel with AutoScroll=true. I'd like to manually scroll the panel's VerticalScroll. I've tried both setting VerticalScroll.Value and Panel.ScrollControlIntoView(...).
However, both cases yielded the following result: The scrollbar does appear to have scrolled to the value but the panel's contents remain unmoved. Scrolling upwards shows an empty panel.
I'm trying to do this during startup. If I scroll directly to the control after a delay (from a thread), it works (though setting the scrollbar value doesn't).
Is there a better (synchronous) way of achieving what I'm looking for?
Use Shown event, for example:
private void Form1_Shown(object sender, EventArgs e)
{
this.panel1.ScrollControlIntoView(this.button1);
//Or if you need a special location:
//this.panel1.AutoScrollPosition = new Point(100, 100);
}

How to change the background of a button programatically?

I need to change the BackgroundImage of a button on click of another button (In Windows Forms in C#). But I can't find out how to do it!!
I searched on the internet and found many examples and all of them use ImageBrush, ImageSource etc.... but these don't work on my application, it shows me errors every time I Use them.
I read on the internet that I have to add this namespace:
using Windows.UI.Xaml.Media.Imaging;
But it shows me an error on the begging which says to add this System before Windwons and when I add it:
using System.Windows.UI.Xaml.Media.Imaging;
it shows me than the error at UI .... I can't figure it out how to solve this!!
Please help me guys!
To Change Background Image of a button there are two ways i know.
Add the Image to the resources folder of your project and use.
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Properties.Resources.ImageName;
}
Use Image.FromFile();
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "//Card1.png");
}
You are trying to use solutions for WPF in Winforms. This will not work.
The class you need is System.Drawing.Image (or System.Drawing.Bitmap, which inherits from Image).
Bitmap b = new Bitmap(#"C:\myBitmap.jpg");
myButton.Image = b;
Be sure to call Dispose on your Bitmap if and when it is no longer in use.

c# .NET - Tabpage margin

I have a tabcontrol with 3 tabpages. I need to add a left margin to the first tabpage ( so move all tabpages move right of 200px ). How can I do it??
Using Visual Studio 2008 / c#
EDIT Reading again I think you're more looking for the controls on each page to be on the right of the tabs rather than moving the buttons.
As Hans suggests a panel would be the easiest way. But it's not pretty.
private void Form1_Load(object sender, EventArgs e)
{
// Create spacer tab with a name long enough to reach the 200px mark
TabPage spacer = new TabPage("..............................................................");
tabControl1.TabPages.Insert(0, spacer);
// Create a panel at the same location of the tab control.
Panel spacerBlock = new Panel();
spacerBlock.Name = "spacer";
spacerBlock.Location = tabControl1.Location;
spacerBlock.Width = 198;
spacerBlock.Height = 20;
this.Controls.Add(spacerBlock);
spacerBlock.BringToFront();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
// Ensure the user can't use the keyboard to somehow select the spacer tab.
if (tabControl1.SelectedIndex == 0)
tabControl1.SelectedIndex = 1;
// Check if the second (first I guess) tab is selected and adjust the panel to keep the look consistant.
if (tabControl1.SelectedIndex == 1)
this.Controls["spacer"].Width = 198;
else
this.Controls["spacer"].Width = 200;
}
You'll want to make sure the tab isn't selectable by the user via keyboard shortcuts thus the index change check.
Also note the panel will have to have its width adjusted if the second (first in your case) tab is selected due to the 3d GUI effect.
Honestly the hassle of taking into account the appearance settings of the end user to ensure the spacer tab's text and the panel width are correct length doesn't really make up for fancy look IMHO.
Only other option I could think of would be a tab panel with a 16px height. Again this would have to be adjusted depending on the end users appearance settings, not to mention the excess overhead in getting it all working.
If it's the AjaxControlToolkit tab control, add this CSS class:
.TabContainer .ajax__tab_header
{
padding-left: 200px;
}
you would need a work-around for that because tab pages can't be moved. You might wanna place a groupbox inside the tabpage and then you can add all the controls inside the groupbox as you desire...
// tabPage 1
this.tabPage1.Controls.Add(this.groupBox1);
// groupBox1
this.groupBox1.Location = new System.Drawing.Point(200,6);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.AnyControls); //etc

How can I set an icon for a ListViewSubItem?

In a ListView you can have icons on each item.
When viewing in Details-mode, the icon is shown in the left-most column.
Can I show an icon in some other column?
The ListView control does not support images in sub-items natively. The easiest thing to do is switch to a DataGridView and use a DataGridViewImageColumn. If that is not possible, then you'll need to draw the icons yourself using the custom draw support in the ListView control. To do this set ListView.OwnerDraw = true and handle the ListView.DrawSubItem and ListView.DrawColumnHeader events.
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
// Only interested in 2nd column.
if (e.Header != this.columnHeader2)
{
e.DrawDefault = true;
return;
}
e.DrawBackground();
var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), imageRect);
}
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
Use P/Invoke and send LVM_SETITEM message to the listview (you should set LVS_EX_SUBITEMIMAGES style on control creation or via LVM_SETEXTENDEDLISTVIEWSTYLE), specify the subitem index and the corresponding image index. You will need to do it for every list item you insert.
ObjectListView is an open source wrapper around a .NET Winforms ListView. It supports images on subitems using the p/invoke strategy that that #ligget78 mentioned. It also solves many other common problems with a ListView.
It allows you to make very nice looking ListViews with a minimum effort:
(source: sourceforge.net)
Inherit from ListView and draw your own icons.
public class MyListView : ListView
{
protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
{
base.OnDrawSubItem(e);
}
}
The icon is shown in the "first" column, and this is also the basis for the keyboard prefix search. One possible solution could be to reorder the columns by setting the DisplayIndex of the first column to something else.
listView1.Columns[0].DisplayIndex = 1;
This of course only works if you need an icon in only one column.
There's no .NET support for this.
Have a look at this project.
Take a loot at this:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d25b4ffa-2ea4-43cd-a3ae-8dd0387197ae/
In addition to the accepted answer, you should handle the DrawItem event as well, or it will not work.

Categories