?
Solved

ASP.NET converting Labels into Textbox controls

Posted on 2015-02-02
11
Medium Priority
?
218 Views
Last Modified: 2015-02-03
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
Comment
Question by:pzozulka
  • 7
  • 2
  • 2
11 Comments
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 40585371
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
 
LVL 8

Author Comment

by:pzozulka
ID: 40585419
It's showing me that the Textbox object does indeed have a value property. I also changed it to Text with same results.
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 40585424
Does _money  contain value after the line

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

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 8

Author Comment

by:pzozulka
ID: 40585433
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
 
LVL 8

Author Comment

by:pzozulka
ID: 40585436
Does _money  contain value after the line
Yes it does.
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 40585580
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
 
LVL 8

Author Comment

by:pzozulka
ID: 40586837
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 40586910
Just to clarify.  Are the textbox's there but empty or not displayed.  (To test put some dummy text in the textbox).
0
 
LVL 8

Author Comment

by:pzozulka
ID: 40586952
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
 
LVL 8

Author Comment

by:pzozulka
ID: 40586996
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
 
LVL 8

Author Comment

by:pzozulka
ID: 40587161
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses
Course of the Month4 days, 17 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question