Click here to Skip to main content
14,972,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am creating WPF app in C# that creates a mathematical tasks for kids(addition, subtraction etc.). In one User Controller I have a button "Begin", which loads another UC and in that UC I have 10 tasks, beside every task I have Text Box for user to input answer and I have button "Confirm" (all of this is dynamically created), to be able to make a list of user answers when the button is clicked. But now, I have a problem with empty Text Boxes.
I want, when the button "Confirm" is clicked, to check if there are an empty Text Box-es, and if there is/are, I want Label/s to appear with some text beside that empty box/es. As soon as user start typing in that or those particular text box(es) I want that label to dissapear.

What I have tried:

This is where I create tasks and Text Boxes:

public void CreateTasksVisually(UCFirstLevelAddition userControl, List<string> additionTasksList)
        {
            int x = 350, y = 200;
            for (int i = 0; i < 10; i++)
            {
                userControl.Controls.Add(new Label
                {
                    Text = additionTasksList[i],
                    Height = 40,
                    Width = 100,
                    Name = "additionTaskLabel" + i,
                    BackColor = Color.Transparent,
                    Location = new Point(x, y + 15)
                });

                userControl.Controls.Add(new TextBox
                {
                    Name = "container" + i.ToString(),
                    Text = "",
                    Height = 20,
                    Width = 50,
                    BackColor = Color.White,
                    Location = new Point(x + 120, y + 15)

                });

                y = y + 60;
            }
        }


This is where I create a button "Confirm":
public Button CreateSubmitBtnVisually(UCFirstLevelAddition userControl)
       {
           int x = 350, y = 200;
           Button submitBtn = new Button();
           submitBtn.Name = "btnSubmit";
           submitBtn.Text = "Potvrdi";
           submitBtn.Size = new Size(100, 40);
           submitBtn.Location = new Point(350, 829);

           submitBtn.Click += new EventHandler(userControl.submitBtn_Click);

           userControl.Controls.Add(submitBtn);
           return submitBtn;
       }


And this is where I try to check if anyone of the Text Box-es is empty:
public void CreateEmptyContainerLabels(UCFirstLevelAddition userControl)
        {
            int x = 550, y = 180;
            Label message = new Label();
            foreach (Control c in userControl.Controls)
            {
                if (c is TextBox)
                {
                    TextBox container = c as TextBox;
                    if (container.Text.Length == 0)
                    {
                        message.Text = string.Format("Ne sme biti prazno!");
                        message.Width = 400;
                        message.ForeColor = Color.Red;
                        message.Font = new Font("Century Gothic", 16, FontStyle.Bold);
                        message.Location = new Point(x, y + 40);
                    }
                    userControl.Controls.Add(message);
           
                    y = y + 60;
                }
            }

        }


But this check is not doing what I want. Can you help me with this?
Thank's in advance.
Posted
Updated 26-Aug-20 5:38am
Comments
BillWoodruff 12-Sep-20 17:35pm
   
see: https://kmatyaszek.github.io/wpf%20validation/2019/03/06/wpf-validation-using-idataerrorinfo.html
BillWoodruff 13-Sep-20 3:53am
   
Just to make sure: is this WPF or is it WinForms ?
BillWoodruff 13-Sep-20 4:14am
   
Also see: https://stackoverflow.com/a/18204513/133321

1 solution

"Not doing what I want". A "label" is heavier than a TextBlock. And since you can collapse controls, there's not much point in adding primitives on the fly. In any event, there are a few recommended practices when dealing with validation.

Validating with the IDataErrorInfo Interface (C#) | Microsoft Docs[^]
   
Comments
BillWoodruff 12-Sep-20 17:35pm
   
My vote of #2:

'A "label" is heavier than a TextBlock' : A Label is about as light-weight a Control as you can use.

Your link points to an example in an ASP MVC app: the question is about WPF; however, IDataErrorInfo is available in WPF.
Gerry Schmitz 12-Sep-20 21:18pm
   
You need to find another dumpster.

https://stackoverflow.com/questions/5382925/difference-between-label-and-textblock
BillWoodruff 13-Sep-20 3:46am
   
Personal attacks and insults are not appropriate in any CP forum.

I have set your rating to #!.

The OP's code shows creation of WPF TextBox's, not TextBlocks:

https://stackoverflow.com/questions/18204245/is-there-any-difference-between-wpf-textblock-and-textbox

In fact, there's nothing in the code that implies WPF.

Too bad you didn't give the OP the link to the comparison in WPF of Label and TextBlock ... that might have been helpful.
Gerry Schmitz 13-Sep-20 10:21am
   
Still thicker than a brick and selective comprehension. And you're a troll.

"I am creating WPF app in C# ...'

"... I want Label/s to appear with some text beside that empty box/es. "

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900