How to add customized component in C#? - c#

I need to call acadcolor component from visual studio.when i add the component it's fine.
After I need to use that component so just I drag and drop that control to windows form visual studio automatically closed without passed any message.
Can anybody know how to add and how to work with acadcolor component from visual studio?
Thanks advance.

Here's an ADN article on just what Alex Filipovici was mentioning:
Use 64-bit ActiveX Component from a .NET Assembly
There are other alternatives too. Here's an ADN article replicating the control with WPF: WPF Implementation To Mimic Color Layer Controls.
You can also open the color dialog if it's to select a color. That's what I've done most recently:
using acColor = Autodesk.AutoCAD.Colors;
using acWindows = Autodesk.AutoCAD.Windows;
//...
public acColor.Color GetAutoCADColor()
{
acWindows.ColorDialog colorDialog = new acWindows.ColorDialog();
DialogResult dialogResult = new DialogResult();
dialogResult = colorDialog.ShowDialog();
switch (dialogResult)
{
case DialogResult.OK:
return colorDialog.Color;
case DialogResult.Cancel:
return Color.Empty.ConvertToAutoCADColor();
default:
return Color.Empty.ConvertToAutoCADColor();
}
}
Extension methods:
internal static class ColorExtensions
{
internal static Color ConvertToWindowsColor(this acColor.Color acColor)
{
return Color.FromArgb(acColor.ColorValue.ToArgb());
}
internal static acColor.Color ConvertToAutoCADColor(this Color winColor)
{
return acColor.Color.FromRgb(winColor.R, winColor.G, winColor.B);
}
}
Just a thought or two.

Related

C# Winforms Devexpress has other button tooltips?

In normal Winforms I have a maximize button with standard tooltips on mouse move.
Within devexpress my maximized button show after maximized "Restore Down" and in german "Wieder nach unten". It is bad, because not like the windows standard. I cannot change the tooltip, because I donĀ“t know how to access the maximize button of the form.
Language: C#, Winforms
WIth UI Lib: Devexpress
Can someone help me?
You can create your own Localizer class that inherits from DevExpress.XtraBars.Localization.BarLocalizer. Override the GetLocalizedString method and return what value you'd like for the minimize/maximize buttons. For instance:
public class MyRibbonLocalizer : DevExpress.XtraBars.Localization.BarLocalizer
{
public override string Language
{
get
{
return System.Globalization.CultureInfo.CurrentCulture.Name;
}
}
public override string GetLocalizedString(BarString id)
{
switch (id)
{
case BarString.MinimizeButton:
return "My Minimize string";
case BarString.MaximizeButton:
return "My Maximize string";
default:
return base.GetLocalizedString(id);
}
}
}
You will need to register this localizer class in your RibbonForm in order for it to work. I typically do this within the constructor:
DevExpress.XtraBars.Localization.BarLocalizer.Active = new MyRibbonLocalizer();

Get Aero Window Colour

I've made a pretty slick Windows 8-ish interface using WPF. It already turns out way better than I could wish for, but I was wondering the following:
Is it somehow possible to retrieve the current window colour set by the user? You know, you can set the Aero colour when you right-click the desktop... My plan is to use that colour for a couple of canvas elements on my GUI.
Thanks in advance!
The SystemColours class exists for this very purpose. You can bind directly to it like so
"{DynamicResource {x:Static SystemColors.WindowColorKey}}"
You can query the ColorizationColor registry key for this.
I've even went a step further and created a method to get the hexadecimal colour value, hope this helps you:
public void SomeMethod()
{
int argbColor = (int)Microsoft.Win32.Registry.GetValue(#"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM","ColorizationColor", null);
var color = System.Drawing.Color.FromArgb(argbColor);
string hexadecimalColor = ConverterToHex(color);
}
private static String ConverterToHex(System.Drawing.Color c)
{
return String.Format("#{0}{1}{2}", c.R.ToString("X2"), c.G.ToString("X2"), c.B.ToString("X2"));
}
I managed to get the correct colour using the following code:
Little sidenote: It has a small correction in it to ignore the alpha bit of the hex number, so I get the full color rather than the less saturated one.
string colorizationValue = string.Format("{0:x}", Microsoft.Win32.Registry.GetValue(#"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", "ColorizationColor", "00000000"));
StringBuilder bl = new StringBuilder(colorizationValue);
bl[0] = 'd';
bl[1] = '9';
colorizationValue = bl.ToString();
BrushConverter bc = new BrushConverter();
Brush brush = (Brush)bc.ConvertFrom("#" + colorizationValue);
cvs_barColor.Background = brush;
I created an open-source library for this here which is also available on NuGet.
install-package aerocolor-wpf.AeroColor
After installing the package, you can refer to a DynamicResource called AeroColor and AeroBrush depending on what you need.
There's some setup code that's needed too, but it isn't much. Just put something in your Loaded event handler of the window, as seen below.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
AeroResourceInitializer.Initialize();
}
}
The neat thing about this library is that it installs a hook as well, which updates those resources as the actual Aero color changes in the system too. This means you don't have to handle that either, and if you use a DynamicResource to point to the color in your XAML instead of a StaticResource, WPF will automatically update the color in your UI as well.
Looks very cool when Windows 8 changes the Aero color transitionally and your color follows.

How to write an add-in to change text color in Visual Studio editor?

After searching for a long time for a simple way of changing the text color of a #region directive in Visual Studio, I've concluded there is no easy way of doing so.
I know how to change the #region statement color, and how to change the collapsed region color, but I want to change the color of the text with the region description. So:
#region Some text <--- all this text should be in a different color
public void Test()
{
}
#endregion <--- this too
It seems a lot of people are looking for something like this - see How to change the color of expanded regions' titles in VS2008?.
So I've been looking at writing a simple Visual Studio add-in to change the color.
However, it's more complicated than I thought it would be, with classes like Snapshot, Tagger, Classifier, WpfTextViewCreationListener, AdornmentLayer etc.
Simply put, I don't know where to start! I followed a couple of tutorials at the MSDN site, but they seem too complicated for what I'm trying to do.
Can someone point me to the most simple way of doing this? Ie. which classes/methods/events within the VS SDK I should use. I don't mind if the color is not customisable via the UI etc either. I'm using VS2010.
Edit: Just had the mztools website recommended to me; I'll take a look there too. Also noticed that StackOverflow's syntax highlighting of regions is pretty much exactly what I want!
I eventually came up with a solution, at least for VS2010.
Whilst I have used this for coloring '#region' and '#endregion' tags, a similar solution ought to be applicable for any text content in a Visual Studio window.
It seems that this sort of problem can be resolved by creating a IViewTaggerProvider which will 'tag' parts of the source code with a 'classification'.
Visual Studio will provide a style for text tagged with that classification which can then be changed by the user to the desired style via Tools > Options... > Environment > Fonts and Colors.
The Tagger provider looks like:
[Export(typeof(IViewTaggerProvider))]
[ContentType("any")]
[TagType(typeof(ClassificationTag))]
public sealed class RegionTaggerProvider : IViewTaggerProvider
{
[Import]
public IClassificationTypeRegistryService Registry;
[Import]
internal ITextSearchService TextSearchService { get; set; }
public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag
{
if (buffer != textView.TextBuffer)
return null;
var classType = Registry.GetClassificationType("region-foreground");
return new RegionTagger(textView, TextSearchService, classType) as ITagger<T>;
}
}
This creates an ITagger object, which, given a Visual Studio text view, will tag parts of the text with the given classification type. Note that this will work for all text views (i.e. source code editor, 'Find Results' windows etc.). It may be possible to change this by editing the ContentType attribute (to just C#?).
The classification type (in this case "region-foreground") is defined as:
public static class TypeExports
{
[Export(typeof(ClassificationTypeDefinition))]
[Name("region-foreground")]
public static ClassificationTypeDefinition OrdinaryClassificationType;
}
[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = "region-foreground")]
[Name("region-foreground")]
[UserVisible(true)]
[Order(After = Priority.High)]
public sealed class RegionForeground : ClassificationFormatDefinition
{
public RegionForeground()
{
DisplayName = "Region Foreground";
ForegroundColor = Colors.Gray;
}
}
The Order attribute determines when the classification will be applied compared to other classifications which may also apply to a span of text.
The DisplayName will be used in the Tools > Options... dialog.
Once the classification is defined, an ITagger class can search a view's text and provide classifications for applicable sections of the text it finds.
Simply put, its job is to listen for the ViewLayoutChanged event of the text view, which is fired when the provided text view's content changes (e.g. because the the user has typed something).
It must then search the text for the area of text it is interested in (called a 'span'). Here, it returns spans of lines containing either #region or #endregion. I've kept this simple, but the TextSearchService used to find matches can also search using regular expressions.
Finally, a method is provided for Visual Studio to retrieve the tags of the text it has found, called GetTags(). For a given span collection, this will return text spans with classification tags, i.e. areas of those spans which should be classified in a certain way.
Its code is:
public sealed class RegionTagger : ITagger<ClassificationTag>
{
private readonly ITextView m_View;
private readonly ITextSearchService m_SearchService;
private readonly IClassificationType m_Type;
private NormalizedSnapshotSpanCollection m_CurrentSpans;
public event EventHandler<SnapshotSpanEventArgs> TagsChanged = delegate { };
public RegionTagger(ITextView view, ITextSearchService searchService, IClassificationType type)
{
m_View = view;
m_SearchService = searchService;
m_Type = type;
m_CurrentSpans = GetWordSpans(m_View.TextSnapshot);
m_View.GotAggregateFocus += SetupSelectionChangedListener;
}
private void SetupSelectionChangedListener(object sender, EventArgs e)
{
if (m_View != null)
{
m_View.LayoutChanged += ViewLayoutChanged;
m_View.GotAggregateFocus -= SetupSelectionChangedListener;
}
}
private void ViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
if (e.OldSnapshot != e.NewSnapshot)
{
m_CurrentSpans = GetWordSpans(e.NewSnapshot);
TagsChanged(this, new SnapshotSpanEventArgs(new SnapshotSpan(e.NewSnapshot, 0, e.NewSnapshot.Length)));
}
}
private NormalizedSnapshotSpanCollection GetWordSpans(ITextSnapshot snapshot)
{
var wordSpans = new List<SnapshotSpan>();
wordSpans.AddRange(FindAll(#"#region", snapshot).Select(regionLine => regionLine.Start.GetContainingLine().Extent));
wordSpans.AddRange(FindAll(#"#endregion", snapshot).Select(regionLine => regionLine.Start.GetContainingLine().Extent));
return new NormalizedSnapshotSpanCollection(wordSpans);
}
private IEnumerable<SnapshotSpan> FindAll(String searchPattern, ITextSnapshot textSnapshot)
{
if (textSnapshot == null)
return null;
return m_SearchService.FindAll(
new FindData(searchPattern, textSnapshot) {
FindOptions = FindOptions.WholeWord | FindOptions.MatchCase
});
}
public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
if (spans == null || spans.Count == 0 || m_CurrentSpans.Count == 0)
yield break;
ITextSnapshot snapshot = m_CurrentSpans[0].Snapshot;
spans = new NormalizedSnapshotSpanCollection(spans.Select(s => s.TranslateTo(snapshot, SpanTrackingMode.EdgeExclusive)));
foreach (var span in NormalizedSnapshotSpanCollection.Intersection(m_CurrentSpans, spans))
{
yield return new TagSpan<ClassificationTag>(span, new ClassificationTag(m_Type));
}
}
}
For brevity I have omitted namespaces and the using statements, which are typically of the form Microsoft.VisualStudio.Text.*. For these to be available, the Visual Studio 2010 SDK must first be downloaded.
I've been using this solution for the past few months without issue.
One limitation I noticed is colours are not 'blended', so a color with less than 100% opacity will not 'fade out' the existing colours in a span - which might be useful to preserve syntax highlighting.
I also have little idea of its efficiency, as it looks like it will repeatedly search a document on each keypress. I have not done the research to see if Visual Studio optimises this somehow. I do notice a slowdown of Visual Studio on large files (> ~1000 lines), but I also use Resharper, so I cannot attribute this to this plugin alone.
As this was coded mostly using guesswork, I welcome any comments or code changes which could clarify or simplify things or improve upon the code's performance.
I guess you could start with Visual Studio Add-in project, it will used EnvDTE, which was considered as Visual Studio Object Model, and please find MSDN document here:
http://msdn.microsoft.com/en-us/vstudio/bb968855
You could control your visual studio behavior, like debugger, code editor and so on by EnvDTE.

How to capture selected values from a dialog?

I created a FontDialog.cs Windows Form where my users can choose colors among other things for the text. I need to capture what the user has selected on the dialog:
Here's how I'm calling the dialog:
DialogsTestingGrounds.FontDialog dialog = new FontDialog();
dialog.ShowDialog();
How can I capture the selected values, I imagine I have to create properties for everything I want to transfer on the FontDialog.cs form end, right?
What you would want to do is expose properties on your FontDialog that make the values available. You could then use dialog.PropertyName to reference it by the name of the property.
It is not necessary, you can use, ie, dialog.Font to get the selected font,
dialog.Color for the color and so on...
Mitchel's answer will work but you might want to incorporate a couple other items along the same line.
Have a public property (per Mitchel's
answer).
Have a public constructor on your
form with the type of the property as
an argument so you can pass in the value
in question (this would allow you have the dialog prepopulated with old selection).
Surround your call to your dialog
with a check for dialogresult so you
only change the value when the user
wants to. (note the process for this is different in WPF)
Felice is also right in that you
don't really need to create a new
font dialog if the only thing you
care about is the font. There is a
built in font dialog in .Net
http://msdn.microsoft.com/en-us/library/system.windows.forms.fontdialog%28v=vs.71%29.aspx
So the internals of your dialog class may look like this psuedo code.
public Font SelectedFont { get; set; }
public FontDialog()
{
//set your defaults here
}
public FontDialog (Font font)
{
SelectedFont = font;
//dont forget to set the passed in font to your ui values here
}
private void acceptButton_Click(object sender, EventArgs e)
{
SelectedFont = //How ever you create your font object;
}
Then to call your function (assumes the the acceptButton above is the forms AcceptButton)
DialogsTestingGrounds.FontDialog dialog = new FontDialog();
if(dialog.ShowDialog() == DialogResult.OK)
//Do Something

How to make a visual reuasable class in C#?(Visual Studio 2010)

i have this class for example:
class labeledtext
{
private TextBox tb;
private Label l;
public labeledtext(Form frm)
{
tb = new TextBox();
l = new Label();
frm.Controls.Add(tb);
frm.Controls.Add(l);
}
public void SetCaption(string cpt)
{
l.Text = cpt;
}
public string text()
{
return tb.Text;
}
}
I want to make an object from this class, that can be used in any other project, as a visual object. I want to use it like a text box, drag it from toolbox to my form and put it and so on.
how can i do this?
I use visual studio 2010, if it is important.
I think you're looking for a user control.
In the solution explorer, right click, new usercontrol.
You can draw it in design time, and you can drag it wherever you want. (It will appear in the toolbar after you build the solution).
Here is a little guide: LINK

Categories

Resources