Click here to Skip to main content
15,885,985 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to manage dinamically generated user controls? Pin
OriginalGriff12-Feb-18 0:37
mveOriginalGriff12-Feb-18 0:37 
GeneralRe: How to manage dinamically generated user controls? Pin
BillWoodruff12-Feb-18 2:00
professionalBillWoodruff12-Feb-18 2:00 
GeneralRe: How to manage dinamically generated user controls? Pin
OriginalGriff12-Feb-18 2:07
mveOriginalGriff12-Feb-18 2:07 
GeneralRe: How to manage dinamically generated user controls? Pin
BillWoodruff12-Feb-18 5:44
professionalBillWoodruff12-Feb-18 5:44 
AnswerRe: How to manage dinamically generated user controls? Pin
BillWoodruff11-Feb-18 21:40
professionalBillWoodruff11-Feb-18 21:40 
GeneralRe: How to manage dinamically generated user controls? Pin
alin112-Feb-18 2:53
alin112-Feb-18 2:53 
GeneralRe: How to manage dinamically generated user controls? Pin
BillWoodruff12-Feb-18 4:39
professionalBillWoodruff12-Feb-18 4:39 
GeneralRe: How to manage dinamically generated user controls? Pin
alin112-Feb-18 7:15
alin112-Feb-18 7:15 
Thanks again. This is what I did so far:

Item.cs
using System.Windows.Forms;

namespace ToDo
{
    public partial class Item : UserControl
    {
        public Panel ItemPanel { set; get; }
        public CheckBox DoneCheckBox { set; get; }
        public Button DeleteButton { set; get; }
        public TextBox UserTextBox { set; get; }

        public Item()
        {
            InitializeComponent();

            ItemPanel = itemPanel;
            DoneCheckBox = doneCheckBox;
            DeleteButton = deleteButton;
            UserTextBox = userTextBox;
        }
    }
}


ItemContainer.cs
List<Item> itemList = new List<Item>();
Item crtItem;


void AddItem()
{
    if (inputTextBox.Text != "")
    {
        Item item = new Item();
        itemList.Add(item);
        crtItem = item;
        item.Dock = DockStyle.Top;
        item.UserTextBox.Text = inputTextBox.Text;
        item.Enter += Item_Enter;
        item.Leave += Item_Leave;
        item.DoneCheckBox.CheckedChanged += DoneCheckBox_CheckedChanged;
        item.DeleteButton.Click += DeleteButton_Click;
        itemPanel.Controls.Add(item);
        item.BringToFront();

        inputTextBox.Text = "";
        itemsLabel.Text = itemList.Count + " item(s)";
    }

    inputTextBox.Focus();
}


private void Item_Enter(object sender, EventArgs e)
{
    crtItem.ItemPanel.BackColor = Color.FromArgb(200, 233, 253);
    crtItem.UserTextBox.BackColor = Color.FromArgb(200, 233, 253);
}

private void Item_Leave(object sender, EventArgs e)
{
    crtItem.ItemPanel.BackColor = Color.FromArgb(240, 240, 240);
    crtItem.UserTextBox.BackColor = Color.FromArgb(240, 240, 240);
}

private void DoneCheckBox_CheckedChanged(object sender, EventArgs e)
{
    if (crtItem.DoneCheckBox.Checked)
    {
        crtItem.UserTextBox.ForeColor = Color.FromArgb(150, 150, 150);
        crtItem.UserTextBox.Font = new Font("Segoe UI", 18F, FontStyle.Strikeout, GraphicsUnit.Point, 0);
    }
    else
    {
        crtItem.UserTextBox.ForeColor = Color.FromArgb(0, 0, 0);
        crtItem.UserTextBox.Font = new Font("Segoe UI", 18F, FontStyle.Regular, GraphicsUnit.Point, 0);
    }
}

private void DeleteButton_Click(object sender, EventArgs e)
{
    itemList.Remove(crtItem);
    itemPanel.Controls.Remove(crtItem);
    crtItem.Dispose();

    Invalidate();
}


The problem is with this crtItem (in your example: currentToDo). How do I update its value when I click a random Item usercontrol? Here's the VC solution: download[^] Feel free to modify it if you want.
GeneralRe: How to manage dinamically generated user controls? Pin
BillWoodruff12-Feb-18 7:52
professionalBillWoodruff12-Feb-18 7:52 
GeneralRe: How to manage dinamically generated user controls? Pin
alin112-Feb-18 8:01
alin112-Feb-18 8:01 
GeneralRe: How to manage dinamically generated user controls? Pin
BillWoodruff12-Feb-18 9:13
professionalBillWoodruff12-Feb-18 9:13 
GeneralRe: How to manage dinamically generated user controls? Pin
alin112-Feb-18 9:27
alin112-Feb-18 9:27 
GeneralRe: How to manage dinamically generated user controls? Pin
BillWoodruff12-Feb-18 17:54
professionalBillWoodruff12-Feb-18 17:54 
Questionsevenzipsharp password Pin
Member 1367023411-Feb-18 10:15
Member 1367023411-Feb-18 10:15 
AnswerRe: sevenzipsharp password Pin
Dave Kreskowiak11-Feb-18 11:01
mveDave Kreskowiak11-Feb-18 11:01 
GeneralRe: sevenzipsharp password Pin
BillWoodruff11-Feb-18 23:10
professionalBillWoodruff11-Feb-18 23:10 
GeneralRe: sevenzipsharp password Pin
Dave Kreskowiak12-Feb-18 2:30
mveDave Kreskowiak12-Feb-18 2:30 
AnswerRe: sevenzipsharp password Pin
BillWoodruff11-Feb-18 23:11
professionalBillWoodruff11-Feb-18 23:11 
SuggestionMessage Removed Pin
10-Feb-18 9:12
Markhoernchen10-Feb-18 9:12 
GeneralMessage Removed Pin
10-Feb-18 9:59
mveRichard MacCutchan10-Feb-18 9:59 
AnswerMessage Removed Pin
10-Feb-18 11:28
Markhoernchen10-Feb-18 11:28 
QuestionError with Primary key in Sql database Pin
Member 1367168610-Feb-18 7:32
Member 1367168610-Feb-18 7:32 
AnswerRe: Error with Primary key in Sql database Pin
Wendelius10-Feb-18 8:27
mentorWendelius10-Feb-18 8:27 
QuestionSplit a String of numbers in textfield Pin
auting829-Feb-18 11:06
auting829-Feb-18 11:06 
AnswerRe: Split a String of numbers in textfield Pin
Luc Pattyn9-Feb-18 16:22
sitebuilderLuc Pattyn9-Feb-18 16:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.