Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET converting Labels into Textbox controls

Posted on 2015-02-02
11
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 44

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 44

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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