Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Constituent Controls Size or Lcation In Composite Control In Form Designer

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
VB.net
I have composite control with a label, a textbox, and a combo box.
Is there a way to change the sizes or locations of these controls when the composite control is placed on the form designer?
The only thing I seem to be able to do is change the size of the entire composite control.
I want to be able to use the composite control for various items on a form that would require different sizes but not being able to change anything makes the composite control somewhat useless.


Auguy
Sylvania/Toledo Ohio
 
OK, changed focus a bit and came up with this composite control to display the combo box value in a text box.

Notes: glbViewMode is an integer with a value of zero, any other value puts the control in Edit Mode.
cboAddWidth is a just a fudged integer value (17) to increase the size of the combo box when typing in the combo box.
And Yes it does make the combo box arrow move off the composite control visible space by design.
I was originally gong to make the whole control larger when the combo box size is changed, but decided I like it this way.

Any comments or suggestions are welcome.

Code:
Imports System.ComponentModel

' Purpose: Display the value of a ComboBox without the dimmed appearance when disabled
'          and without having to draw the ComboBox manually
' Control consists of a TexBox and a ComboBox
' TextBox and ComboBox are both set to the composite control width on loading
' TextBox is in front of the ComboBox
' TextBox.Text is set to the ComboBox.Text
' In Edit Mode the textBox is not visible allowing the ComboBox to be viewed and changed
' When NOT in Edit Mode the TextBox hides the ComboBox so it cannot be changed
' The ComboBox is always visible so its value will be changed when the binding source value changes

Public Class myComboTextBox
  ' Control Edit Mode
  Private EditMode As Integer
  ' This Control Width
  Private ThisControlWidth As Integer
  ' Original Combo Selected Index
  Private OrgSelectedIndex As Integer = -1
  ' cbo Width Has Changed Flag
  Private ComboWidthChanged As Boolean

  Public Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
  End Sub

  ' Set Initial Control Settings
  Private Sub myComboTextBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ThisControlWidth = Me.Width
    ComboWidthChanged = False

    ' Change Backcolor Due To ReadOnly BackColor Is Not White
    txtBox.BackColor = myClrWhite

    txtBox.Width = ThisControlWidth
    cboBox.Width = ThisControlWidth
  End Sub

  ' Set View/Edit Mode
  Public Property PropComboTextCurrentMode() As Integer
    Get
      Return EditMode
    End Get
    Set(ByVal Value As Integer)
      EditMode = Value
      txtBox.Visible = CBool(IIf(EditMode = glbViewMode, True, False))
      cboBox.Enabled = CBool(IIf(EditMode = glbViewMode, False, True))
    End Set
  End Property

  ' Set Combo SelectedIndex
  Public Property PropSelectedIndex() As Integer
    Get
      Return cboBox.SelectedIndex
    End Get
    Set(ByVal Value As Integer)
      cboBox.SelectedIndex = Value
    End Set
  End Property

  ' Force Combo to Uppercase & Increase Width To Make AutoComplete DropDown Wider
  Private Sub cbo_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles cboBox.KeyPress
    If Not ComboWidthChanged = True Then
      cboBox.Width = cboBox.Width + cboAddWidth
      ComboWidthChanged = True
    End If

    If Char.IsLetter(e.KeyChar) Then
      e.KeyChar = Char.ToUpper(e.KeyChar)
    End If
  End Sub

  ' Restore Combo Width
  Private Sub cbo_LostLeave(ByVal sender As Object, ByVal e As EventArgs) Handles cboBox.LostFocus, cboBox.Leave, cboBox.MouseLeave
    If ComboWidthChanged = True Then
      cboBox.Width = ThisControlWidth
      ComboWidthChanged = False
    End If
  End Sub

  ' Save Original Combo Selected Index
  Private Sub cboBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.GotFocus
    OrgSelectedIndex = cboBox.SelectedIndex
  End Sub

  ' Check Combo For Invalid Selection, Restore Original SelectedIndex
  Private Sub cboSelection_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles cboBox.Validating
    ' Invalid Selection or No Selection Made
    If cboBox.SelectedIndex = -1 Then
      ' Invalid Selection but Text is Present
      If cboBox.Text.Trim.Length > 0 Then
        MessageBox.Show("Invalid selection", _
                  "Attention", _
                   MessageBoxButtons.OK, _
                   MessageBoxIcon.Exclamation, _
                   MessageBoxDefaultButton.Button1)
        e.Cancel = True

        If OrgSelectedIndex = -1 Then
          cboBox.Text = ""
        Else
          cboBox.SelectedIndex = OrgSelectedIndex
        End If
      End If
    End If
  End Sub

  ' Save Original Combo Selected Index
  Private Sub cboBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.Validated
    OrgSelectedIndex = cboBox.SelectedIndex
  End Sub

  ' Set Textbox Text To Combo Text
  Private Sub cboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.SelectedIndexChanged
    txtBox.Text = cboBox.Text
  End Sub

End Class

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top