Failing to select or set focus to a custom control - vb.net

I have the following code to select a custom control. But it does not want to set the active control to 'uPnlEntryOptions'. I have tried using control.select as well, same result. I have also tried
ActiveControl = uPnlEntryOptions
Code
uPnlEntryOptions.Visible = True
uPnlEntryOptions.Refresh()
uPnlEntryOptions.Select()
uPnlEntryOptions.Focus()
Debug.Print(CStr(uPnlEntryOptions.Focused))
Debug.Print(CStr(uPnlEntryOptions.CanFocus))
Output
False
True

The Focused property is true only if the control has the focus, not a sub-control.
Calling Select (or Focus?) on a UserControl will select the first child control. Afterwards the UserControl.ContainsFocus = True since the focus is WITHIN the UserControl, but the UserControl.Focused = False, since the UserControl itself isn't the focused control.

Related

Unable to change Panel Visibility

I'm having a panel inside another panel. Now I need to alter inner panel's visibility based on certain condition. But I'm unable to change. It always remains to False. Here is the code I'm trying to:
PnlTagFolders.Visible = True 'Always remains False.
PnlTagFolders.BringToFront()
Here is the screenshot for reference:
You can't make a Panel visible if the parent Panel isn't visible. So make sure the parent Panel is visible (.Visible = True).
true if the control and all its child controls are displayed; otherwise, false. The default is true.
source: Microsoft Docs
See the following:
ChildPanel.Visible = False
ParentPanel.Visible = False
ChildPanel.Visible = True
Debug.Print(ChildPanel.Visible) 'False
Another example:
ChildPanel.Visible = False
ParentPanel.Visible = True
ChildPanel.Visible = True
Debug.Print(ChildPanel.Visible) 'True

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;

RadioButtons Highlighting Incorrectly

On my form, I have 4 RadioButtons, each with its appearance set to Button. In my program, I change each of these RadioButton's ForeColour, BackColour and AutoCheck status, as below:
ARadioButton.AutoCheck = False
ARadioButton.BackColor = Color.FromKnownColor(KnownColor.ControlLightLight)
ARadioButton.ForeColor = Color.FromKnownColor(KnownColor.ControlDark)
However, later on, I reset these properties back to default:
ARadioButton.AutoCheck = True
ARadioButton.BackColor = DefaultBackColor
ARadioButton.ForeColor = DefaultForeColor
My issue is that instead of the entire button being highlighted, only the outside is, as shown in the images below.
Originally:
After changes are made and RadioButtons reset to default using code above:
I know this may seem trivial, but I would like the entire RadioButton to be highlighted when the user clicks on the RadioButton, not just the outside.
Is there a way I could somehow reset this?
Try setting the BackColor property to Color.Transparent

How to get scrollbar in Panel in VB.Net?

I am developing a Windows Application in VB.Net. In that, there is one case where there is one form, and in that form there is a Panel, and within the Panel there is a rich text box.
So my requirement is to get a scrollbar in the Panel. So when the user does scroll on the panel, the rich text box can scroll accordingly like MS Office functionality..
Can any one give me an idea how to do it?
Set Panel.AutoScroll = True and scrollbars will automatically appear whenever any controls in the panel fall outside its boundaries.
Set the .Dock property to FILL and the .WordWrap property to FALSE for the richtextbox.
Also set the Panel's .Dock property to FILL.
In order to use panel autoscroll property I do that:
panel.AutoScroll = true
panel.VerticalScroll.Visible = False or panel.HorizontalScroll.Visible = False
In order to know the dimensions of the scroolbars use
SystemInformation.HorizontalScrollBarHeight
SystemInformation.VerticalScrollBarWidth
So you can change the dimension of the panel when the scroolbar is shown.

vb.net tabpage using a form for tabpanels issues

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.