Trying to create a simple custom control in C# Winforms that inherits from Panel, but as soon as I change it to inherit from "Panel" instead of "UserControl" I get this error:
Here's the code for the entire class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SETPaint
{
public partial class Canvas : Panel
{
public Canvas()
{
InitializeComponent();
}
private void Canvas_Load(object sender, EventArgs e)
{
}
}
}
Delete the line 'this.AutoScaleDimensions = ...' in your Designer.cs file (line 35 according to the exception). There's probably another similar to 'this.AutoScaleMode = .Font' too.
This problem arises because you've used the Designer when the Control derived from UserControl, and it set some default properties in the InitializeComponent() method. Those properties are part of the UserControl base type, but not the Panel base type.
Since the IDE Designer can't load the Designer.cs file to fix this problem, you need to do it manually.
Your code should be like this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SETPaint
{
public class Canvas : Panel
{
}
}
And on the toolbox there should be an item called Canvas after you build the solution
Related
Creating a round button for my WPF app in visual studio 2013. The problem here is onPaint() Method generates error asking me to enter some valid parameters.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace CurrencyTeller
{
class CircularButton:Button
{
protected override void OnPaint (PaintEventArgs pevent)
{
GraphicsPath grpath = new GraphicsPath();
grpath.AddEllipse(0,0,ClientSize.Width,ClientSize.Height);
this.Region = new System.Drawing.Region(grpath);
base.OnPaint(pevent);
}
}
}
Error 3 Argument 1: cannot convert from 'CurrencyTeller.PaintEventArgs' to 'System.Windows.Forms.PaintEventArgs'
From what I can see, youre creating a Forms control, not a WPF control. Create a new "WPF User Control" in your Project and port your button to the template. Beware that Windows.Forms controls dont work in a WPF enviroment.
Best you look at some WPF Control tutorial.
I have a VSTO project where a click of a button on the ribbon, opens a new Word doc with a specific template, and opens it with a custom task pane. On that pane is a button, which when clicked, i want to add a row to a table which exists in the doc template.
Currently i'm just getting a 'command failed' exception at run time when the button on the task pane is clicked. Here is the whole class. As you can see i've tried two ways to do it, both fail:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using word = Microsoft.Office.Interop.Word;
namespace TestWordAddin2010
{
public partial class NewDescDMtaskPane : UserControl
{
public NewDescDMtaskPane()
{
InitializeComponent();
}
private void addSpare_Click(object sender, EventArgs e)
{
Globals.ThisAddIn.Application.ActiveDocument.Tables[1].Rows.Add(Globals.ThisAddIn.Application.ActiveDocument.Tables[1].Rows[1]); //this doesn't work
//word.Table wordTable = Globals.ThisAddIn.Application.ActiveDocument.Tables[1];
//wordTable.Rows.Add(wordTable.Rows[1]); //neither does this work
}
}
}
Any help appreciated.
Most likely your first table contains merged cells. Then you won't be able to access individual rows in the Rows collection.
As a workaround you could use the Selection object as follows:
ActiveDocument.Tables(1).Range.Select();
Application.Selection.Collapse();
Application.Selection.InsertRowsAbove();
I have a strange browser issue regarding my windows forms app.
I have placed a web browser in my form and I'm simply trying to load google.
The problem is that the browser is auto refreshing it every few seconds and it makes it useless.
I have used:
webBrowser1.Url = new Uri("http://google.com");
and
webBrowser1.Navigate("http://google.com");
And the result is the same. The page is still auto refreshing. It's the first time I'm facing this issue. Is there anyone who faced it and can help me?
I'm running the code on Visual Studio 2012 - windows 7 x64
EDIT:
Here is the code of the 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 Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate("http://google.com");
}
}
}
I can't see how you're wiring things up, but to me it looks like you're wiring WebBrowser.DocumentCompleted to navigate to Google. The problem is;
The WebBrowser.DocumentCompleted event occurs when the WebBrowser control finishes loading a document.
In other words, every time you get an event that the page has finished loading, you're calling webBrowser1.Navigate("http://google.com"); which reloads it again.
I created an UserControl and everything works great except for the properties. The problem is I lost all the Design Property in my UserControl.. to be more clear, i made a screenshot.
This is my UserControl
..and a ToolStrip sample
this is my sample code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel.Design;
namespace _Sample__User_Control
{
[Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(IDesigner))]
public partial class ClientsCollectionListBox : UserControl
{
public ClientsCollectionListBox()
{
InitializeComponent();
}
[Browsable(true)]
[DefaultValue("Hello")]
public string MyString { get; set; }
}
}
my User Control doesn't have the Design Property. Upon dragging the User Control to my form, there's still the Design Property but upon running or adding another instance to this control, i lost the display. Any help for this problem would be appreciated. Do i need to Invalidate on load?
SOLUTION:
Spelling error.. sorry.. please close this thread..
[Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(IDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(IDesigner))]
Is there a way I can change the font types in a MessageBox.Show() to get bigger size, bold, italic styles?
You can always make your own MessageBox creating a new Windows.Forms class:
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 MessageBoxFont
{
public partial class Message : Form
{
public Message(String text)
{
InitializeComponent();
tbxMessage.Text = text;
btnOK.Focus();
}
private void btnOK_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Then you can control the properties (like the font, size, color and the like) shown under the solution explorer. You initialize this form like this:
private void OpenMessageBox()
{
String text = "This is a sample error message";
Message message = new Message(text);
message.Show();
}
Its a work-around, however, easier to implement :)
I believe that those fonts are controlled by the operating system.
You could (however) make a custom dialog and put anything you want in there including custom fonts.
Here is the MSDN resource for custom dialogs.
http://msdn.microsoft.com/en-us/library/2chz8edb(VS.90).aspx
Have you thought of something like a customized message box (www.html-messagebox.com)?
For more customization such as building an irregular shaped message box (Homer Simpson's head), you are better off creating your own MessageBox-like implementation for your project.
Check this http://www.windowsdevelop.com/windows-forms-general/change-font-size-for-messageboxshow-dialogs-62092.shtml