Solved

ASP.NET converting Labels into Textbox controls

Posted on 2015-02-02
11
191 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:Michael74
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:Michael74
ID: 40585424
Does _money  contain value after the line

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

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

831 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