VB.NET never get focus on any control - vb.net

i have developed a simple calculator like in windows calculator,
but unlike in windows calculator, after clicking any button, the focus on that button is still there on the particular clicked button.
so how to never get focus for all buttons on calculator form ... ?
i don't think that it will better to write loose focus code on every button's click event ... so any better solution ?

Without seeing any code of yours, I am going to assume that you have a text box that displays the numbers pressed by the user, so you need to set the focus to the text box once a user clicks a button, like this:
TextBox1.Focus()
Note: If your text box is not named TextBox1, then change the name to whatever your text box is actually named.

Instead of a standard button use an instance of a NoFocusButton class derived from the Standard button. In this class override the ShowFocusCues property and return always false.
Form f = new Form();
// Need to add manually the buttons to your form unless you build a customcontrol
NoFocusButton b = new NoFocusButton();
b.Text = "ClickMe";
f.Controls.Add(b);
f.Show();
// Class derived by the Button control, it is identical but the
// property that control the drawing of the Focus rectangle returns FALSE
// tricking the WinForm system to avoid to draw the focus rectangle
class NoFocusButton : System.Windows.Forms.Button
{
protected override bool ShowFocusCues
{
get
{
return false;
}
}
}
The credit goes to Remove Focus Rectangle from Button

Related

How to hide and show group of controls using Panel [duplicate]

I am designing a multipage windows form using panels.
I'm displaying a login form and validating the button click, and want to hide the login panel and show the main panel.
However, when I click the button, the login panel disappears alright, but the main panel does not appear. since there is nothing to display, the form window shrinks to just the minimize/maximize/close buttons.
Here's the code for the button:
private void btn_login_Click(object sender, EventArgs e)
{
if (pwdBox.Text == optopwd)
{
MessageBox.Show("Good Morning!!");
loginpanel.Visible = false;
mainpanel.Visible = true;
}
else MessageBox.Show("Incorrect password!");
pwdBox.Text = "";
}
Please let me know what I have missed/misunderstood. Thanks!
Edit:
Screenshots:
Login Screen:
http://img641.imageshack.us/img641/9310/loginscreenj.jpg
Empty window:
http://img163.imageshack.us/img163/1376/emptyx.jpg
The standard mistake is that you accidentally put the mainpanel inside the loginpanel. So when you make loginpanel invisible, the mainpanel can never become visible. This accident is common in the designer, it won't let you put two panels on top of each other. You fix it with View + (Other Windows) + Document Outline. Drag mainpanel and drop it on the form. You'll have to fix the Location property by editing it in the Properties window instead of moving the panel with the mouse.
An entirely different approach is to use a TabControl. Easy in the designer, you just need to hide the tabs at runtime. Code is here.
Or use two UserControls.
Looks like your for is automatically resizing. There are 2 properties on the form responsible for auto size:
AutoSize = True;
AutoSizeMode = GrowAndShrink;
If you have the above settings then your form would shrink just to control panel (buttons) if there's nothing else to display.
Let me know if that helps.
UPDATED
also... does your control "pwdBox" belong to main panel?
Two suggestions:
Try setting the height attribute to 100%
mainpanel.Height = 100%
If that doesn't work, ensure that the page isn't initializing with mainpanel.visible set to false on a postback.

Cannot get the selected item in a ListViewer in a JFace Dialog

I created a dialog class that inherited from JFace Dialog using Windows Builder. In that, I added some controls included a button and a JFace ListViewer. In widgetSelected() function of the button, I can get out the selected item in the ListViewer. But in `okPressed(), I cannot get this. I don't know why. Can you help me?
Thanks!
If you want to access UI elements in okPressed you must do so before calling super.okPressed() because that will close the dialog and dispose of the controls. So something like:
#Override
protected void okPressed()
{
IStructuredSelection sel = viewer.getStructuredSelection();
// TODO deal with selection
// Call super.okPressed() last
super.okPressed();
}
Alternatively save the selection when your widgetSelected is called.

Modeless dialog created by modal dialog in Compact Framework

I am working on a Compact Framework application. This particular hardware implementation has a touchscreen, but its Soft Input Panel has buttons that are simply too small to be useful. There are more than one form where typed input is required, so I created a form with buttons laid out like a keypad. The forms that use this "keypad" form are modal dialogs. When a dialog requiring this "keypad" loads, I load the "keypad" form as modeless:
private void CardInputForm_Load(object sender, EventArgs e)
{
...
keypadForm = new KeypadForm();
keypadForm.Owner = this;
keypadForm.SetCallback(keyHandler);
keypadForm.Show();
}
The SetCallback method tells the "keypad" form where to send the keystrokes (as a Delegate).
The problem I'm having is that the modeless "keypad" form does not take input. It is displayed as I expect, but I get a beep when I press any of its buttons, and its caption is grayed-out. It seems like the modal dialog is blocking it.
I've read other posts on this forum that says modal dialogs can create & use modeless dialogs. Can anyone shed light on this situation? Is there a problem with my implementation?
I found the answer: Set the keypad form's Parent property, not its Owner property, to the form instance wanting the keystrokes. The keypad dialog's title bar stays grayed out, but the form is active.
private void CardInputForm_Load(object sender, EventArgs e)
{
// (do other work)
keypadForm = new KeypadForm();
keypadForm.Parent = this;
keypadForm.Top = 190; // set as appropriate
keypadForm.Show();
}
Be sure to clean up when done with the parent form. This can be in the parent's Closing or Closed events.
private void CardInputForm_Closing(object sender, CancelEventArgs e)
{
// (do other work)
keypadForm.Close();
keypadForm.Dispose();
}
There are two panels on the keypad form, one with numerals and one with letters and punctuation that I want. There is also an area not on a panel that is common to both, containing buttons for clear, backspace, enter/OK, and cancel. Each panel has a button to hide itself and unhide its counterpart ('ABC', '123', for example). I have all the buttons for input on the keypadForm fire a common event. All it does is send the button instance to the parent. The parent is responsible for determining what action or keystroke is desired. In my case I named the buttons "btnA", "btnB", "btn0", "btn1", "btnCancel", etc. For keystrokes, the parent form takes the last character of the name to determine what key is desired. This is a bit messy but it works. Any form wishing to use the keypad form inherits from a base class, defining a method for callback.
public partial class TimeClockBase : Form
{
public TimeClockBase()
{
InitializeComponent();
}
// (other implementation-specific base class functionality)
public virtual void KeyCallback(Button button)
{
}
}
The click event on the keypad form looks like this.
private void btnKey_Click(object sender, EventArgs e)
{
// play click sound if supported
(Parent as TimeClockBase).KeyCallback(sender as Button);
}
The method in the parent form looks like this.
public override void KeyCallback(Button button)
{
switch (button.Name)
{
case "btnCancel":
// setting result will cause form to close
DialogResult = DialogResult.Cancel;
break;
case "btnClear":
txtCardID.Text = string.Empty;
break;
// (handle other cases)
}
}

How to hide a control from the form while designing in visual studio?

I am designing a settings form for my application as shown below:
A tree view with multiple nodes at the left and I want to have one GroupBox for each node to be displayed at the right whenever a node is selected. I have designed my group box with necessary controls for the first node. The question is, how do I design an another group box in the same place when another item is already there. Is there a way to hide a control from a form during design time?
I have always just changed the Z-Order of the GroupBox or Panel by right clicking on it and sending it to back. Just make sure when you add the other GroupBoxes that you add them to the same Parent. Make their Visible property False and then display the GroupBox you want at runtime by making it Visible.
EDIT: Changed answer to be more relevant.
EDIT #2: I missed the VB tag, translating this should be a trivial task anyway.
If you extend GroupBox like this, you'll have a stock GroupBox which will hide itself at design time.
public class myGroupBox : GroupBox
{
public myGroupBox() { InitializeComponent(); }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (DesignMode) this.Visible = false;
}
}
NOTE: This should work for almost any non-sealed control.

Why isn't the LostFocus event occurring?

With a listbox visible, I have clicked on the windows form hoping to use the listbox.lostfocus event to let me hide the listbox - but the event does not occur. I suppose I can use the form.click event to hide the listbox, but how would I get the form to accept focus?
A Form does not want to receive the focus. It was designed to be a container control, it makes sure that one of its child controls always gets the focus. It is technically possible to whack it over the head and make it lose that behavior:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.SetStyle(ControlStyles.ContainerControl, false);
}
protected override void OnClick(EventArgs e) {
this.Focus();
base.OnClick(e);
}
}
This is however a bad idea. A Form doesn't have any way to indicate that it has the focus, you'll also have to override OnPaint() to do something like draw a focus rectangle. If you don't then the user completely loses track of where the focus is located. Then there's the considerable inconvenience that nothing interesting can happen when the user uses the keyboard, a form doesn't have a use for it.
Don't do this. If you want to make a control disappear then add a menu item, toolbar button or a normal button to your UI. Something the user can click on.
the LostFocus event work when the focus move to another control like textbox,... or when the form all of it lost the focus you can use click event for the form to detect taht