0

Okay so basically I have a calendar display and when you click on anyone of the dates on it, it creates a new panel with a label displaying the date selected. I also made it so when you click on a date and a new panel is made, a label, textbox and button is created and placed onto that new panel as well.

So what I want and have been struggling with is for me to enter something into that textbox then to press the button to submit it and then for it to show on the label.

I think I know what the issue is but I've been stuck at this for hours.

error error_2

Here is my code:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void monthCalendar1_DateSelected_1(object sender, DateRangeEventArgs e)
    {
        Panel newPanel = new Panel();
        this.Controls.Add(newPanel);
        newPanel.Visible = true;
        newPanel.Size = new Size(564, 831);
        newPanel.Location = new Point(0, 190);
        newPanel.BringToFront();

        Label textLabel = new Label();
        textLabel.Size = new Size(500, 500);
        textLabel.Font = new Font(textLabel.Font.Name, 25, textLabel.Font.Style);
        textLabel.Location = new Point(3, 3);

        Label dateLabel = new Label();
        dateLabel.Size = new Size(500, 500);
        dateLabel.Font = new Font(dateLabel.Font.Name, 25, dateLabel.Font.Style);
        dateLabel.Location = new Point(128, 3);

        Button Submitbutton = new Button();
        Submitbutton.Location = new Point(100, 500);
        Submitbutton.Text = "Add Food";
        Submitbutton.Size = new Size(400, 100);
        Submitbutton.BackColor = Color.Aqua;
        Submitbutton.BringToFront();
        Submitbutton.Click += Button_Click;


        TextBox textBox = new TextBox();
        textBox.Location = new Point(100, 650);
        textBox.Size = new Size(500, 500);
        textBox.BackColor = Color.Aqua;
        textBox.Visible = true;
        textBox.Text = "Enter food here...";
        textBox.BringToFront();

        Label inputtedFood = new Label();
        inputtedFood.Size = new Size(500, 500);
        inputtedFood.Font = new Font(inputtedFood.Font.Name, 25, inputtedFood.Font.Style);
        inputtedFood.Location = new Point(100, 600);
        inputtedFood.Text = "placeholder";

        newPanel.Controls.Add(dateLabel);
        newPanel.Controls.Add(textLabel);
        newPanel.Controls.Add(Submitbutton);
        newPanel.Controls.Add(textBox);
        newPanel.Controls.Add(inputtedFood);
        
        
        
        String myCalendar = monthCalendar1.SelectionRange.Start.ToShortDateString();

        textLabel.Text = "Date:";
        dateLabel.Text = myCalendar;
    }


    private void Button_Click(object sender, EventArgs e)
    {
        inputtedFood.Text = textBox.Text;
    }
    
    private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }
}

I tried the above code and was met with errors that are shown in the post.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • 2
    The reference to those controls only exist in the DateSelected code block. Instead of a panel and controls, that should probably be a UserControl. Otherwise, you will have to do a search for those controls by giving them a name and looping through the Controls collections to find them. – LarsTech Feb 03 '23 at 22:31
  • 2
    It might be beneficial to take a moment to learn how to construct and use `UserControls` – Ňɏssa Pøngjǣrdenlarp Feb 03 '23 at 22:35
  • @LarsTech So how can I reference the Label inputtedFood and the TextBox textBox to the Button_Click event? – Brandon Miller Feb 04 '23 at 23:11
  • See [Find control by name from Windows Forms controls](https://stackoverflow.com/q/3898588/719186) – LarsTech Feb 05 '23 at 15:38

1 Answers1

2

Totally agree with both LarsTech and Ňɏssa Pøngjǣrdenlarp, you should be building a UserControl in place of the Panel and placing the TextBox, Button, and Label inside of that.

Your immediate question, though:

So what I want and have been struggling with is for me to enter something into that textbox then to press the button to submit it and then for it to show on the label.

Can be accomplished with this simple code:

Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
    inputtedFood.Text = textBox.Text;
};

Here's a little example showing it in action:

private void button1_Click(object sender, EventArgs e)
{
    FlowLayoutPanel flp = new FlowLayoutPanel();

    TextBox textBox = new TextBox();
    // ... more code ...

    Label inputtedFood = new Label();
    inputtedFood.Text = "placeholder";
    // ... more code ...

    Button Submitbutton = new Button();
    // ... more code ...
    Submitbutton.Click += (s2, e2) =>
    {
        inputtedFood.Text = textBox.Text;
    };

    flp.Controls.Add(textBox);
    flp.Controls.Add(Submitbutton);
    flp.Controls.Add(inputtedFood);
    flowLayoutPanel1.Controls.Add(flp);
}

The output:

enter image description here

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Hi thanks for the detailed response, but how can I reference inputtedFood label and textBox TextBox to the Button_Click event so it will work? – Brandon Miller Feb 04 '23 at 23:14
  • 1
    Exactly as I have shown. Because the dynamic handler is wired up in the same scope/block as the Label and TextBox, the event handler will be able to reference those controls. You can clearly see it working in my example. – Idle_Mind Feb 05 '23 at 00:27
  • Yeah you even showed a little video playing as well! Thanks for the help, I've marked your response as the answer! – Brandon Miller Feb 11 '23 at 01:36
  • So just tried your code and worked as expected... but new issue! Woo! When I click on a new date and go back to the other date where I inputted something it resets it... How can I make it save the contents I put into the textbox and then the label it prints onto? – Brandon Miller Feb 11 '23 at 01:43
  • 1
    You'd have to show/update your code above so we can troubleshoot. It'd be even better if you can show us a video of it, too. – Idle_Mind Feb 11 '23 at 02:10
  • Here is a video of my code and it running - https://mega.nz/file/H1Jh2BoI#sCsqDiMa3B45ItBH3al600zCCsNsiDc_rdHbCxt31qA – Brandon Miller Feb 16 '23 at 00:28
  • So what's happening is every time you select a new date in monthCalendar1 a NEW PANEL is being created. The previous one is actually still there, but BEHIND the new one(s). *You never remove them so they will just stack on top of each other. What you could do is to use a [Dictionary](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-7.0) to associate an already created panel with a date. Then if it exists already, you can simply bring it to the front instead of creating another panel. So you'd use a `Dictionary` to do this. – Idle_Mind Feb 16 '23 at 01:15
  • Your "myPanel" String would be the KEY, and "newPanel" would be the VALUE. – Idle_Mind Feb 16 '23 at 01:15
  • Also, it looks like your Panel() Paint events are EXACTLY THE SAME except for the name of the panel. You can make of the Paint events for those panels use the same method. Just cast the `sender` parameter to the source control: `Panel pnl = (Panel)sender;` Then you can use `pnl` where ever you had a specific panel name before. – Idle_Mind Feb 16 '23 at 01:16
  • Even better, you could create a new class that DERIVES from Panel and put that code in there. Then you'd drag it from your ToolBox and wouldn't need that code in your Form. – Idle_Mind Feb 16 '23 at 01:20