StrataFrame Forum

Setting AutoAdjustmaxLength in base class

http://forum.strataframe.net/Topic25336.aspx

By Ger Cannoll - 12/6/2009

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
By Les Pinter - 12/7/2009

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

By Aaron Young - 12/7/2009

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

By Ger Cannoll - 12/7/2009

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 .

By Les Pinter - 12/8/2009

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

By Aaron Young - 12/8/2009

Hi Les,



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



Aaron
By Les Pinter - 12/8/2009

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

By Greg McGuffey - 12/9/2009

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
By Les Pinter - 12/9/2009

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

By Greg McGuffey - 12/9/2009

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
By Les Pinter - 12/9/2009

Hi Greg,

   So what kind of cookies do you like...

Les

By Russell Scott Brown - 12/9/2009

Ha! 

This time of year probably gingerbread.

By Greg McGuffey - 12/9/2009

Oatmeal-chocolate chip-walnut. Tongue
By Les Pinter - 12/10/2009

Dude, I was the one who didn't think to cast it. Actually, this opens up a whole raft of possible topics for Subclassed StrataFrame controls. I'll credit you liberally, as you deserve. And my wife needs your mailing address for the cookies...

Les