C# TabPage ImageKey not drawing - c#

I have an Application that runs tests on a customers account in order to judge if their service is working correctly. During the process of running the tests, the application reads each test and checks to see if it passes / fails / etc... It plays a green checkmark / red x on the tabPage itself as an imagekey... the imagekey is assigned as so
(tabPage as TabPage).ImageKey = "pass.png";
tabPage is actually an object that is passed through to the function so i can refer to it from a different method.
When the tabControl for the tabPage is created (dynamically), an imageList is added to the tabControl (which is where the images are pulled from).
(tabControl[0] as TabControl).ImageList = imageList2;
So when the method finally gets around to the code to assign the ImageKey it does run through the code, however it just shows up as a blank image. It's weird, because it works for some people and not others. It does not currently work on mine atm either and they do not show when I execute the source code. Does anyone have any ideas? Here's an image to help describe the issue... More code to follow if needed.

Your need for an answer has probably long-since passed, but I had the same problem, and here is my solution:
Setting the ImageKey does not work, but setting the ImageIndex does cause the icon to display. I made an enumerator that mapped the icons to their indices.
private enum TabIconCodes : int
{
Stopped = 1,
Running = 2,
}
unitTab.ImageIndex = (int)TabIconCodes.Stopped;
This is not an ideal solution, but it does work.

Hy! Another solution on the same concept:
tabp.ImageIndex = ImageListTabpages.Images.IndexOfKey("ImageKey")
It does work for me.

I understand this question was asked a long time ago but the answer provided here either is a workaround or not clear enough.
This does NOT show the image:
var tabPage = new TabPage("page text");
tabPage.Name = "pageName";
tabPage.ImageKey = "myImageKey";
TabPages.Add(tabPage); //tab is added AFTER setting the ImageKey
This works fine:
var tabPage = new TabPage("page text");
TabPages.Add(tabPage); //tab is added BEFORE setting the ImageKey
tabPage.Name = "pageName";
tabPage.ImageKey = "myImageKey";
Hope this helps!

Before setting up the ImageKey for PageTab the TabControl of the PageTab needs to have ImageList set and PageTab has to be added to the TabControl

Related

How to change label color of a usercontrol from another usercontrol ? C# .NET Framework

i have this project with 3 usercontrols and 1 form, the usercontrols appear on the form when a button is pressed. Now i need to change a label color on let's say "UserControl3" by clicking a button located in "UserControl1", i tried the following code but it didnt work
var name = new UserControl3();
name.label.ForeColor = Color.Green;
i also tried another way but it threw an exception, image below
https://imgur.com/kBHUBoX
i then found another way that almost works as intended, by removing and adding the usercontrol. The problem is that then it goes on the main form that is supposed to be empty
var name = new UserControl3();
this.Controls.Remove(name);
this.Controls.Add(name);
name.label1.ForeColor = Color.Green;
the code above almost works as i want but i dont think its a good solution and im pretty sure there are other easier ways to make this work... Any kind of help is appreciated. Thanks
You do not have to re-create an instance of usercontrol but use the first instance created and just change the color of the control.
just this code:
name.label.ForeColor = Color.Green;
Define this code globally
var name = new UserControl3();

c# & winforms - unable to set Image of ToolStripButton

This seems like a fairly basic thing to do, but for some reason it just fails silently:
/// <summary>
/// Sets the button to show it's busy image
/// </summary>
public void SetBusy()
{
if (Control is Button)
{ ((Button)Control).Image = BusyImage; }
else if (Control is ToolStripButton)
{ ((ToolStripButton)Control).Image = BusyImage; }
}
BusyImage is set using BusyImage = Properties.Resources.Busy;
If I debug this, I can see that the image appears to be setting correctly (if I hover over the Image member when at a breakpoint I can see it change), but it doesn't actually change the image when you look at the button.
I have noticed that this works when all the above code is hosted in the same Project file as the UI, but when it's shipped out to a different project (but within the same Solution), it fails silently.
Any ideas where I'm going wrong?
Thanks
EDIT 1:
Even trying to set the Image to a file from the Resources of the same project as the ToolStripButton doesn't work (still fails silently).
Interestingly, it works absolutely fine when using a normal Button, regardless of which project the images are in.
Why the difference in behaviour between Button and ToolStripButton?
EDIT 2:
It appears that moving the code that sets the image into the same project as the ToolStripButton works. However, I would like to keep it in a separate project if at all possible...
try this instead, tested it and working fine at my end:
public void ChangeImg(Component ctrl)
{
if (ctrl is Button)
{ ((Button)ctrl).Image = Properties.Resources.keylock; }
else if (ctrl is ToolStripButton)
{ ((ToolStripButton)ctrl).Image = Properties.Resources.keylock; }
}
Finally figured this out. To cut a long story short, I had some ToolStripButtons hidden somewhere in my form, only visible in the combobox in the designer's properties window (even when you select it from there, you can't see it on the form anywhere). I was passing the name of one of these to the ImageButton instead of the correct one (which had a default name like toolStripButton3)...
I'd love to know how it happened, I suspect user error on my part...but then again I find it strange that VS will allow a ToolStripButton to exist when it doesn't appear on any ToolStrip on the form.
Either way, my code seems to work quite happily now. The reason it appeared to work when run from the same project was that I was using a different button to test the theory.
Lots of process of elimination got it down to just two buttons that weren't playing ball; on a hunch I decided to compare the properties of the working and non-working buttons, whereupon I discovered the issue...

How to change the background image of a button when clicked in code?

There is a similar question like mine here in Stackoverflow but it only explains how to change it in XAML. I want to know how can I change it in code.
Here is a image that shows how I do it in XAML using Blend:
Link for full size: https://snag.gy/4Skk4.jpg
Basically I want to change the background of a button's pressed state in C# but I can't seem to find any examples on the Internet. It must be in code because sometimes the image of the button will change therefore the button's pressed image must change as well.
The following code is just to change the image of the button and it's just the start.
image.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(#"images/Button-warning-icon.png", UriKind.Relative));
image.Stretch = Stretch.Uniform;
buttonWarnings.Background = image;
If I understand you correctly, you are trying to change the appearance of the Button control in a "pressed" visual state.
I'm not near my dev computer to try it out, but to "unblock you" I'll give a direction.
First, as you noticed in your Blend screenshot, each visual state is represented with a Storyboard, which defines how various properties change. In your case, you're looking to change Background property.
The VisualStateGroups and their states are defined by the control. You can override them when you re-template the control. So, retemplate the button control using Blend with "Edit Template"->"Edit Copy".
Then, in code, you should be able to do the following:
1) Get visual states (this would not work unless you re-template the control, AFAIK)
var visualStateGroups = VisualStateManager.GetVisualStateGroups(buttonWarnings);
2) Get the VisualStateGroup of "CommonStates" from the visualStateGroups
collection
var commonStatesGroup = visualStateGroups.Find((g) => ((VisualStateGroup)g).Name == "CommonStates") as VisualStateGroup;
3) Get the "Pressed" VisualState:
var pressedVisualState = commonStatesGroup.Find((vs) => ((VisualState)vs).Name == "Pressed") as VisualState;
4) Change the storyboard of that state
pressedVisualState.Storyboard = newStoryboardWithCustomImageBackgroundProperty;
(Disclaimer: I'm not near in a computer to try it now - it's all in theory)
There are many examples to be found on the internet!
Take a look at some:
http://mobile.dzone.com/articles/windows-phone-buttonimage
http://loekvandenouweland.com/index.php/2011/01/windows-phone-image-button/
Actually its quite simple,
While in button pressed state....see part 3 in the image you uploaded above.
Above all the colors there is a row containing 5 icons.
Click on 4th icon.
it will show you option to choose image as background.

Comboboxes unexpectedly automatically closing when mouse is released

I've stumbled upon a phenomenon which I can't explain myself and I'm interested in why it is happening. I hope someone can explain me the reason and also how to get rid of this phenomenon:
I have created a few pages for the application and then a search page. So far so good. Whenever I tried to click on the combobox itself it opened normally, but as soon as I released the mousebutton it closed again. Except when I moved the mouse outside of the combobox area and THEN released it. The only thing out of the ordinary I saw there was that the focus was reset automatically to the last text-field before the combobox.
The combobox itself was a normal html combobox while the textbox itself was created with Html.TextBox("search") I also tried to change the namings in case I overlooked something in javascript but no changes.
I then tried the following:
#Html.TextBox("search")
Kategorie #Html.DevExpress().ComboBox(
settings => {
settings.Name = "PrductCategory";
settings.ControlStyle.CssClass = "select";
settings.Width = 300;
settings.SelectedIndex = 0;
settings.Properties.DropDownStyle = DevExpress.Web.ASPxEditors.DropDownStyle.DropDown;
settings.Properties.IncrementalFilteringMode = DevExpress.Web.ASPxEditors.IncrementalFilteringMode.Contains;
settings.Properties.TextField = "Name";
settings.Properties.DisplayFormatInEditMode = false;
settings.Properties.Native = false;
settings.Properties.TextFormatString = "{0}";
settings.Properties.DisplayFormatString = "{0}";
settings.Properties.ValueField = "Id";
settings.Properties.ValueType = typeof(int);
}
).BindList(Categories).GetHtml()
This behaved as it should have the whole time (although the combobox was horribly formated. As I did not find out how to format it to look like a "normal html" combobox I decided to try the native mode but more to that below). When I set the native mode to true, the same phenomenon happened again (with false again it behaved normal).
After a few hours of looking through tutorials and docs I'm still at a complete loss (especially as I didn't find any setfocus commands being used).
So my question is:
Does anyone have any clue as to why that could happen and how to stop this phenomenon from happening?
Tnx
Remarks:
When in native mode and I switch via tab onto the combobox I can go through the list as normal (with the arrow keys), but I still can't open the combobox as it again closes automatically and the focus is reset onto the textbox ("search") as in all other cases (aside from native=false).
When native mode is set to false and I click on the combobox, then the focus is lost (and set to the text field before the combobox [and it's textfield] for under 1 second and then set to the combobox while the combobox does NOT close).
Thomas,
It sounds like some of the DevExpress settings you're using to initialize the ComboBox may be causing strange behavior in the browser. Could you include a copy of what the rendered control looks like from the browser source?
It might be worth removing some of the optional settings such as IncrementalFilteringMode to see if that is causing a problem.

How to programmatically set selected Panorama item in WP7

I'm using a panorama control in a WP7 app. One of the PanoramaItems takes you to another page, which then allows you send an email through the EmailComposeTask. If you don't select to send the email and press the back button, the Panorama returns to the item you last selected. However, if you do select to send an email (and therefore leave the app), it does not return to the previously selected PanoramaItem. Instead, it returns to the first item in the Panorama. I tried keeping track of the selected index and setting it, but I got an error saying the SelectedIndex is not settable. This is confirmed on MSDN documentation http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.panorama.selectedindex%28VS.92%29.aspx
Is there any way to manually set the selected index/item on a panorama? If not, is there a way for it to remember what was selected, even if the user leaves the app to compose an email?
I'm not sure if you can programmatically force an animation to another PanoramaItem, but you can change the Panorama.DefaultItem.
So you might have 3 PanoramaItem's and on the OnNavigatedTo() handler, change the default item via:
panoramaControl.DefaultItem = panoramaControl.Items[indexToSet];
This should help when you recover from a tombstone.
You could try the solution posted by Silicon Shark in this thread. It's noted to work, but only on the initial display - which shouldn't be a problem for your requirements of restoring state after tombstoning.
How to programmatically set the visible item in a Panorama control?
You can get the currently active page from the panorama's SelectedIndex property.
Unfortunately setting DefualtItem is only an approximation to solving this problem, which you may have discovered already.
Edit: Be aware that setting DefaultItem, changes which page of the panorama is the first page. It's a subtle difference, but you will see how it matters looking at the positioning of the heading and the wrap around of the background image.
Here is a solution. It does work as expected and does not rearrange your panorama, so your user interface is consistent.
pan.SetValue(Panorama.SelectedItemProperty, panoramaItem);
Panorama temp = pan;
LayoutRoot.Children.Remove(pan);
LayoutRoot.Children.Add(temp);
LayoutRoot.UpdateLayout();
this is not a perfect solution in that it does not slide nicely like panorama should, and it is probably not very efficient, but on the other hand you are not changing the default item so your user interface stays consistent.
I tested solutions listed here without success. Here is what I did that works like a charm!
PanoramaItem panItem = (PanoramaItem)panorama.Items[1];
panorama.Items.Remove(panItem);
panorama.Items.Insert(0, panItem);
You need to remove the panel from the list and re-inserting it at the desired position!
Set new selected item by
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[newSelectedItem]);
However, it work only on the initial so my idea is let the panorama control re-init when we change the selected item. This is my code, just add this after Panorama.SelectedItem changing.
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Collapsed;
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[(curIndex + 1) % pan.Items.Count]);
pan.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Visible;
But there is not transition effect now! Although, you can create your self.
It work great for me, this page also create a effect for sliding right http://xme.im/slide-or-change-panorama-selected-item-programatically
I'm using this model to change to a pivot when the device goes into landscape view, I'll probably end up extracting the current item to the application state. The panorama is a no-go in landscape orientation.
private int hub_page_index;
protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
base.OnOrientationChanged(e);
if (panorama.Visibility == Visibility.Visible)
{
hub_page_index = panorama.SelectedIndex;
}
else if (pivot.Visibility == Visibility.Visible)
{
hub_page_index = pivot.SelectedIndex;
}
if (e.Orientation == PageOrientation.Landscape
|| e.Orientation == PageOrientation.LandscapeLeft
|| e.Orientation == PageOrientation.LandscapeRight)
{
// Display Pivot in Landscape orientation
pivot.SetValue(Pivot.SelectedItemProperty, pivot.Items[panorama.SelectedIndex]);
panorama.Visibility = Visibility.Collapsed;
pivot.Visibility = Visibility.Visible;
}
else
{
// Display Panorama in Portrait orientation
panorama.SetValue(Panorama.SelectedItemProperty, panorama.Items[pivot.SelectedIndex]);
pivot.Visibility = Visibility.Collapsed;
panorama.Visibility = Visibility.Visible;
}
}

Categories

Resources