Setting AutoAdjustmaxLength in base class


Author
Message
Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
I want to set the AutoAdjustMaxLength to true in my Base Class Textbox. Where is the best place to put this and also to ensure it does not fire for Text Boxes that are not bound to a Business Object
Les Pinter
Les Pinter
StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)
Group: Forum Members
Posts: 43, Visits: 213
Hi Gerard,

   If you add just three lines of code to the Enter handler of the subclassed textbox, I think it will work as you want. This is based on the code from my Subclassed StrataFrame TextBox article at LesPinter.com:

using System; using System.Drawing;

public class MyTextBox : MicroFour.StrataFrame.UI.Windows.Forms.Textbox
{
  public MyTextBox()
  { Font = new System.Drawing.Font(
    "Courier New", 10F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));

// New:
    this.Enter += new System.EventHandler(this.myTextBox1_Enter);
    this.Leave += new System.EventHandler(this.myTextBox1_Leave); }

  private void myTextBox1_Enter(object sender, EventArgs e)
  { ForeColor = Color.White;
    BackColor = Color.Blue;

// Add the following three lines of code:

    if   ( BusinessObject == null)
         { AutoAdjustMaxLength = false; }
    else { AutoAdjustMaxLength = true; }

  }

  private void myTextBox1_Leave(object sender, EventArgs e)
  { ForeColor = Color.Black;
    BackColor = Color.White; }
}

   I've attached my working sample code. Use the enclosed script to create the SQL database "Pinter" used by the program.

Les

Aaron Young
Aaron Young
StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
Hi Gerard,

If your forms are disabled until the user clicks Add or Edit, you can also tap into the EnabledChanged event. In the event I check if the control is enabled and if the multiline property is false before setting autoadjustmaxlength. I use textboxes for notes type fields (VARCHAR(MAX) in SQL) and wasn't sure of the implications of setting autoadjustmaxlength to true for these fields which is why I ignore them.

This is a real nice property to have Smile

Aaron

Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)Advanced StrataFrame User (654 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
Les , Aaron many thanks for your replies.

I have this working now ok but if the textbox source happens to be a Varchar(Max) it blows out with an eror-> Value of -1 is not valid for Max Length in setMaxLength method. I realise this is because it is a Varchar(max) field and no way of knowing what the max size is.

So  I have put in a condition to test for Multiline. This is ok as long as I have Multiline set on for Varchar(Max) fields which would normally be the case, but not necessarily always if there was a situation , where in certain circumstances, I did not want to have multiline on.

Is there a way to check that the TextBox source is a VarChar(Max) field. If I could do this, it would be a more definitive way of checking, rather than relying on the user interface where multiline had to be switched on .

Les Pinter
Les Pinter
StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)
Group: Forum Members
Posts: 43, Visits: 213
Hi Gerard,

   The only time you could intercept the fact that the column is based on a varchar(MAX) is at BO generation time, so I don't think you can plan to approach the problem in that way; a custom property, or additional code in one of the TextBox event handlers, is where you'll have to deal with this.

Les

Aaron Young
Aaron Young
StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)StrataFrame User (439 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
Hi Les,



Is there any chance you could modify the textbox control to detect this at the point the MaxLength is set?



Aaron
Les Pinter
Les Pinter
StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)
Group: Forum Members
Posts: 43, Visits: 213
Hi Aaron,

   I changed the datatype of EventName in the RodeoEventConfiguration table to VarChar(MAX) and rebuilt the business object, and then looked at the generated class code. The MaxLength property of eventName is set to -1 at line 860 of my generated BO class. There's no event that can be handled to intercept that assignment - it's just there.

   I'm looking into how best to detect that in a subclassed StrataFrame textbox's constructor. More on this story as it develops...

Les

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.5K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
The BO knows about the fields that is managing. I'd think you could test for the field length of the bound field using the FieldLengths dictionary of the bound BO. To make is generic, you could also check out the FieldDbType as well. I.e.



//-- This handles setting the AutoAdjustMaxLength based on if the

//   TextBox is bound to a non-max varchar field

    string boundField = string.Empty;

    if (!string.IsNullOrEmpty(this.BindingField))

    {

      boundField = this.BindingField;

    }

    if ( BusinessObject != null

       && this.BusinessObject.FieldDbTypes[boundField] == DbType.String

       && this.BusinessObject.FieldLengths[boundField] > 0)

        AutoAdjustMaxLength = true;

    }

    else { AutoAdjustMaxLength = false; }




(NOTE: I just typed this out, so there could be syntax/spelling errors, but you should get the idea).



You could run this as suggested in the Enter event or the EnabledChanged event. However, what really is driving this is the binding state. I'd be tempted to sub-class the control, override the BusinessObject and BindingField properties, adding an event like BindingStateChanged and then handle this in that. Of course, better yet would be for this to be built into the framework! BigGrin
Les Pinter
Les Pinter
StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)StrataFrame Team Member (73 reputation)
Group: Forum Members
Posts: 43, Visits: 213
Hi Greg,

   That sure looks interesting, and if you can find a way to get rid of the little red squiggles under FieldDbTypes and FieldLengths (see attached screenshot), I'll give you a cookie<g>.

Les

Attachments
Snap1.gif (139 views, 22.00 KB)
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.5K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
My first thought was white out.... Blink



Then I realized that I forgot that the BusinessObject property of IBusinessBindable is BusinessLayerBase and not BusinessLayer. So you need to cast it:



((BusinessLayer)this.BusinessObject).FieldDbTypes[boundField] == DbType.String;



I did mention that I just typed the code in the post and there might be some syntax errors....Blush
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search