vb.net tabpage using a form for tabpanels issues - vb.net

I have a simple vb.net form a tabpanel strip, and then a seperate form which is loaded for the tabpage.
Here is the code for the button that dynamically creates new tabs:
Dim tempTab As New TabPage
initTab(tempTab)
xt.TabPages.Add(tempTab)
xt.SelectedIndex = xt.TabCount - 1
Here is the code for the "initTab":
Dim tmpTab As New MainTab
tmpTab.Dock = DockStyle.Fill
tmpTab.Panel1.Dock = DockStyle.Fill
tab.Controls.Add(tmpTab)
tab.Text = "Untitled"
tab.Name = " "
I can easily set the focus of any tab by entering following which sets the focus for example to the last tab:
xt.SelectedIndex = xt.TabCount - 1
Now the issue is, how can I set the focus to a textbox on the custom form (in my example labeled "MainTab")? I've tried virtually everything I can google and I can't seem to find any example of how to setfocus or even set/get anything from the MainTab form.
Anyone can help me?

Erm, turning a form into a child control takes some surgery. You have to set its TopLevel property to false, hide the border, make it visible. I don't see it in the code snippet, is MainTab actually a form?
Anyhoo, you cannot use the Focus() method on a control until it is visible. Odds are good that it isn't visible yet in your code snippet. Use the Select() method instead. Say:
tmpTab.TextBox1.Select()
Or just set the TabIndex property of the first control that should get the focus to 0 in the designer.

xt.Controls(xt.SelectedIndex).Controls("TEXTBOXNAME").Focus()
Just make sure that you set the Name property of the textbox you want to have focus (in this case the name would be TEXTBOXNAME) if you do it like this.

Related

Changing MDI background affects child form background [duplicate]

How can I change the BACKGROUND color of the MDI FORM in C#?
I changed it using the background color property but the color is not changed.
What should I do to perform this task?
The actual BackGround colour of the MDI control is based on the colour in the Windows current Theme. You have to physically set the MdiClient control's background inside the WinForm.
// #1
foreach (Control control in this.Controls)
{
// #2
MdiClient client = control as MdiClient;
if (!(client == null))
{
// #3
client.BackColor = GetYourColour();
// 4#
break;
}
}
Edit - Added comments:
We need to loop through the controls in the MdiParent form to find the MdiClient control that gets added when you set the Form to be an MdiParent. Foreach is just a simple iteration of a type through a collection.
We need to find the MdiClient control within the form, so to do this we cast the current control within the loop using the 'as' keyword. Using the 'as' keyword means that if the cast is invalid then the variable being set will be null. Therefore we check to see if 'client' is null. If it is, the current control in the loop is not the MdiClient control. As soon as the variable 'client' is not null, then the control we've got hold of is the MdiClient and we can set its background colour.
Set the backcolour to anything you want. Just replace "GetYourColour()" with whatever colour you want, i.e. Color.White, Color.Blue, Colour.FromArgb(etc)...
As there is only ever 1 MdiClient, there's no point continuing the loop as it's just a waste of processing time. Therefore we call 'break' to exit the loop.
Let me know if you want anything else explaining.
Write this in your load method of your MDI form.
Controls.OfType<MdiClient>().FirstOrDefault().BackColor = Color.Purple;

VB.NET - form inside a panel resize

I can't figure this out! I've researched docking, anchoring etc. but I'm yet to get it to correctly resize when the parent form resizes.
Edit:
Quick Overview:
I have a main form with a menustrip docked to the top and a panel set to fill. I have links within the menustrip which open forms within the panel. See code below. I am struggling to get the form within the panel to resize with the panel.
I've set the panel background to black and my form inside the panel to white. I can see the panel resizing with the form correctly. The form within the panel stays in its original position. If i start my app in maximized, then click on my menustrip sure enough the form within the panel resizes correctly. However, once it is maximized it doesn't shrink with the form like the panel container does.
I've deleted everything on my whole form, added 1 panel to it and set to fill then on form load do the following:
Dim f As New Contactdetails With {.TopLevel = False, .AutoSize = True}
f.Dock = DockStyle.Fill
Me.Panel2.Controls.Add(f)
f.Show()
This still doesn't work. When i maximize my main form, the form inside the panel does not resize but the panel does as i've changed it's background to monitor its change in size
Turn AutoSize off:
Dim f As New Contactdetails With {.TopLevel = False, .AutoSize = False}
f.Dock = DockStyle.Fill
Me.Panel2.Controls.Add(f)
f.Show()
If the previous form in the panel isn't being used anymore, then you should first dispose of it:
While Me.Panel2.Controls.Count > 0
Me.Panel2.Controls(0).Dispose()
End While
Set the WindowState to Normal in properties of the child form
and
Me.Dock = DockStyle.
Fill in the Load sub of the child form

Unable to dynamically load a control into a SplitContainer panel

I have placed a SplitContainer control onto a form. I have a custom control inside panel 1. This custom control is the container for another user-control.
There is a TreeView control inside this user-control. I am trying to load another user-control onto panel 2 upon selection of a node in the tree view. But its not getting loaded. Am i missing something?
The code for loading the control is given below:
Dim ucImportExcel1 As New ucImportExcel()
frmMain.SplitContainer1.Panel2.Controls.Add(ucImportExcel1)
ucImportExcel1.Dock = DockStyle.Fill
An addition to the above: In the same treeview selection event
For the code below it sets the form text:
me.parentform.text = "Sample Text 1"
Whereas if use this code, nothing happens:
frmmain.text = "Sample Text 2"
When referred by a Directcast, it solved the problem:
DirectCast(Me.ParentForm.Controls.Item("SplitContainer1"), System.Windows.Forms.SplitContainer).Panel2.Controls.Add(ucImportExcel1)

Windows Forms: Unable to Click to Focus a MaskedTextBox in a Non TopLevel Form

Like the title says, I've got a Child form being shown with it's TopLevel property set to False and I am unable to click a MaskedTextBox control that it contains (in order to bring focus to it). I can bring focus to it by using TAB on the keyboard though.
The child form contains other regular TextBox controls and these I can click to focus with no problems, although they also exhibit some odd behavior: for example if I've got a value in the Textbox and I try to drag-click from the end of the string to the beginning, nothing happens. In fact I can't use my mouse to move the cursor inside the TextBox's text at all (although they keyboard arrow keys work).
I'm not too worried about the odd TextBox behavior, but why can't I activate my MaskedTextBox by clicking on it?
Below is the code that shows the form:
Dim newReportForm As New Form
Dim formName As String
Dim FullTypeName As String
Dim FormInstanceType As Type
formName = TreeView1.SelectedNode.Name
FullTypeName = Application.ProductName & "." & formName
FormInstanceType = Type.GetType(FullTypeName, True, True)
newReportForm = CType(Activator.CreateInstance(FormInstanceType), Form)
Try
newReportForm.Top = CType(SplitContainer1.Panel2.Controls(0), Form).Top + 25
newReportForm.Left = CType(SplitContainer1.Panel2.Controls(0), Form).Left + 25
Catch
End Try
newReportForm.TopLevel = False
newReportForm.Parent = SplitContainer1.Panel2
newReportForm.BringToFront()
newReportForm.Show()
I tried your code and got a good repro this time. As I mentioned in my original post, this is indeed a window activation problem. You can see this in Spy++, note the WM_MOUSEACTIVATE messages.
This happens because you display the form with a caption bar. That convinces the Windows window manager that the window can be activated. That doesn't actually work, it is no longer a top-level window. Visible from the caption bar, it never gets drawn with the "window activated" colors.
You will have to remove the caption bar from the form. That's best done by adding this line to your code:
newReportForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Which will turn the form into a control that's otherwise indistinguishable from a UserControl. You can still make it distinctive by using this code instead:
newReportForm.ControlBox = False
newReportForm.Text = ""
Either fix solves the mouse click problem.
This is a miserable bug and it took me a long time to find this question. We're doing exactly the same thing as the OP, displaying a Form inside a split container. My workaround was to add an event handler to the MaskedTextBox's Click event:
private void MaskedTextBoxSetFocus(object sender, EventArgs e)
{
var mtb = (MaskedTextBox)sender;
mtb.Focus();
}
This works for the MaskedTextBox but I'm concerned about other odd behavior due to this bug so I will probably set the border style as in the accepted answer.
The text box behavior is a symptom of the same problem. Something is swallowing mouse down notifications. It isn't explained by your code snippet. Forms indeed swallow the mouse click that activates them, but that is a one-time behavior and is turned off by setting its TopLevel property to False.
Not much left. One candidate is the Control.Capture property, turned on at the MouseDown event for a button so that the button can see the MouseUp event, no matter where the mouse moved. That's a one-time effect as well. Watch out for controls that set the Focus in a MouseDown event.
The other is some kind of IMessageFilter code in your form(s) that's eating WM_LBUTTONDOWN messages.

How to dock a control inside a panel at runtime on windows forms?

I am designing a simple user interface using winforms. In the designer I have a panel on the form and would like to add a datagridview control into the panel at runtime and set the dock property of the datagridview to 'Fill' so that it fills the panel.
I am struggling to do this and following code is not working out for me:
Dim MyDataGridview as New DataGridView()
MyDataGridView.Dock = DockStyle.Fill
Me.MyPanel.Controls.Add(MyDataGridview)
Can anybody help point me in the right direction?
Edit:
Sorry for being vague - I don't get an error but the datagridview isn't visible. It gets added 'behind' the panel and so I tried using .SendToBack() and .BringToFront() methods thinking that the panel was hiding the datagridview but this doesn't seem to work either. At any rate, it seems like the datagridview is being added to the form but just not docked within the panel
Actually, your code ought to work, but you do realize you'll just get a grey rectangle w/o columnheaders etc?
Anyway, the following should be a bit more reliable:
Dim MyDataGridview as New DataGridView()
Me.MyPanel.Controls.Add(MyDataGridview)
MyDataGridView.Dock = DockStyle.Fill
MyDataGridView.Visible = True
Set the Dock property AFTER adding the DataGridView to the panel and then call "BringToFront()" to change the Z-index.
I think that the Z-index part is what makes the trick ;)
This was happening to me, but I had forgotten to un-suspend the layout logic of the container after adding the panel :). So all I needed was:
Container.ResumeLayout() ' docking works now!
Try this:
Dim MyDataGridview as New DataGridView()
MyDataGridView.Parent = Me.MyPanel
MyDataGridView.Dock = DockStyle.Fill
MyDataGridView.Show