ObjectListView refactoring

This commit is contained in:
Jaex 2019-12-03 23:56:26 +03:00
parent 1b00fb0538
commit d61123b8c4
4 changed files with 24 additions and 19 deletions

View file

@ -28,19 +28,22 @@ protected override void Dispose(bool disposing)
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HistoryItemInfoForm));
this.olvMain = new ShareX.HistoryLib.ObjectListView();
this.SuspendLayout();
//
// olvMain
//
this.olvMain.AutoFillColumn = true;
this.olvMain.BorderStyle = System.Windows.Forms.BorderStyle.None;
resources.ApplyResources(this.olvMain, "olvMain");
this.olvMain.FullRowSelect = true;
this.olvMain.GridLines = true;
this.olvMain.HideSelection = false;
this.olvMain.MultiSelect = false;
this.olvMain.Name = "olvMain";
this.olvMain.SetObjectType = ShareX.HistoryLib.ObjectListView.ObjectType.Properties;
this.olvMain.SelectedObject = null;
this.olvMain.UseCompatibleStateImageBehavior = false;
this.olvMain.View = System.Windows.Forms.View.Details;
//
@ -49,8 +52,6 @@ private void InitializeComponent()
resources.ApplyResources(this, "$this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.olvMain);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.MaximizeBox = false;
this.Name = "HistoryItemInfoForm";
this.ResumeLayout(false);

View file

@ -35,7 +35,7 @@ public HistoryItemInfoForm(object hi)
InitializeComponent();
ShareXResources.ApplyTheme(this);
olvMain.SelectObject(hi);
olvMain.SelectedObject = hi;
}
}
}

View file

@ -136,7 +136,7 @@
<value>olvMain</value>
</data>
<data name="&gt;&gt;olvMain.Type" xml:space="preserve">
<value>ShareX.HistoryLib.CustomControls.ObjectListView, ShareX.HistoryLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</value>
<value>ShareX.HistoryLib.ObjectListView, ShareX.HistoryLib, Version=13.0.2.0, Culture=neutral, PublicKeyToken=null</value>
</data>
<data name="&gt;&gt;olvMain.Parent" xml:space="preserve">
<value>$this</value>

View file

@ -26,6 +26,7 @@
using ShareX.HelpersLib;
using ShareX.HistoryLib.Properties;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
@ -39,13 +40,22 @@ public enum ObjectType
Properties
}
[DefaultValue(ObjectType.Properties)]
public ObjectType SetObjectType { get; set; }
private object selectedObject;
public object SelectedObject
{
get
{
return selectedObject;
}
set
{
SelectObject(value);
selectedObject = value;
SelectObject(selectedObject);
}
}
@ -55,6 +65,7 @@ public ObjectListView()
MultiSelect = false;
Columns.Add(Resources.ObjectListView_ObjectListView_Name, 125);
Columns.Add(Resources.ObjectListView_ObjectListView_Value, 300);
ContextMenuStrip cms = new ContextMenuStrip();
cms.ShowImageMargin = false;
cms.Items.Add(Resources.ObjectListView_ObjectListView_Copy_name).Click += PropertyListView_Click_Name;
@ -77,7 +88,7 @@ private void PropertyListView_Click_Name(object sender, EventArgs e)
private void PropertyListView_Click_Value(object sender, EventArgs e)
{
if (SelectedItems.Count > 0)
if (SelectedItems.Count > 0 && SelectedItems[0].SubItems.Count > 1)
{
string text = SelectedItems[0].SubItems[1].Text;
@ -88,7 +99,7 @@ private void PropertyListView_Click_Value(object sender, EventArgs e)
}
}
public void SelectObject(object obj)
private void SelectObject(object obj)
{
Items.Clear();
@ -98,9 +109,9 @@ public void SelectObject(object obj)
if (SetObjectType == ObjectType.Fields)
{
foreach (FieldInfo property in type.GetFields())
foreach (FieldInfo field in type.GetFields())
{
AddObject(property.GetValue(obj), property.Name);
AddObject(field.GetValue(obj), field.Name);
}
}
else if (SetObjectType == ObjectType.Properties)
@ -110,23 +121,16 @@ public void SelectObject(object obj)
AddObject(property.GetValue(obj, null), property.Name);
}
}
FillLastColumn();
}
}
private void AddObject(object obj, string name)
{
if (obj is HistoryItem)
{
SelectObject(obj);
return;
}
ListViewItem lvi = new ListViewItem(name);
lvi.Tag = obj;
if (obj != null)
{
lvi.Tag = obj;
lvi.SubItems.Add(obj.ToString());
}