How to always display a control on the currently active tab of a TabControl? - vb.net

I have a tab control with two tabs. Both tabs have controls which are unique to them, but there is one control which I would like to always appear on whichever tab is currently active.
I figure I just need to add some code to TabControl1_SelectedIndexChanged().
I tried
MyControl.Parent = TabControl1.TabPages(
TabControl1.TabPages.IndexOf(TabControl1.SelectedTab))
MyControl.Parent.Update() ' is this necessary?
and I also tried
TabControl1.TabPages(
TabControl1.TabPages.IndexOf(TabControl1.SelectedTab)).Controls.Add(SeMyControl)
but neither worked (the control moved once, but when I went back to the original tab, the control did not appear there.
googling found someone suggesting
TabControl1.TabPages(TabControl1.TabIndex).Controls.Add(MyControl)
but that looks dodgy as the control is never removed from the old tab, so repeated switching would probably add the control multiple times.
I feel that I am close, but not quite ... how do I do it?

No, that works fine since Controls.Add() changes the Parent property. Which automatically removes it from the tab page it was on before. A control instance can only have one parent.
The more straight-forward approach is to simply not put the control on a tab page but leave it parented to the form which a lower Z-order so it is always on top of the tab control. The only problem with that is that the designer will hassle you. It automatically sucks the control into the tab page when you move it on top of the tab control. One trick to fix that is to leave it off the tab control and change its Location property in the form constructor.

Using your second code snippet that you are concerned about because it doesn't remove it from the original tab, why not just remove it from the original tab before you add it to the new tab?
Maybe something like: TabControl1.TabPages(TabControl1.TabIndex).Controls.Remove(MyControl)

Related

Can't View VBA Form

So I created a form, and have been using it for a while.
All of a sudden today it stopped working. Other forms load and can be viewed but not this one.
The weirdest part is that the controls still respond!
So I can't actually see the form but I can still interact with it. (only using keyboard no mouse clicks are responding) I know the form is enabled. But I simply can't see it! I have restarted excel, and the whole computer.
I even completely removed all code in the userform to see if I could get the blank shell to load, and while I can tell it loads (it disables everything in excel until I use Alt+F4) it still won't be visible. I have checked the Enabled, All Colors, Captiuon, Width, Height, Top, Left, StartUpPostion, SpecialEffect, Zoom, DrawBuffer properties and everything is fine.
I have also Exported and reimported it and still simply nothing. Also tried importing it into another Workbook, nothing. I at my wits end (not that it's very far to reach!)
Also, I can still see the userform in the designer I can still edit it, I get NO errors. This happens not only when calling the form in code, but even when running it right from the designer.
Any one ever have this issue? Any ideas or solutions?
The issue ended up being the Top and left properties. While I did check them, they way I checked them was not accurate; I still have no idea why, but I think it defiantly has something to do with dual monitor setup.
The way I tested double checked the forms top and left properties was by creating a new form and adding a button on the button click event I had a msgbox show the top and left properties of the form at time of clicking.
I then moved the new form to where I wanted the form that woudn't show to be and clicked the button; I then set these values to the form that wouldn't show.
I tested this location and made sure to match the properties with the values I wanted multiple times and it didn't work.
For some reason the values needed where about 1000 digits off (instead of 250 I needed to use 1250) even though I had set the form right where I wanted the new one to be and made sure to save the properties when they matched what I wanted

Making multiple forms appear as one in VB.NET

I am writing a Windows Forms application in VB.NET. I have three forms: the main form, which shows a list of accounts, the account form which allows the user to view/edit the information for a specific account, and the policy form which allows the user to view/edit the information on a specific policy for that account. I want the forms to appear as if they are all the same window. Example: when the application starts, the user clicks an account name in the list box on the main form and clicks "edit". What I want to happen is that the window stays in the exact same place and stays the same exact size, only the content of the main form appears to be replaced with the content of the account form. Same thing if the user then chooses to edit a policy from the account form. When the user finishes and clicks "save", the main form comes back up. Through this entire use case, it would appear to the user as if they were viewing the same window the entire time, with the content of that window changing.
How can I do this? I have tried something like:
Dim newForm as New AcctForm
newForm.Location = Me.Location
newForm.Show()
Me.Close()
The problem is that if the user moves the original window, the new window appears where the parent form originally appeared, not where it ended up.
I see this is already in the comments, but what I have done in this case in the past is build each "form" in the application as a custom control. Then I have one actual form, and navigation works by changing which custom control is currently loaded on the parent form. To move from one screen/view to another, you remove the current custom control from the form's controls collection and add the new custom control.
I believe this is superior to manually setting the startup position and size, because you can use the form's .SuspendLayout()/.ResumeLayout() methods to hide the interim state, where there is no control loaded, from the user. This is harder to do when you want one form to be completely replaced by another.
This also makes it easy to set certain form properties in one place and have them be consistent for the application. You can even have an area on the form with controls that will now show in every view.
When using this pattern, I typically have each of my custom controls inherit from a common base. You may not have anything specific you will do with that base at the outset, but it almost always comes in handy later.
Finally, switching to use this scheme is easier than you think. Just go to the code for the each of your current forms, and you will find that each class currently inherits from System.Windows.Forms.Form. Most of the time, all you really need to do is change them to inherit from System.Windows.Forms.Panel and you're most of the way there.
As others have said, it may be better to redesign your application using custom controls or panels etc.
However, to answer your question regarding the seemingly random location of your forms, the first thing to check is that each form has it's StartPosition property set to Manual.
If your main form is resizable, then I would also add code to adjust newForm to the same size too.
I hope that helps with your immediate issues; so that you can move on to redesigning the application!
good morning there is another way . set property for second form to (top most) and use also
from2.show();
that make you switch between forms and keep form2 top other
Thanks
try using ShowDialog()
Dim newForm as New AcctForm
newForm.Location = Me.Location
newForm.ShowDialog()
Me.Close() <-- removed this

Form sent to back on interaction with menu item

I've got two forms, one being frmMain and the other being frmDatasets. The idea is that frmDatasets is used to manage the application's datasets (it's a program for comparing sorting algorithms).
I have a MenuStrip on frmDatasets with a few items. I just decided to change the Add Dataset item to a drop-down menu with options for loading one from file, or generating one according to certain parameters. All well and good; just add the items, swap some icons around, and change the event handler's signature to handle the right Click event.
Except when I run the program, clicking on the Add Datasets top-level item for the first time sends frmDatasets to the back, displaying an inactive frmMain. Clicking the item again shows the dropdown menu as usual.
If I instead click another top-level menu item, that works just fine. But then simply hovering my mouse over the Add Dataset item causes the 'send to back' thing to happen.
I say "send to back", but it's really just putting it behind frmMain. It remains in front of other windows like Visual Studio and Firefox.
Anyone know what on Earth is going on with this form?
EDIT: If I show frmDatasets as a modal dialog, it stops the whole "sending to back" thing, but I still have to click the menu item twice before it shows the list.
EDIT2: Overriding the onClick event to call mnuAddDatasetDropDown.ShowDropDown removes the double-click issue, but it's only a solution if I keep the form as a modal dialog. Still gets sent to back. Overriding mouseEnter doesn't do anything to solve it.
Setting the TopMost property of frmDatasets from its Design view to 'true' fixed all of the problems I was having. No insight as to what was going on, unfortunately, but if anyone else is having this problem then hopefully it'll work for them too.

How to edit Expression Blend selected control part?

I am trying to edit a specific control part within a custom TabItem Template. In this case, it's the TemplateBottomSelected control part.
I'm having an issue where I cannot seem to view or edit any control parts within the template editor except the default TemplateTopSelected. Here's a screenshot of what I'm talking about:
Even though I'm selecting the "TemplateBottomSelected" grid, the only thing I can see is the template for the TemplateTopSelected item.
How the heck do I edit the other control parts within the template using the editor?
Thanks!
that's because these templates are either collapsed or the opacity of them is set to 0. :)
you can use the eye toggle button to only show the one you want to modify at design time, set the visibility to visible, do your changes, and reset it to collapsed.
please let me know if you need more info.

Visual Basic & Context Menu

Is there a way to add a Header to the popup menu? I don't find a property for this.
You could also simulate a header on the context menu by putting the information you want to display in the header as the first item in the context menu. Put a separator (or perhaps two) underneath it and put no code behind it.
That visually separates the item and users will learn pretty quick that clicking the first item doesn't do anything.
This may not work in your situation, but whenever I needed to show a context menu off of a grid row that wasn't the current/highlighted row, I made that row the current row first, then showed the menu. That would eliminate the need for a header on the menu. I assume you're showing the context menu if the user right clicks on the row. Well before showing the context menu, make sure that row is current and highlighted.
Do as Corin says, but disable the first item as well. If you disable it, that should remove confusion entirely.