VB.Net Hide Tabpage - vb.net

Ive seen some discussion on here about how to hide tabs in a tabcontrol but they all seem to be in C or some variant. I havent seen one for vb.net (i cant do C)
What i want to do is hide or disable all some of the tabs till the user has logged in.
Ive sorted out the login and logout. All i need to do is add the code to enable/disable some tabs until the user has logged in.
Anyone know a good way to do this?
WinForms btw

You just add and remove TabPages from the TabControl through the TabPages collection:
TabControl1.TabPages.Add(myTabPage)
and to remove it:
TabControl1.TabPages.Remove(myTabPage)
Note: Removing a TabPage does not dispose it, it just removes it from the TabPage collection.

Currently, the following code block disables all the controls on that TabPage (Sets Control.Enabled = False). The tab itself is still visible and selectable from the TabControl, it is not hidden. The tab is selectable and all the elements appear disabled.
TabMyTab.Enabled = False
If you want to disable the tab similar to i.e. button.Enabled = False which does not allow the control to be used, you will need to do something different as disabling a TabPage as in the code above disables all controls in that tab. If this is what you want, keep reading. A lot of programmers suggest using the TabControl to disallow the tab from being selected by selecting a different or the previously selected tab. This is the most effective way I know. I would implement this as follows:
Private PreviousTab As New TabPage
Private CurrentTab As New TabPage
Private Sub TabControlName_Deselected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Deselected
PreviousTab = e.TabPage
End Sub
Private Sub TabControlName_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Selected
CurrentTab = e.TabPage
If (PreviousTab.Name <> CurrentTab.Name) And (CurrentTab.Name = UnselectableTab.Name) Then
MessageBox.Show("Tab disabled.", "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TabControlName.SelectedTab = PreviousTab
End If
End Sub
Substitute your own values for "UnselectableTab" and "TabControlName" for your project.

Just hide the whole TabControl by setting its Visible property

Related

Show ContextMenuStripItem without clicking off the current cell

I'm using an UltraGrid which has a ContextMenuStrip with 2 items. These are shown when right-clicking an UltraGridCell.
However, in order to show them, the user has to first click off the cell to take it out of edit mode, then right click on it to show the ContextMenuStripItems.
This has become confusing and irritating to the user, so I was wondering if there is any way that it can be changed to show them when right clicking whilst still in edit mode?
I've tried this to take it out of edit mode after a key is pressed, but it doesn't work.
Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp
ugComm.UpdateData()
If ugComm.ActiveCell.IsInEditMode = True Then
ugComm.ActiveCell.Row.Update()
End If
End Sub
I also tried something in the MouseClick that was suggested on the Infragistics forums, but again it didn't work.
Is there any way that a user right-clicking a cell that is in edit mode can bring up the ContextMenuStripItems rather than this menu?
The above image shows what is currently show when right-clicking a cell in edit mode (The cell is the bottom right white cell). I don't want this to appear, but the CMS instead.
EDIT
I've tried the suggestions in the current answers, but neither of those worked for me. Possibly because the grids are a slightly older version?
My most recent effort was done with the following code:
Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.cmCommRate.Show(mouseX, mouseY)
End If
End Sub
But this wasn't triggered until the cell was no longer in edit mode.
NEITHER OF THE ANSWERS BELOW RESOLVE THE ISSUE. STILL NEEDS AN ANSWER
When any cell of the grid enters in edit mode a TextBox is drawn over the cell. The nice part here is this text box is reused for all the cells in the grid. When you right click on the cell in edit mode the default context menu, that comes from MS, shows. What you need to do is get this text box and assign it your context menu strip. You can do this by handling ControlAdded event of the grid like this:
' create a field to store the TextBox
Private cellTextBox As TextBox
Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
' Check if added control is TextBox
If TypeOf e.Control Is TextBox Then
' If added control is TextBox store it in your private field and set its ContextMenuStrip
If Me.cellTextBox Is Nothing Then
Me.cellTextBox = DirectCast(e.Control, TextBox)
Me.cellTextBox.ContextMenuStrip = Me.ctx
End If
End If
End Sub
I have tried to write an event handler for the MouseUp event with this code
Private Sub grid_MouseUp(sender As Object, e as MouseEventArgs) Handles grid.KeyUp
grid.PerformAction(UltraGridAction.ExitEditMode)
grid.ContextMenuStrip.Show()
End Sub
and it works.
The ContextMenuStrip was added in code with this text (as example)
ContextMenuStrip ctx = new ContextMenuStrip()
ctx.Items.Add("Test1")
ctx.Items.Add("Test2")
ctx.Items.Add("Test3")
grid.ContextMenuStrip = ctx

How do I show a ToolStripMenu after user right-clicks a ToolStripMenuItem?

When the user right clicks on a ToolStripMenuItem (which is part of another ToolStripMenu), I want to show a new ToolStripMenu.
I'm using Visual Basic
I tried creating a ToolStripMenu and adding an Items in code. But when right clicking (on the ToolStripMenuItem) I cannot see the new ToolStripMenu. Is it maybe because I didn't define the location where the new ToolStripMenu should be visible?
THE CODE:
Private Sub RappelVerkoopfacturenToolStripMenuItem_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = System.Windows.Forms.MouseButtons.Right Then
Dim tsFavorites As New ToolStrip
tsFavorites.Items.Add("Add to favorites")
tsFavorites.Show()
End If
End Sub
You have to add the new ToolStrip to the Forms´ controls:
Me.Controls.Add(tsFavorites)
Additionally you don´t have to call the Show() method. Change visibility with the Visible property:
tsFavorites.Visible = True
(Nevertheless, the control is visible after adding it to the controls of the form by default. So you don´t have to set it explicitly.)

Dirty Form with TabControl

Hi I have a form with a TabControl, 3 tabpages and controls(textboxes and comboboxes) in each tabPage.
I have one event for all the changings of the controls in tabPages (controlValueChanged) and one event for the tabControl.SelectedIndexChanged.
So I want to my button (btnOK) enabled if the form is Dirty.
Private Sub controlValueChanged(sender As System.Object, e As System.EventArgs)
If bLoading=False 'bLoading is a boolean that is true after I create and populate the controls
Dirty = True 'Dirty is a boolean property
End If
End Sub
So when I change something in one of my controls the event is fired and btnOK is Enabled.
The problem is that the first time that I change (visiting) to a tabPage the controls is that TabPage fire the controlValueChanged event causing my button to Enabled even if I haven't changed anything in my controls.
If I am visiting a TabPage for second time the event is not fired.
I know that this is normal but how I can overcome this problem and have my Dirty =true only if the control is fired because of something is changing and not when the tabPage is changing?
What about creating a new boolean variable - DoNotMakeDirty, say, then you could, in the TabControl1_SelectedIndexChanged sub, set DoNotMakeDirty = True and then put an if statement in your controlValueChanged sub.

Setting Focus on a Tab

I have a tab in a windows form called Wafer Map that has three sub-tabs. The First sub-tab is the called Map and has a Load and Skip button. I am trying to set the focus on the Wafer sub-tab on the Load button click. This is the following code I have tried to use.
Private Sub Load_Wafer_Layout_Map_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Load_Wafer_Layout_Map.Click
Wafer_Info.Enabled = True
Wafer_Info.Show()
End Sub
The Wafer_Info.Enabled = True is used to enabled all of the controls on the Wafer tab and works properly when the button is clicked. I have tried using .Focus() and .Show() to bring focus to the next tab but I am not have any luck getting to switch. Anyone have any suggestions?
Just set it:
tabControl.SelectedTab = yourTab
On the Tab Controls Tab Pages, just ensure you name the tab you are attempting to reference. Additionally, see MSDN TabControl.SelectedTab
The code that worked for me is Tab_WaferMap.SelectTab(1). Tab_WaferMap is my main tab and the 1 is the index of the sub tab I wanted to show
I came across this thread as i was looking for a solution to my own focus issue. I have a TabControl with many TabPages. Each TabPage is set to auto scroll due to overflowing content. The problem I ran into was the mouse scroll wheel would not function if the TabPage did not have focus. Since there is not an event for each tab click it made setting focus to each TabPage a challenge. It was not hard, but a challenge none the less. So, here is my code (assuming auto scroll true).
On form load sets focus to main TabPage:
Private Sub frmParent_Load(sender As Object, e As System.EventArgs) Handles Me.Load
TabControl1.TabPages(0).Focus()
End Sub
Sets focus to current TabPage by getting the index then setting focus.
This is triggered by TabControl1.SelectedIndexChange event.
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim intTabIndex As Integer = TabControl1.SelectedIndex
TabControl1.TabPages(intTabIndex).Focus()
End Sub
I hope someone find this useful. It was very useful for me.
Joshua
You can also set the Selected Index of the tab (and sub-tab) using a (zero based) numeric value:
TabParent.SelectedIndex = 3
TabSub.SelectedIndex=2

Right click: menu options

there is a feature I'd like to implement in my application:
The user right clicks on my picturebox object. Good.
When that happens, some code of mine executes and will generate a list of options.
Then a menu appears where the mouse right-clicked, made up of these options.
When the user clicks on one of these options, the menu is deleted and some code is run given the option index as parameter.
My two problems:
How can I tell when the user right clicks? I can see an event handler for "click", but that includes left clicks....
How do I create one of these menus? I mean, go ahead and right click something. That's the kind of menu I'm looking for.
You need to implement the picturebox' MouseUp event. Check if the right button was clicked, then create a ContextMenuStrip with the menu items you want. You can use, say, the Tag property of the items you add to help identify them so you can give them a common Click event handler. Like this:
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If e.Button <> Windows.Forms.MouseButtons.Right Then Return
Dim cms = New ContextMenuStrip
Dim item1 = cms.Items.Add("foo")
item1.Tag = 1
AddHandler item1.Click, AddressOf menuChoice
Dim item2 = cms.Items.Add("bar")
item2.Tag = 2
AddHandler item2.Click, AddressOf menuChoice
'-- etc
'..
cms.Show(PictureBox1, e.Location)
End Sub
Private Sub menuChoice(ByVal sender As Object, ByVal e As EventArgs)
Dim item = CType(sender, ToolStripMenuItem)
Dim selection = CInt(item.Tag)
'-- etc
End Sub
To your first question: you actually handle just the "click" event, there is no separate event for right-click. But look at the EventArgs object you get passed for the event: it includes information of which button was pressed (and would give you more info if a mouse click had anything beyond that). So you check the button within an if block, and you're good to go.
To your second question: http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx. If your menu is pre-defined, just look for that component on the Designer and prepare the menu from there and call its Show() method from the click handler. If you need to decide the menu entries on the fly, the linked documentation page actually includes an example on that ;)
PS: oops, I just noticed Jon's comment on the question. The answer I gave you is for Windows Forms. If you are on WPF let us know and I'll update with the details (although the concepts ain't too different).
There is actually an easier way to do this. Double-click on the control you wish to be able to right click. Now go to the top of the page and it should say in comboboxes; 'Control' and 'Click' Click on the 'click' combobox and look for: Right-Click. Use a ContextMenuStrip for your right click menu.
Now you can choose which function you want.
Private Sub PictureBox1_RightClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.RightClick
ContextMenuStrip1.Show()
MsgBox("Content Activated.", MsgBoxStyle.Information, "Success!")
End Sub
Hope I could help. :)
Coridex73