두 개의 사용자 정의 컨트롤이 있는데 하나는 확인란이있는 간단한 그림 홀더입니다. 그리고 이전 컨트롤의 컬렉션을 가진 것보다 컨테이너 역할을하는 또 하나. 사용자 정의 컨트롤을 디자인 뷰로 끌면 Visual Studio에서 오류가 발생합니다.
은 그래서 HorizontalPictureScroller 많은 SelectablePicture 컨트롤을 할 수 있습니다. 나는 각 컨트롤에 대한 작은 코드를 붙여 것이다 :먼저 HorizontalPictureScroller : 이제
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 System.Collections.ObjectModel;
namespace WinformsPlayground
{
[Serializable()]
public partial class HorizontalPictureScroller : UserControl
{
public HorizontalPictureScroller()
{
InitializeComponent();
Pictures = new ObservableCollection<SelectablePicture>();
Pictures.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Pictures_CollectionChanged);
}
#region "Properties"
public ObservableCollection<SelectablePicture> Pictures { get; set; }
private int PositionControlX = 0;
#endregion
#region "Methods"
private void RedrawPictures()
{
PositionControlX = 0;
foreach (var picture in Pictures)
{
picture.Location = new Point(PositionControlX + panelPicturesWrapper.AutoScrollPosition.X, 0);
PositionControlX += 130;
panelPicturesWrapper.Controls.Add(picture);
}
}
public void AddPicture(SelectablePicture picture)
{
Pictures.Add(picture);
}
public void RemovePicture(SelectablePicture picture)
{
Pictures.Remove(picture);
}
public void MovePictureLeft(int index)
{
SelectablePicture tmpPicture = Pictures[index];
Pictures[index] = Pictures[index - 1];
Pictures[index - 1] = tmpPicture;
}
public void MovePictureRight(int index)
{
SelectablePicture tmpPicture = Pictures[index];
Pictures[index] = Pictures[index + 1];
Pictures[index + 1] = tmpPicture;
}
#endregion
#region "Events"
void Pictures_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
RedrawPictures();
}
#endregion
}
}
의 SelectablePicture 제어 : 여기
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;
namespace WinformsPlayground
{
[Serializable()]
public partial class SelectablePicture : UserControl
{
public SelectablePicture()
{
InitializeComponent();
panel1.BackgroundImageLayout = ImageLayout.Zoom;
}
public SelectablePicture(Image image)
{
panel1.BackgroundImage = image;
panel1.BackgroundImageLayout = ImageLayout.Zoom;
}
#region "Properties"
public Image Image()
{
return panel1.BackgroundImage;
}
public bool IsSelected()
{
return chkSelected.Checked;
}
#endregion
#region "Methods"
public void ToggleCheckBox()
{
chkSelected.Checked = chkSelected.Checked ? false : true;
}
public void VisuallySelect()
{
this.BackColor = Color.FromArgb(51, 153, 255);
}
public void VisuallyDeselect()
{
//If none of the controls inside the usercontrol have focus, set this control to white.
if (!this.Focused && !this.panel1.Focused && !this.chkSelected.Focused)
{
this.BackColor = Color.White;
}
}
#endregion
#region "Events"
private void panel1_Click(object sender, EventArgs e)
{
VisuallySelect();
ToggleCheckBox();
panel1.Focus();
}
private void chkSelected_Click(object sender, EventArgs e)
{
VisuallySelect();
ToggleCheckBox();
chkSelected.Focus();
}
private void SelectablePicture_Click(object sender, EventArgs e)
{
VisuallySelect();
ToggleCheckBox();
this.Focus();
}
private void panel1_Leave(object sender, EventArgs e)
{
VisuallyDeselect();
}
private void chkSelected_Leave(object sender, EventArgs e)
{
VisuallyDeselect();
}
private void SelectablePicture_Leave(object sender, EventArgs e)
{
VisuallyDeselect();
}
#endregion
}
}
은의 스크린 샷입니다 HorizontalPictureScroller를 Winform 디자인보기로 드래그하려고 할 때 오류가 발생합니다 (미안 텍스트를 여기에 붙여 넣을 수 없습니다) :
내 사용자 컨트롤은 매우 간단하며 코드에서 어떤 문제가 있는지 알 수 없습니다.
어쩌면 그것은 내 맹신적 인 실수 일 수 있습니다. : P 시간 내 주셔서 감사합니다.
[Serializable()]의 제목을 제거하더라도 여전히 오류가 발생합니다. 실제로 누군가가 오류의 원인이라고 말했기 때문에 나는 그것을 추가했습니다 : P 다른 아이디어? 감사! –
@Serg,'[Serializable()]'을 제거하고 각 컨트롤을 개별적으로 추가 할 때 둘 다 예외가 발생합니까? 'HorizontalPictureScroller' 만 예외를 던지면 문제는 'Serializable'로 표시되어 사용하는 ObservableCollection 일 수 있습니다. –
@adrift : 예! HorizontalPictureScroller 만 디자이너 뷰로 끌면 예외가 발생합니다. 이 문제를 해결하는 방법에 대한 제안 사항이 있으십니까? –