Create custom User Control Items - c#

I want to make my own DropDown (Combobox) with visual Items, which contain a Picture, a Name and a Comment. The problem is if I add an Item in the properties everything is okay, visual studio adds the code in the form designer, but the item isn't displayed.
The Item Collection 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;
using System.Collections;
namespace List_Item_Test
{
public partial class My_Item : CollectionBase
{
public Items this[int Index]
{
get
{
return (Items)List[Index];
}
}
public bool Contains(Items itemType)
{
return List.Contains(itemType);
}
public int Add(Items itemType)
{
//i think hier something is missing???
return List.Add(itemType);
}
public void Remove(Items itemType)
{
List.Remove(itemType);
}
public void Insert(int index, Items itemType)
{
List.Insert(index, itemType);
}
public int IndexOf(Items itemType)
{
return List.IndexOf(itemType);
}
}
}
The Item Container 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 List_Item_Test
{
public partial class Item_Container : UserControl
{
public Item_Container()
{
InitializeComponent();
}
My_Item hallo = new My_Item();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public My_Item MyItemTypes
{
get { return hallo; }
set
{
Items hallo1 = new Items();
hallo1.SetBounds(0, 10 + /*hallo.Count - 1 **/ 50, Width, 50);
this.Controls.Add(hallo1);
}
}
}
}

Related

How to send a list from a form to another one

I have two forms in a Windows Forms project: Form1 and aCurso.
I'm trying to send a List with objects of a class called curso (I mean: List<curso>) from Form1 to aCurso.
But Visual Studio show this:
Accessibility inconsistency: The type of parameter List<curso> is less accessible than the method aCurso.aCurso(List<curso>).
So, here is the code from Form1:
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 _18_05_18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<curso> cursos = new List<curso>();
private void btnAC_Click(object sender, EventArgs e)
{
Form f = new aCurso(cursos);
f.Show();
}
}
}
Here's code from aCurso:
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 _18_05_18
{
public partial class aCurso : Form
{
List<curso> cursos = new List<curso>();
public aCurso(List<curso> cursos)
{
InitializeComponent();
this.cursos = cursos;
}
}
}
Here's code from class curso:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18_05_18
{
class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}
}
You cannot expose a public method signature where some of the parameter types of the signature are not public. It wouldn't be possible to call the method from outside since the caller couldn't construct the parameters required.
All you have to do is make curso class public
public class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}

C# - Tier Separation - How to use these delegates?

Here's the relevant code:
ClickMeGame.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public class ClickMeGame
{
public OnClickMe onClickMeCallback;
public int score;
public ClickMeGame()
{
score = 0;
}
private void IncrementScore()
{
score++;
}
}
}
ClickMeCallBackDefinitions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public delegate void OnClickMe();
}
MainWindow.cs (Windows 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;
using ClassLibrary;
namespace ClickMe
{
public partial class mainWindow : Form
{
private ClickMeGame game;
public mainWindow()
{
InitializeComponent();
game = new ClickMeGame();
game.onClickMeCallback = clickMeButton_Click();
}
private void clickMeButton_Click(object sender, EventArgs e)
{
UpdateUI();
}
private void UpdateUI()
{
scoreLabel.Text = string.Format("The score is: {0}", game.score);
}
}
}
So what I'm trying to do is, when the user clicks a button present on the form, I want a label on the form to update with the game score which increments with every click.
I'm learning about/want to be able to do this with delegates in that I want to separate the project into 2 tiers; Presenation and Logic. I know it's unnecessary to do so, but I'd like to make it such that when you click the button, the Windows Form receives information about the game score via delegates/callback methods. I'm unsure how to do this, but I tried making the callback definition and referencing it, but I'm lost from there.
Assuming that the UI button uses the click event clickMeButton_Click then here you go.
public partial class mainWindow : Form
{
private ClickMeGame game;
public mainWindow()
{
InitializeComponent();
game = new ClickMeGame();
game.onClickMeCallback = param => UpdateUI();
}
private void clickMeButton_Click(object sender, EventArgs e)
{
game.onClickMeCallback.Invoke();
}
private void UpdateUI()
{
scoreLabel.Text = string.Format("The score is: {0}", game.score);
}
}

Cannot set ImageIndex of own TreeNode inside constructor

I got two Classes One is derived from TreeView and the other from TreeNode:
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;
using System.IO;
using System.Resources;
using ExtendedTreeView.Properties;
namespace ExtendedTreeView
{
public partial class ExtendedTreeView : System.Windows.Forms.TreeView
{
public ExtendedTreeView()
{
InitializeComponent();
ImageList il = new ImageList();
il.Images.Add("blank", Resources.favicon);
il.Images.Add("application", Resources.application);
il.Images.Add("computer", Resources.computer);
il.Images.Add("network", Resources.network);
il.Images.Add("session", Resources.session);
il.Images.Add("user", Resources.user);
il.Images.Add("usergroup", Resources.usergroup);
this.ImageList = il;
}
}
}
and:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.NetworkInformation;
using System.Management;
using System.Windows.Forms;
using Cassia;
using System.Security.Principal;
namespace GUI
{
public class Computer : System.Windows.Forms.TreeNode, IDictionaryEnumerator
{
private DictionaryEntry nodeEntry;
private IEnumerator enumerator;
public String computerName { get; private set; }
public Computer(String computername)
{
this.computerName = computername;
enumerator = base.Nodes.GetEnumerator();
this.Text = this.ipAdressen[0].ToString() + " - " + this.computerName;
this.ImageIndex = 2;
}
#region TreeNodeStuff
public string NodeKey
{
get
{
return nodeEntry.Key.ToString();
}
set
{
nodeEntry.Key = value;
}
}
public object NodeValue
{
get
{
return nodeEntry.Value;
}
set
{
nodeEntry.Value = value;
}
}
public DictionaryEntry Entry
{
get
{
return nodeEntry;
}
}
public bool MoveNext()
{
bool Success;
Success = enumerator.MoveNext();
return Success;
}
public object Current
{
get
{
return enumerator.Current;
}
}
public object Key
{
get
{
return nodeEntry.Key;
}
}
public object Value
{
get
{
return nodeEntry.Value;
}
}
public void Reset()
{
enumerator.Reset();
}
#endregion
}
}
But the TreeNode-Icons are stuck at Index 0. So my problem is, that im not able to set the ImageIndex-Property of the Computer TreeNode.
What am I doing wrong?

singleton implementation problem in C#

--ConsoleApplication 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class MsgService
{
private static CreateConnectionToA _instanceA;
private static CreateConnectionToB _instanceB;
protected MsgService()
{
}
public static MsgService GetInstanceA(string paramA, string paramB)
{
if (_instanceA != null)
{
return _instanceA;
}
return _instanceA = new CreateConnectionToA("p1","p2");
}
public static MsgService GetInstanceB(string paramA, string paramB)
{
if (_instanceB != null)
{
return _instanceB;
}
return _instanceB = new CreateConnectionToB("p1", "p2");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class CreateConnectionToB : MsgService
{
public CreateConnectionToB(string param1, string Param2)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class CreateConnectionToA : MsgService
{
public CreateConnectionToA(string param1, string Param2)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MsgService.GetInstanceA("p1", "p2");
Console.Read();
}
}
}
--ConsoleApplication 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press");
Console.Read();
ConsoleApplication2.MsgService.GetInstanceA("p1", "p2");
Console.Read();
}
}
}
I am trying to Make simgleton implementation but something is wrong with my approach. It always creates new instance of _instanceA and _instanceB from each console application.
Can someone please point me out what needs to be done here?
You would need named Mutexes for inter-process synchronization.
Sharing an object instance between two applications is kinda hard, since they run in separate appdomains, by default. To accomplish what I think you're trying to do, you'll need to either
marshal across appdomain boundaries with, or
run the two processes in a shared appdomain. Write a 3rd process — a shell — that's responsible for spawning/hosting the other two processes in a shared appdomain.
http://www.codeproject.com/KB/dotnet/AppDomainMemImprovement.aspx
Sharing data between AppDomains

Where would the responsibility to handle this event be placed?

I have a NavigationBar.cs user control.
I also have NavigationItem.cs user control.
Here's the code for both:
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 Uboldi.CustomUI
{
public partial class NavigationBar : UserControl
{
public NavigationBar()
{
InitializeComponent();
}
public List<NavigationItem> NavigationItems { private get; set; }
public NavigationItem SelectedItem { get; set; }
}
}
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 Uboldi.CustomUI
{
public partial class NavigationItem : UserControl
{
public NavigationItem()
{
InitializeComponent();
}
private Image _picture = null;
public Image Picture
{
get
{
return _picture;
}
set
{
_picture = value;
ptbIcon.Image = _picture;
}
}
private string _content = null;
public string Content
{
get
{
return _content;
}
set
{
_content = value;
lblDisplayText.Text = _content;
}
}
}
}
I only want a single NavigationItem in the navigationbar to be 'selected' at any given time.
When an item is selected a different color will be given to it.
My question is, where should I program this code? In the bar, or is it something a button should do and have the bar just invoke that SetYourSelfAsSelected() method?
Thanks.
I would implement this functionality in the NavigationBar for the following reason: Some other developer could use your NavigationItems in a different context where no active items are marked or marked any different. Or one wants to overload the NavigationBar to implement another behaviour for active items.

Categories