Where is the form icon saved in a WinForms application? - c#

I don't know where is the icon of an form saved...
I actually search it in project resources and did not found it, also it is not specified any path to it into the Properties of the form, still it occurs on form.
Is it somewhere serialized ? I just trying to find it and modify it and be sure that the new added icon will stay there...
Is it enough to just set a new icon from the form properties of the form and that one will automatically be saved in the location of the old one ?
To be more specific I also added a picture having an example with the icon of a form that I am talking about.

Open your form in the designer. Open the Properties box. Click the form to select it. Look for the "Icon" element in the Properties box in the Window Style group.
If you change the icon via the Properties box, then look at your designer code, you will see something like this (Form1.Designer.cs):
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1008, 540);
this.Controls.Add(this.btnTestReplacePerformance);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtResults);
this.Controls.Add(this.btnGetMatches);
this.Controls.Add(this.txtSource);
this.Controls.Add(this.txtRegex);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
And you can see that the icon (this.Icon) is loaded from the resources.

Related

How do I get a control's position and size from the original designer settings?

Windows (depending font/zoom/scaling) is resizing my form. I'd like to programmatically find the original position and size of the form and controls (which were set in the designer. At runtime, these sizes and positions change as the form is created.
Windows (depending font/zoom/scaling) is resizing my form.
Windows is not resizing your form. Your form is configured to auto-scale. This is controlled by the AutoScaleMode Property. The designer default value is System.Windows.Forms.AutoScaleMode.Font (the true default is Inherit). A high-level overview of the WinForm auto-scaling can be found in the documentation Current support for automatic scaling. A common alternative value to use is System.Windows.Forms.AutoScaleMode.DPI. When auto-scaling is enabled, a scaling factor computed using the value of AutoScaleDimensions and a value evaluated at runtime using the DPI reported to the application. AutoScaleDimensions is set in designer genearated code.
If the application is declared to be DPI-aware, the true DPI value is used. If it is not DPI-aware, Windows tells that application that it is running at 96 dpi. The reported DPI value also affects the metrics used in Font-based scaling.
I'd like to programmatically find the original position and size of the form and controls (which were set in the designer. At runtime, these sizes and positions change as the form is created.
This can be accomplished by intercepting the point before auto-scaling is applied. There is not a direct way to do this as you are dealing with auto-generated code (the Form.designer.cs file's InitializeComponent method). It is in this method that AutoScaleDimensions and AutoScaleMode are set.
When you create a new form, and this method looks like the following.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
Notice that the AutoScaleDimensions property is not yet declared in the method; it will be added once you make some change to form (change a property or add a control) and the new information is written to the file. The AutoScaleDimensions property will only be written if AutoScaleMode is set to either DPI or Font.
The following is the result of changing the form's Size.
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(782, 453);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
The purpose for showing the above is show you how to obtain the value for setting the AutoScaleDimensions property in the following proposed solution.
Proposed Solution:
In the designer, set the form's AutoScaleMode property to either Inherit or None. Then modify the form's constructor code to reflect the following:
SizeF scalingFactor;
public Form1()
{
InitializeComponent();
SuspendLayout();
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
AutoScaleDimensions = new System.Drawing.SizeF(8.0F, 16.0F);
// at this point you can obtain the scaling factor that will
// be applied on ResumeLayout
scalingFactor = AutoScaleFactor;
// store the design-time bounds in the control's Tag property
RecursivelyRecordBounds(this);
ResumeLayout(true);
}
Also add this method to the form's code.
private static void RecursivelyRecordBounds(Control c)
{
c.Tag = c.Bounds;
foreach (Control cc in c.Controls)
{
cc.Tag = cc.Bounds;
if (cc.Controls.Count > 0)
{
RecursivelyRecordBounds(cc);
}
}
}
For the purposes of this example, each control's design-time Bounds is stored in its Tag property. Note that setting the two auto scaling properties must be wrapped in a SupendLayout/ResumeLayout region. Also it is important to first set the AutoScaleMode property before setting the AutoScaleDimensions property.
An alternative solution (hack) would be leave the AutoScaleMode property set to either Font or DPI and to rely on the observed pattern of the Initialize method. In this method, the form's Text property is always set after the two scaling properties. This allows using the TextChanged event to be used to signal the proper time to retrieve the design-time Bounds.
public Form1()
{
EventHandler eh = (s,e) => RecursivelyRecordBounds(this);;
TextChanged += eh;
InitializeComponent();
TextChanged -= eh;
}

c# ListView Tiles view always appears as Largeicons view [duplicate]

I have a inherited Listview which standard has to be in Tile Mode. When using this control, the DrawItem gives e.bounds which are clearly bounds of largeIcon view ?? When debugging to check the view it is actually set to, it says it's in Tile view ?? Yet e.DrawText draws LargeIcon view ??
......... Edit: .................
This seems only to happen when the control is placed upon another usercontrol?
......... Edit 2: .................
It gets stranger ... When i add buttons next to the list to change the view at runtime, "Tile" is the same as "LargeIcon", and "List" view is the same as "SmallIcons" ??? I've also completely removed the ownerdraw ...
.......... Edit 3: .................
MSDN Documentation:
Tile view
Each item appears as a full-sized icon
with the item label and subitem
information to the right of it. The
subitem information that appears is
specified by the application. This
view is available only on Windows XP
and the Windows Server 2003 family.
On earlier operating systems, this value is ignored and the ListView
control displays in the LargeIcon
view.
Well I am on XP ?!?
...... Edit 4 .....................
Holy mother of strangeness ...
We are now at the point we've completely stripped down EVERYTING ... We have a standard listview on a form, manually filled with 3 values. No Ownerdraw. It is set to Tile.
When we start this form, the list is drawn as LARGEICON.
Now, we start another blank solution, copy this exact same form to the new project, start debug and low and behold .. it is drawn in TILE view ????
... help ...
public class InheritedListView : ListView
{
//Hiding members ... mwuahahahahaha //yeah i was still laughing then
[BrowsableAttribute(false)]
public new View View
{
get { return base.View; }
}
public InheritedListView()
{
base.View = View.Tile;
this.OwnerDraw = true;
base.DrawItem += new DrawListViewItemEventHandler(DualLineGrid_DrawItem);
}
void DualLineGrid_DrawItem(object sender, DrawListViewItemEventArgs e)
{
View v = this.View;
//**when debugging, v is Tile, however e.DrawText() draws in LargeIcon mode,
// e.Bounds also reflects LargeIcon mode ???? **
}
................................
This code behaves differently at different solutions:
private void InitializeComponent()
{
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("fhsdhdsfhsdfhs");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("fdshdsfhdsfhsd");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("hdshsdfhsdfhsdfsdfsdf");
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// listView1
//
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2,
listViewItem3});
this.listView1.Location = new System.Drawing.Point(36, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(487, 242);
this.listView1.TabIndex = 2;
this.listView1.TileSize = new System.Drawing.Size(480, 50);
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Tile;
//
// TestControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(595, 712);
this.Controls.Add(this.listView1);
this.Name = "TestControl";
this.Text = "TestControl";
this.ResumeLayout(false);
}
#endregion
OK, we found it. The magic spell is:
Application.EnableVisualStyles();
We skipped this line of code to test our form.
If you don't call this method before you create your form with your listview, TILE view gets drawn as LARGEICON.
Seems totally logical ... :-(
http://blogs.msdn.com/rprabhu/archive/2003/09/28/56540.aspx
Q What does Application.EnableVisualStyles actually do?
Windows XP ships with two versions of the Common Controls Library (comctl32.dll) - versions 5.8 and 6.0. v5.8 renders controls in the "Classic" style that you get on Windows NT/2000 and Windows 9x. v6.0 renders controls using the XP Visual Styles look and feel. Since most Windows Forms controls are based on comctl32, how they are rendered depends on which version of comctl32 is used to do the rendering. By default, v5.8 is used to render the client area of the app and v6.0 is used to render the non-client area. That is why you see the title bar and window borders automatically render "themed", while the controls (like Button, TextBox, ListView, ComboBox and so on) have the classic look by default.
In v1.0 of the Framework, the way to get visual styles in a Windows Forms app was to ship a manifest file with the app, that has information in it to indicate that v6.0 of comctl32 should be used for rendering. While this works fine, many developers felt it cumbersome to author, maintain and deploy manifest files. They felt the need to be able to do this programmatically. Now, the Platform SDK does provide API to do this. Basically, you need to create and activate an Activation Context that has pretty much the same DLL redirection information in it as the manifest file. The Activation Context API can be used to do this in a way suitable to your application.
If you take a look at these API, you will probably notice that they aren't very easy to use. While the advanced developers may like to tinker around with activation contexts, it is probably not something a developer who wants some "quick and dirty" code to get visual styles will do. So the Windows Forms team decided to wrap these API and expose a simple method that developers could call, that would isolate them from these complexities. So, essentially, when you call Application.EnableVisualStyles, we set up an activation context around the application's message loop, so that comctl32 function calls can be properly redirected to comctl32 v6.0. That way, you don't need to include a manifest with your app.

GLControl is not showing up in toolbox

For a Computer Graphics class in school I have been assigned to use a GLControl in my project. I have downloaded and installed OpenTK and OpenTK.GLControl. Both of the references appear within my project references tab in solution explorer. I have been trying to find out how to add the GLControl to my toolbox.
The things I have tried.
I have done the tools -> choose toolbox tools -> (select GLControl) but it isn't there to select to add to toolbox
I have attempted to drag the reference of the tool to the toolbar as someone suggested doing on the web.
I have uninstalled my nuget packages and reinstalled them.
Crying for hours and hoping that it works.
What if anything is there that I can do to make this work as I am unable to get anywhere without this control.
As the author of an IDE that makes heavy use of OpenTK's GLControl (SGDK2) I can tell you that I never put the GLControl in the toolbox, and I don't think I would want to. It's not exactly the kind of control you want to deal with at design time. Instead I simply added a dummy control (a Panel) to my form, then went into the designer code file (Form1.Designer.cs, for example) and replaced every occurrence of System.Windows.Forms.Panel with OpenTK.GLControl. Pretty much everything I do with the control is in code, so the designer integration isn't that important anyway.
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new OpenTK.GLControl();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(32, 37);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(223, 175);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private OpenTK.GLControl panel1;
EDIT:
That said, I did just try right-clicking on the General section of the Toolbox, selecting "Choose Items..." and browsing to the location of OpenTK.GLControl.dll. This added GLControl to the General category in my toolbox. So I'm not sure why you would be having difficulty. But hopefully forcing it onto a form as shown above will either get it into your project anyway or give you a better error or explanation about why it's not working with your project.

C# Index is unpredictable when adding controls to a panel

I have no code to show because this is a problem with me not understanding the behavior of the designer in VS2015 using C#. I have added a series of labels to a panel, so I can iterate through them in code. The problem is, it seems no matter what order I add the labels to the panel, the indexes of the controls make no sense.
Here is a screen shot. The back colored labels to the right are all contained in a separate panel. I have coded the labels to show their index within the panel container.
These were added one at a time from the bottom up. How can I manually add the labels and still have predictable indexes?
Any help is appreciated.
This is the result after making the labels the same size, renaminging them lbl0, lbl1 etc. and adding them one at a time from top to bottom....
You can see where controls are added to the control collection if you look in the Form.Designer.cs file, which is part of the class definition for your form.
Here you will see a section that begins with // Form, and under that you will see where it calls `this.Controls.Add();
The items in this list appear in the order in which you dropped them onto the form (at least for me they do). I just copy/pasted 10 labels onto the form, and I see this:
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 684);
this.Controls.Add(this.label10);
this.Controls.Add(this.label9);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
At runtime, the controls are found in the collection exactly as you would expect from reading the code above; the last control added is at index [0]:
Now, adding a panel is a slightly different story, but not much. Because the Panel is a container object, the labels get added to the Panel controls collection:
//
// panel1
//
this.panel1.Controls.Add(this.label18);
this.panel1.Controls.Add(this.label17);
this.panel1.Controls.Add(this.label16);
this.panel1.Controls.Add(this.label15);
this.panel1.Controls.Add(this.label14);
this.panel1.Controls.Add(this.label13);
this.panel1.Controls.Add(this.label12);
this.panel1.Controls.Add(this.label11);
this.panel1.Location = new System.Drawing.Point(37, 366);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 172);
this.panel1.TabIndex = 13;
Note, however, that the behavior is the same. The most recent control is added to the Controls collection first, and will be in the Panel.Controls[0] position.

Object does not match target type

I have a TableLayoutPanel with a grid of PictureBox controls within it. I'm trying to find a shortcut way to change them all to Label controls instead of manually deleting each one and placing new controls in each cell.
I thought I could go into the designer code and find/replace PictureBox with Label, but now I get an
"Object does not match target type"
error in Visual Studio's error list. I can't view the designer page now either. Is this not allowed? If it is allowed, what's the right way to do it?
If you take a closer look at the generated code:
label1:
this.label1 = new System.Windows.Forms.Label();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(134, 163);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
pictureBox1:
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(97, 75);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
My guess is that the
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
is changed by you into something like:
((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
which doesn't work, and results in designer issues. Object does not match target type.
so, apply the changes you already did, remove the lines like:
((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.label1)).EndInit();
and I think you're good to go.
Don't change designer code. That stuff is automatically generated. Not only can your changes cause unexpected behavior, but they can get over-written as well.
I would attempt to make a change or 2 to your form, or whatever your designer is behind, and hope it regenerates all it's code.
You can delete all the picture boxes in the designer, then add all the labels in the _load event (or another convenient event). That way it will be easier to change next time.
As Haxx illustrated, you will have to clean-up the extra initialization PictureBox requires as well. The error you received is a interface casting error. In your case, as Haxx guessed, the Label control doesn't implement the ISupportInitialize interface.
Unlike most, I am not afraid of changing designer code in the interest of expediency, for what you are doing, it is ok to do so. Just know your objects, check-in prior to doing so, and don't put custom code in there!

Categories