Лучший отвечающий
Создание собственного элемента управления

Вопрос
-
Всем доброго время суток. Столкнулся с такой проблемой нужно создать пользовательский элемент MyCustomControl содержащий ползунок (trackBar), числовое поле (numericUpDown), метки (label) и рисунок. Сам пользовательский элемент я создал и все туда добавил. Вопрос как объединить все это, чтобы стал единый целый пользовательский элемент, который можно добавить на форму и меня его свойства, а не каждого из перечисленных выше элементов по отдельности. Может где то надо галочку добавить, или какие то свойства поменять я все просмотрел, но пока к сожалению ничего не нашел. Подскажите пожалуйста. Заранее огромное спасибо.18 февраля 2011 г. 13:02
Ответы
-
Вам нужно самому добавлять необходимые свойства для UserConrol, например:
[Description("Устанавливает значение numericUpDown1"), Category("Values"), DefaultValue(0), Browsable(true)] public decimal NumValue { get { return numericUpDown1.Value; } set { numericUpDown1.Value = value; } }
Более подробно о этом можно почтить в разделе Разработка пользовательских элементов управления Windows Forms в .NET Framework, а именно Определение свойства элемента управления Windows Forms
Для связи [mail]- Предложено в качестве ответа Алексей Митев 18 февраля 2011 г. 15:39
- Помечено в качестве ответа Abolmasov Dmitry 20 февраля 2011 г. 17:18
18 февраля 2011 г. 15:02 -
Спасбо за советы, я сделал подругому у меня все работает
- Помечено в качестве ответа Наум Фишман 19 февраля 2011 г. 9:47
19 февраля 2011 г. 9:46
Все ответы
-
Вам нужно самому добавлять необходимые свойства для UserConrol, например:
[Description("Устанавливает значение numericUpDown1"), Category("Values"), DefaultValue(0), Browsable(true)] public decimal NumValue { get { return numericUpDown1.Value; } set { numericUpDown1.Value = value; } }
Более подробно о этом можно почтить в разделе Разработка пользовательских элементов управления Windows Forms в .NET Framework, а именно Определение свойства элемента управления Windows Forms
Для связи [mail]- Предложено в качестве ответа Алексей Митев 18 февраля 2011 г. 15:39
- Помечено в качестве ответа Abolmasov Dmitry 20 февраля 2011 г. 17:18
18 февраля 2011 г. 15:02 -
Подскажите пожалуйста, что нужно сделать, чтобы созданный мною пользовательский элемент появился впанели инструментов Toolbox, а затем его можно было перенести на форму и работать с ним.18 февраля 2011 г. 15:42
-
С начала я добавил элемент UserControl на него добавил (trackBar), числовое поле (numericUpDown), метки (label), он должен появиться в списке панели элементов, от куда его можно добавить на форму, но он почему то не появился в списке панели элементов.18 февраля 2011 г. 15:57
-
Просто запустите ваше приложение (F5), а потом сразу можете закрыть. Элемент должен появиться.
Для связи [mail]18 февраля 2011 г. 16:02 -
После того как собрал свой UserControl перестроил решение, UserControl появился на панели элементов. Добавил UserControl на форму. У меня три UserControl, соотвествующие трем цветам: red, blue, green зашел в свойство, чтобы выставить свойство ColorPart соотвественно: R,G,B, а там его нет и надо заменить значение метки Label на MyCustomControl его нет что еще нужно сделать. Подскажите пожалуйста. Помогите пожалуйста целый день сижу. Заранее огромное спасибо.18 февраля 2011 г. 18:45
-
Вот код, для контрола:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace CustomControl { public enum RGBSelection { R,G,B }; public partial class MyCustomControl : UserControl { //the controls private System.ComponentModel.IContainer components = null; private System.Windows.Forms.Label labelLabel; private System.Windows.Forms.NumericUpDown numericUpDownValue; private System.Windows.Forms.TrackBar trackBarValue; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.labelLabel = new System.Windows.Forms.Label(); this.numericUpDownValue = new System.Windows.Forms.NumericUpDown(); this.trackBarValue = new System.Windows.Forms.TrackBar(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDownValue)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.trackBarValue)).BeginInit(); this.SuspendLayout(); // labelLabel this.labelLabel.AutoSize = true; this.labelLabel.Location = new System.Drawing.Point(4, 4); this.labelLabel.Name = "labelLabel"; this.labelLabel.Size = new System.Drawing.Size(71, 13); this.labelLabel.TabIndex = 0; this.labelLabel.Text = "Dummy Label"; // numericUpDownValue this.numericUpDownValue.Location = new System.Drawing.Point(175, 20); this.numericUpDownValue.Name = "numericUpDownValue"; this.numericUpDownValue.Size = new System.Drawing.Size(41, 20); this.numericUpDownValue.TabIndex = 1; // trackBarValue this.trackBarValue.Location = new System.Drawing.Point(7, 20); this.trackBarValue.Name = "trackBarValue"; this.trackBarValue.Size = new System.Drawing.Size(162, 45); this.trackBarValue.TabIndex = 2; // MyCustomControl this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.trackBarValue); this.Controls.Add(this.numericUpDownValue); this.Controls.Add(this.labelLabel); this.Name = "MyCustomControl"; this.Size = new System.Drawing.Size(216, 65); ((System.ComponentModel.ISupportInitialize)(this.numericUpDownValue)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.trackBarValue)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } public RGBSelection ColorPart {get;set;} public string Label { get { return labelLabel.Text; } set { labelLabel.Text = value; } } public int Value { get { return (int)numericUpDownValue.Value; } set { numericUpDownValue.Value = (int)value; } } public event EventHandler<EventArgs> ValueChanged; public MyCustomControl() { InitializeComponent(); numericUpDownValue.Minimum = 0; numericUpDownValue.Maximum = 255; trackBarValue.Minimum = 0; trackBarValue.Maximum = 255; trackBarValue.TickFrequency = 10; numericUpDownValue.ValueChanged += numericUpDownValue_ValueChanged; trackBarValue.ValueChanged += trackBarValue_ValueChanged; } void trackBarValue_ValueChanged(object sender, EventArgs e) { if (sender != this) { numericUpDownValue.Value = trackBarValue.Value; OnValueChanged(); } } void numericUpDownValue_ValueChanged(object sender, EventArgs e) { if (sender != this) { trackBarValue.Value = (int)numericUpDownValue.Value; OnValueChanged(); } } protected void OnValueChanged() { Refresh(); if (ValueChanged != null) { ValueChanged(this, EventArgs.Empty); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle rect = new Rectangle(numericUpDownValue.Left, 5, numericUpDownValue.Width, numericUpDownValue.Bounds.Top - 10); int r = 0, g = 0, b = 0; switch (ColorPart) { case RGBSelection.R: r = Value; break; case RGBSelection.G: g = Value; break; case RGBSelection.B: b = Value; break; } Color c = Color.FromArgb(r,g,b); using (Brush brush = new SolidBrush(c)) { e.Graphics.FillEllipse(brush, rect); } } } }
Когда я его написал компилятор выдал ошибки:
Error 1 Ambiguity between 'CustomControl.MyCustomControl.components' and 'CustomControl.MyCustomControl.components' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 24 31 CustomControl
Error 2 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 33 18 CustomControl
Error 3 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 34 18 CustomControl
Error 4 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 35 18 CustomControl
Error 5 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 36 62 CustomControl
Error 6 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 37 62 CustomControl
Error 7 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 40 18 CustomControl
Error 8 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 41 18 CustomControl
Error 9 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 42 18 CustomControl
Error 10 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 43 18 CustomControl
Error 11 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 44 18 CustomControl
Error 12 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 45 18 CustomControl
Error 13 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 47 18 CustomControl
Error 14 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 48 18 CustomControl
Error 15 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 49 18 CustomControl
Error 16 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 50 18 CustomControl
Error 17 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 52 18 CustomControl
Error 18 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 53 18 CustomControl
Error 19 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 54 18 CustomControl
Error 20 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 55 18 CustomControl
Error 21 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 59 36 CustomControl
Error 22 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 60 36 CustomControl
Error 23 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 61 36 CustomControl
Error 24 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 64 62 CustomControl
Error 25 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 65 62 CustomControl
Error 26 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 76 24 CustomControl
Error 27 Ambiguity between 'CustomControl.MyCustomControl.labelLabel' and 'CustomControl.MyCustomControl.labelLabel' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 80 17 CustomControl
Error 28 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 88 29 CustomControl
Error 29 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 92 17 CustomControl
Error 30 The call is ambiguous between the following methods or properties: 'CustomControl.MyCustomControl.InitializeComponent()' and 'CustomControl.MyCustomControl.InitializeComponent()' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 100 13 CustomControl
Error 31 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 102 13 CustomControl
Error 32 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 104 13 CustomControl
Error 33 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 117 17 CustomControl
Error 34 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 117 44 CustomControl
Error 35 Ambiguity between 'CustomControl.MyCustomControl.trackBarValue' and 'CustomControl.MyCustomControl.trackBarValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 127 17 CustomControl
Error 36 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 127 44 CustomControl
Error 37 Ambiguity between 'CustomControl.MyCustomControl.numericUpDownValue' and 'CustomControl.MyCustomControl.numericUpDownValue' C:\Users\Администратор\Documents\Visual Studio 2010\Projects\C#\CustomControl\CustomControl\MyCustomControl.cs 146 44 CustomControlВот код для формы на которой находятся ползунки для выбора цвета:
Какие еще нужны свойства для контрола, помогите целый день сижу ни как с мертвый точки не сдвинусь.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 CustomControl { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OnValuesChanged(object sender, EventArgs e) { int r = redControl.Value; int g = greenControl.Value; int b = blueControl.Value; colorControl.BackColor = Color.FromArgb(r, g, b); } } }
18 февраля 2011 г. 20:17 -
Я же вам дал ссылки на то, как делать свойства, чтобы они отображались в дизайнере. Мало сделать просто свойсто, ему нужно добавить специальные атрибуты, которые определяют свойство в дизайнере. Минимально нужно добавить атрибут [Browsable(true)].
[Browsable(true), DisplayName("Label Text")] public string Label { get { return labelLabel.Text; } set { labelLabel.Text = value; } } [Browsable(true)] public RGBSelection ColorPart { get; set; }
Для связи [mail]18 февраля 2011 г. 21:17 -
Я что не совсем понимаю можно пожалуйста по по подробней куда именно это нужно добавить?????18 февраля 2011 г. 22:06
-
Спасбо за советы, я сделал подругому у меня все работает
- Помечено в качестве ответа Наум Фишман 19 февраля 2011 г. 9:47
19 февраля 2011 г. 9:46 -
Это были свойства UserControl-а.
Для связи [mail]19 февраля 2011 г. 13:13 -
Как вы сделали по-другому?
Для связи [mail]19 февраля 2011 г. 13:13