Solved

ASP.NET converting Labels into Textbox controls

Posted on 2015-02-02
11
185 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Setting location of a form 4 22
Trouble with References... 5 25
Calling stored proc in EDMX 11 19
Unable  to create new object 9 25
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now