Custom control error - c#

I am working on this custom control. (I am very new to this part of programming.) I am working on an application that has to be able to format mathematical expressions as the user enters the input in my own custom control. This is how I want the control to look like (this image is made in Photoshop):
I will not explain the behavior I want it to have, because this doesn't help you, but the idea is that it is not based on any Windows Control.
Tis is the code I already have:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Support.Components
{
public partial class PartialExpressionEditor : Control
{
public PartialExpressionEditor()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Brush background = Brushes.White;
pe.Graphics.FillRectangle(background, ClientRectangle);
background.Dispose();
}
}
}
When I try to put it in my form, I get this error dialog:
Where is the problem? Or why this error appears?

I think the problem is that you are disposing a system brush:
// background.Dispose();
since you didn't create it:
Brush background = Brushes.White;
To use your own brush that you dispose yourself:
using (SolidBrush br = new SolidBrush(Color.White)) {
pe.Graphics.FillRectangle(br, this.ClientRectangle);
}
You might have to exit Visual Studio to get your Brushes.White brush back.

Related

How To Change Only Font-Family Of All Controls On WinForm Using C# - Not Font-Size

I have an embedded font in my winform application.
Here is the codes :
This is what worked for me in VS 2013, without having to use an unsafe block.
Embed the resource
Double-click Resources.resx, and in the toolbar for the designer click Add Resource/Add Existing File and select your .ttf file
In your solution explorer, right-click your .ttf file (now in a Resources folder) and go to Properties. Set the Build Action to "Embedded Resource"
Load the font into memory
Add using System.Drawing.Text; to your Form1.cs file
Add code above and inside your default constructor to create the font in memory (without using "unsafe" as other examples have shown). Below is my entire Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.Drawing.Text;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
private PrivateFontCollection fonts = new PrivateFontCollection();
Font myFont;
public Form1()
{
InitializeComponent();
byte[] fontData = Properties.Resources.MyFontName;
IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
uint dummy = 0;
fonts.AddMemoryFont(fontPtr, Properties.Resources.MyFontName.Length);
AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.MyFontName.Length, IntPtr.Zero, ref dummy);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
myFont = new Font(fonts.Families[0], 16.0F);
}
}
}
Use your font
Add a label to your main form, and add a load event to set the font in Form1.cs:
private void Form1_Load(object sender, EventArgs e)
{
label1.Font = myFont;
}
Now i want to change Only Font-Family of all controls on WinForm using c#, Not only a label & Not Font-Size.
Mean i want to keep Font-Size of all controls.
Here is related site about this issue.
how-to-change-font-family-of-all-controls-on-window
But in codeproject answers they changed both font-family & font-size.
How can i change only font-family of all controls, not font-size?

How to copy only numbers from masked textbox to a label?

I made a masked textbox for saving numbers with the mask (999) 000-0000 and I want to show only numbers in label but when I do that, it also copies the parantheses and lines.
I know it copies all the text. How I can only copy numbers entered not with mask?
(windows form)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = maskedTextBox1.Text;
}
}
}
One solution is to set TextMaskFormat to ExcludePromptAndLiterals just before reading it's value:
maskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
Console.WriteLine(maskedTextBox1.Text);
//will print 3123 when value in the mask textbox is (31) 23 for mask (00) 00
And after this set Format back:
maskedTextBox1.TextMaskFormat = MaskFormat.IncludeLiterals;
Even if you won't set format back to IncludeLiterals, UI control still would show masked text (31) 23 and will work as usual. This is done if your other logic relies on masked Text field.
So if you don't have such dependencies, you can set this value right in the Visual Studio designer in properties window for maskedTextBox1

Where are the Properties gone ? I did Properties.Resources but properties not exist

This is the code in my class i have:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
namespace Magnifier20070401
{
public partial class MagnifierForm : Form
{
public MagnifierForm(Configuration configuration, Point startPoint)
{
InitializeComponent();
//--- My Init ---
mConfiguration = configuration;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = mConfiguration.ShowInTaskbar;
TopMost = mConfiguration.TopMostWindow;
Width = mConfiguration.MagnifierWidth;
Height = mConfiguration.MagnifierHeight;
// Make the window (the form) circular
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
mImageMagnifier = Properties.Resources.magnifierGlass;
On the Properties im getting: Error 1 The name 'Properties' does not exist in the current context
I added this Form as Existing item to my project and im getting an error on this Properties.
And i have the magnifierGlass image in the Resources.
Found it needed to add my project name before the properties:
mImageMagnifier = ScreenVideoRecorder.Properties.Resources.magnifierGlass;
Now it's working thanks.

Adding controls to the Panel works the first couple of times, then fritzes out

Here's screenshots:
Here's the code I'm using to load the pictureBoxes to the Panel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WebServiceScanner
{
public partial class MainForm : Form
{
int pictureYPosition = 8;
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LoadImageFromScanner();
}
private void LoadImageFromScanner()
{
Image pic = Image.FromFile(#"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg");
PictureBox pictureHolder = new PictureBox();
pictureHolder.Image = pic;
pictureHolder.SizeMode = PictureBoxSizeMode.StretchImage;
pictureHolder.Size = new System.Drawing.Size(180, 250);
pictureHolder.Location = new Point(13, pictureYPosition);
panel1.Controls.Add(pictureHolder);
pictureYPosition += 258;
}
}
}
What could be causing the problem? The Panel has Autoscroll set to true, so maybe that's causing the issue?
IMPORTANT EDIT:
The pictures load absolutely FINE, if I don't touch the scrollbar and leave it in it's initial position (topmost). If I scroll down and add pictures it seems it has a different idea of where the point I'm giving it really is.
Any suggestions?
A panel scrolls its content by adjusting the Location property of its child controls when you move the scrollbar. You need to do this yourself when you add a picture. Fix:
pictureHolder.Location = new Point(13, pictureYPosition + panel1.AutoScrollPosition.Y);
Not sure if this will help. Before you add it to panel call panel1.SuppressLayout() then afterwards call panel1.ResumeLayout(true).
Another option is use a FlowLayoutPanel instead of manually incrementing the distance every time.
The solution that worked for me was to supresslayout and resumelayout, the one of Scott.
To substract the autoscroll Y position, didnt worked at all.

How do I get the scroll position from Microsoft Word

I want to position an image on the page the user is looking at, however I cannot find how to get the currently visible page/scroll in pixels.
Anybody know which object and property could give me that?
Are you trying to control Word from outside Word or is it an integrated control?
I think you want: Object oMissed = doc.Paragraphs[1].Range;
This code below is for an InlineShape, not Shape object. Shape object is for text-wrapping.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
namespace WordAddIn3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Word.Application wdApp = Globals.ThisAddIn.Application;
Word.Document doc = wdApp.ActiveDocument;
string fileName = "c:\\testimage.jpg"; //the picture file to be inserted
Object oMissed = doc.Paragraphs[1].Range; //the position you want to insert
Object oLinkToFile = false; //default
Object oSaveWithDocument = true;//default
doc.InlineShapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
}
}
}
Microsoft: HOWTO: How To Get 32-bit Scroll Position During Scroll Messages
Similarly, you may want to look at this SO question on How do I get the scroll position from Microsoft Execl -- which I just realized was asked by you..

Categories