• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

ASP.NET converting Labels into Textbox controls

We have a web application that creates reports. In one of our older reports, a developer used label controls instead of textboxes to populate data fields returned from the database. When we export the report to Excel, the numeric data fields are being formatted as text in Excel. We have quite a few reports that have this similar issue.

My task is to convert all the labels into textboxes. It would be a very long task to go into the Designer of each report, and manually delete the label controls, and implement a textbox control instead.

Luckily, the way the data is binded to each control is via a method that all reports have access to. So in this method, I am able to programatically hide the labels, and in their place create a textbox. However, I am having difficulty doing so. What is wrong with my code below (starting on line 34). The result is that the data fields simply are empty.

      static void setFormattedValue<T>(ref T ctl, object value, FormatType format)
        {
            Money _money;
            decimal _decimal;
            //double _double;
            Int16 _int16;
            Int32 _int32;
            Int64 _int64;
            DateTime _datetime;

            TextBox txt = ctl is TextBox ? (ctl as TextBox) : null;
            Label lbl = ctl is Label ? (ctl as Label) : null;

            if (lbl != null)
            {
                lbl.ForeColor = Color.Black;
            }
            else
            {
                txt.ForeColor = Color.Black;
            }

            try
            {
                switch (format)
                {
                    case FormatType.AmountFormat:
                        if (Money.IsParsable(value.ToString()))
                        {
                            _money = Money.Parse(value.ToString()).IfNull(Money.Zero).Round();

                            if (lbl != null)
                            {
                                lbl.Visible = false;
                                TextBox replacementTxt = new TextBox();
                                replacementTxt.Location = new PointF(lbl.Location.X, lbl.Location.Y);
                                replacementTxt.Size = new SizeF(lbl.Width, lbl.Height);
                                replacementTxt.Visible = true;
                                replacementTxt.Value = _money.ToDecimal();
                                replacementTxt.OutputFormat = "#,##0.00";
                                replacementTxt.ForeColor = _money.IsLessThanZero ? Color.Red : Color.Black;
                                //lbl.Value = _money.ToString("#,##0.00");
                                //lbl.ForeColor = _money.IsLessThanZero ? Color.Red : Color.Black;
                            }
                            else
                            {
                                txt.Value = _money.ToDecimal();
                                txt.OutputFormat = "#,##0.00";
                                txt.ForeColor = _money.IsLessThanZero ? Color.Red : Color.Black;
                            }
                        }

Open in new window

0
pzozulka
Asked:
pzozulka
  • 7
  • 2
  • 2
1 Solution
 
Michael FowlerSolutions ConsultantCommented:
I don't believe that the Textbox object has a value property
https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox(v=vs.110).aspx

Try changing
replacementTxt.Value = _money.ToDecimal()

Open in new window

to
replacementTxt.Text= _money.ToDecimal()

Open in new window


I would also check that _money contains the appropriate value
0
 
pzozulkaAuthor Commented:
It's showing me that the Textbox object does indeed have a value property. I also changed it to Text with same results.
0
 
Michael FowlerSolutions ConsultantCommented:
Does _money  contain value after the line

_money = Money.Parse(value.ToString()).IfNull(Money.Zero).Round();

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
pzozulkaAuthor Commented:
I did not realize it wasn't a regular TextBox control since it's being used with ActiveReports.
http://helpcentral.componentone.com/nethelp/AR7Help/OnlineEn/GrapeCity.ActiveReports.v7~GrapeCity.ActiveReports.PageReportModel.TextBox_properties.html

I dont believe this makes a difference because on line 35, I am instantiating a new TextBox, and then on line 39 using the Value property. The compiler is not complaining so I'm assuming it's understanding the TextBox I am referring to.
0
 
pzozulkaAuthor Commented:
Does _money  contain value after the line
Yes it does.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
I think it is variable scope:
                            if (lbl != null)
                            {
                                lbl.Visible = false;
//This creates a new variable, which you set various properties to
                                TextBox replacementTxt = new TextBox();
                                replacementTxt.Location = new PointF(lbl.Location.X, lbl.Location.Y);
                                replacementTxt.Size = new SizeF(lbl.Width, lbl.Height);
                                replacementTxt.Visible = true;
                                replacementTxt.Value = _money.ToDecimal();
                                replacementTxt.OutputFormat = "#,##0.00";
                                replacementTxt.ForeColor = _money.IsLessThanZero ? Color.Red : Color.Black;
                                //lbl.Value = _money.ToString("#,##0.00");
                                //lbl.ForeColor = _money.IsLessThanZero ? Color.Red : Color.Black;
//Here the new textbox variable goes out of scope - so the textbox is destroyed
                            }

You need to add the new textbox to the report - you don't do that.  You require something like (air code):
lbl.Parent.Controls.Add(replacementTxt);
That line may need some playing about with to find the exact way to accomplish that task
0
 
pzozulkaAuthor Commented:
Thanks Andy, but no luck.

Below it the TextBox control members -- one of which is called Parent -- Gets a reference to the parent section of the control. (Inherited from GrapeCity.ActiveReports.SectionReportModel.ARControl)
TextBox Control

Below is the Section members -- one of which is called Controls -- Gets a collection of all controls contained in the section.
Section Control

I modified my code to the following to add my new textbox to the collection of controls, but still no luck.

if (lbl != null)
                            {
                                lbl.Visible = false;
                                
                                var replacementTxt = new GrapeCity.ActiveReports.SectionReportModel.TextBox();
                                replacementTxt.Location = new PointF(lbl.Location.X, lbl.Location.Y);
                                replacementTxt.Size = new SizeF(lbl.Width, lbl.Height);
                                replacementTxt.Visible = true;
                                replacementTxt.Value = _money.ToDecimal();
                                replacementTxt.OutputFormat = "#,##0.00";
                                replacementTxt.ForeColor = _money.IsLessThanZero ? Color.Red : Color.Black;
                                replacementTxt.Value = _money.ToDecimal();
                                replacementTxt.OutputFormat = "#,##0.00";
                                //lbl.Value = _money.ToString("#,##0.00");
                                //lbl.ForeColor = _money.IsLessThanZero ? Color.Red : Color.Black;
                                //lbl.Value = _money.ToString("#,##0.00");
                                var parent = lbl.Parent as Section;
                                parent.Controls.Add(replacementTxt);
                                
                            }

Open in new window





On a separate note, I had another idea. In my original post, you can see that I'm passing the control by reference. Would it be possible to modify the reference in such a way that deletes the original label object and replaces it with a textbox object instead?
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Just to clarify.  Are the textbox's there but empty or not displayed.  (To test put some dummy text in the textbox).
0
 
pzozulkaAuthor Commented:
I modified the code as per below:
if (lbl != null)
                            {
                                lbl.Visible = false;
                                
                                var replacementTxt = new GrapeCity.ActiveReports.SectionReportModel.TextBox();
                                replacementTxt.Location = new PointF(lbl.Location.X, lbl.Location.Y);
                                replacementTxt.Size = new SizeF(lbl.Width, lbl.Height);
                                replacementTxt.Visible = true;
                                replacementTxt.Text = "Test";
                                var parent = lbl.Parent as Section;
                                parent.Controls.Add(replacementTxt);
                                
                                
                            }

Open in new window


It does not appear that the textbox is being displayed.
0
 
pzozulkaAuthor Commented:
I think I know what might be causing this to happen. The method posted above (in my original post) is a static method in another class that acts as a utility class (sort of). Perhaps when executing this code there, it doesn't show all the details of why it fails to show the textboxes.

I moved the code above into a method inside the report class itself, and am passing the labels to this method. While debugging, I caught the following exception:

ReportException: Controls can't be added or removed after report starts running. Please modify your ControlCollection in the ReportStart event.

I will give this a try and get back to you.
0
 
pzozulkaAuthor Commented:
This didn't work in the ReportStart event either. Decided to just manually replace all labels containing numeric values with textboxes and call it a day.

Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 7
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now