Access ImageCollection of a UserControl in the Form Designer - c#

I want to make a user control which acts like a TabControl but the tab page buttons should have big images. Therefore I want to have a property Images which adds images to the user control like an ImageList. For every image, the user control then generates a PictureButton and a corresponding panel. For that reason I added an ImageList to the user control and I've also tried to just add a collection of Image.
The problem is, that I cannot access this ImageList from the main form designer.
I've tried the following properties:
public Collection<Bitmap> Images { get; set; }
// ----
public ImageCollection Images { get; set; }
// ----
public List<Bitmap> Images { get; set; }
// ----
public ImageList Images { get; set; }
When I try to add an image to a Collection<Bitmap> or List<Bitmap> in the designer then Visual Studio crashes. I cannot use Image instead of Bitmap.
I know that I could use TabControl itself and give each TabPage an image, but I don't like the look of it.
So what's the best way to add a collection of images to a user control from the main form?

Related

User Control: how to add background image on button in setter/getter

In user control designer I know how to add background image.
My image (16x16px) is located in user control. How to add this image in code?
How do I embed the image in user control so that when I bring the control to another project, the image also appears?
private Bitmap buttonResetImage = null;// <-- here I want to load my initial image.Then the user can change this image as he wishes;
public Bitmap ButtonResetImage
{
get { return buttonResetImage; }
set
{
buttonResetImage = value;
btnReset.BackgroundImage = buttonResetImage;
btnReset.BackgroundImageLayout = ImageLayout.Center;
}
}
Well since MDP does not respond I found the trick. I need to initialize this:
private Bitmap buttonResetImage = global::SliderControl.Properties.Resources.Reset;
If I use only:
private Bitmap buttonResetImage = SliderControl.Properties.Resources.Reset;
I get this error:
Error CS0117 'SliderControl' does not contain a definition for 'Properties' SliderControl
https://i.imgur.com/az52MOP.png
In order to create a user control that has any kind of resources like :
images
fonts
audio
icon
and etc, you can create them in a separate project (called Windows Forms Control Library) and add resources in it.
after all, the result(in this case, a dll file) contains all the resources and code base you need.you can use it in any where you want.

how create a datagrid with panel dynamically?

I'm french, so sorry for my bad english... But I hope that you will help me ;)
I'm developing a soft in c# with lot of information store in DB, like information about people.
I want display these information with a beautiful UI, like that :
For that, I created :
a first panel in Visual
a class for create sub panel dynamically
I make a loop and for each person, I call a method "createPanel" with the information of each person, and these sub panels created are added to the parent panel. The ID of each person is store in the "tag" parameter of the sub panel.
All work fine, but I don't really know how to add event in each sub panel.
As you can see in the link above, the user can click on a bookmark, or a icon for show a menu (the round with 3 point = icon "menu").
My question is how to add event for each sub panel dynamically ? For example I want display a menu for each sub panel when I click on the icon "menu", get the id store in the "tag" of the sub panel, and show a new form for manage information.
Thx for your help, I can add my code if necessary :)
Make a own user control for the panels you want to display containing all the controls from the toolbox you need.
public partial class MyControl : UserControl
{
...
}
In "MyControl" you define events for all things that can happen and must be handled externally of the control. Same as e.g. "SelectedIndexChanged" for combobox.
Your control class would then look like this:
public partial class MyControl : UserControl
{
public event EventHandler<MyImportantEventA> OnImportantThingHappend;
//Event-Invoker
protected virtual void OnOnImportantThingHappend(MyImportantEventA e)
{
OnImportantThingHappend?.Invoke(this, e);
}
}
//Data of your event. Customize as you like/need.
public class MyImportantEventA : EventArgs
{
public string Message { get; set; }
}
When you add your control dynamically, bind to the event(s) of your control:
myUserControl.OnImportantThingHappend += DoSomethingWithEvent;
Things the control can handle it self, don't need to be exposed as events.
E.g.:
"Close" may be something that must be handled externally. You need to remove the control, rearange you controls etc.
"Show Details" probably is something that can be handled completely inside of your control. Your control shows a message box or a fancy tooltip.

dataGridView image column link to file

I am working with an image column on dataGridView:
As shown in the attached picture, when I click on 'browse' I can choose a file from the dialog window.
What I want...
is to make the image on the 'Files' image-column to link to the chosen file (to get the file open when the user clicks on the image).
I have no idea how to make that happen. Could anyone give me a direction?
First You can create your own Class and put in this class Property for File Path
I've created example :
public class MyClass
{
public string Name{get;set;}
public string FilePath{ get; set; }// Here i put The path and it will not appear in the Grid will just show the first item
}
in DataGrid there is an event called SelectionChanged you can add this code there
if (DG.CurrentColumn.Header.ToString() == "Name")
Process.Start((DG.SelectedItem as MyClass).FilePath);
when i select the item it will open the folder you can put instead of ("Name") your column name ("Brows")

How to assign one image to all image sources of buttons

I am using WPF-Xaml I made interface in which user selects image and save it in database, then this saved image assigned to buttons ImageSource and similarly to all other Navigation Panel items, etc.
Problem is that: i have to retrieve image every time from database, Is there any other possibility to save image without connectivity to database i mean Application Settings ?
i am not using MVVM
Create a property in your view model that will hold this image:
public class ViewModel{
public ImageSource SharedImage{get;set;}
}
Then bind your controls that need to show this image to this property. This way the image is set once and used my times by other controls that need to access it.
EDIT
If you want to cache your file locally in your file system when your application first uses the image you could do the following:
public class ViewModel{
private ImageSource _sharedImage;
public ImageSource SharedImage{
get{
if(_sharedImage== null){
// Go load the image from somewhere
}
return _sharedImage;
}
set{
if(_sharedImage == null){
// Store the image contained in value into your local cache
}
_sharedImage = value;
}
}
}

Winform Custom Control: Why Setter not called when used from Constructor?

I have an initial value property like this:
[Category("Main")]
[Description("Intial Value")]
[DefaultValue(10)]
public int InitialValue
{
get { return m_initialValue; }
set {
m_initialValue = value;
this.TrackBar.Value = this.m_initialValue;
}
}
So in my constructor I do this for example:
this.InitialValue = 10;
To my surprise when dragging the custom control on a form the setter is not called so that my trackbar value is not synchronized.
Why ?
Only when I change the property in dialog box the setter is called.
I decided to take your advice as suggested in one of the comments:
You can try by yourself will take 2 minutes.
So I did (it took about 3 minutes), and I was unable to reproduce the behavior that you described.
Here are the exact steps that I followed:
Created a new Windows Forms Application.
Added a new User Control to my project.
Opened the new User Control in design view and added a TrackBar control (leaving the TrackBar control's properties all set to their defaults).
Added the following code to the User Control class (exactly the same as you posted above, with the addition of a private field m_initialValue that you omitted from the original example):
public class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.InitialValue = 10;
}
[Category("Main")]
[Description("Intial Value")]
[DefaultValue(10)]
public int InitialValue
{
get { return m_initialValue; }
set
{
m_initialValue = value;
this.trackBar1.Value = this.m_initialValue;
}
}
int m_initialValue;
}
Built the project.
Opened the default Form (Form1) that was created with the new project in design view.
Dragged the User Control that I had just created (UserControl1) out of the toolbox where it was automatically placed and onto the surface of the form.
The indicator on the slider bar appeared all the way to the right side (the correct and expected position given the default Maximum value of 10). Now, you tell me: What are we doing differently?
Try adding [Browsable(true)] .
The key portion of your question is here:
when dragging the custom control on a form
You're still in the designer, and the designer cheats a bit to render things. Does this still happen when you actually run the application?

Categories