WinForms Multiline Textbox, Enter key insists on moving focus out of textbox - vb.net

I've got a multiline textbox and a button below it.
VB.NET, WinForms, .NET 2.0
System.Windows.Forms.Textbox
Multiline = True
AcceptsReturn = True
AcceptsTab = False
CausesValidation = False
No Events explicitly coded.
I'd like the Enter key to insert line-feeds and never move the focus to the next control (the button).
I'd like the Tab key to always move the focus to the next control in the tab order (the button).
What happens instead is that the Enter key inserts one line-feed and then moves focus to the next control (the button). It also does this with Ctrl-Enter, which really baffles me!
By my reading of the help files and extensive googling, it should work the way I need it to.
But obviously I'm missing something. What am I doing wrong?

A method I often use for this sort of problem is to iteratively add and subtract code until I can narrow it down to the one thing that caused the problem.
For instance, you might start by making a very simple project with just one edit box and one other control, and see what it does. If this code behaves the way you want it to, then you can start adding code bit by bit, getting the simple project closer and closer to intended final product, until the bug appears. Then look at the last bit of code you added, and see if you can subtract bits of that until the bug goes away. Iterating this a few times might help you find the culprit.
Alternatively, you could start with your existing (misbehaving) code, and start simplifying it until the bug goes away. Then you add back part of the last thing you deleted, and iterate as above.
Lastly, in this case you could also try adding an event handler for the edit control's Leave event, and put a breakpoint in the handler. When the BP hits, check the callstack and see if you can get an idea of what code precipitated the focus change. For this to work, your debugger will probably need to be configured to display code that you don't have source for (i.e. disable the Just My Code option in the debugger). You could even paste a (trimmed) callstack into the question if you want to get the group's help in deciphering it.
p.s. Does anybody have a name for the iterative debugging method described above? If not, may I propose calling it Newton's Method (or perhaps Newtoning), since it resembles Newton's Method for iteratively finding roots of mathematical functions.

It definitely shouldn't do that. The only thing I can think is it's not got enough height to accomodate multiple lines. Try adding...
textBox1.ScrollBars = ScrollBars.Vertical
If not I don't know. Try creating a blank project and creating a form with one text box, one button set the properties and see what happens...

Turns out that I had forgotten that I had done this (below) elsewhere on the same form:
'http://duncanmackenzie.net/blog/Enter-Instead-of-Tab/default.aspx
Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Enter Then
e.Handled = True
Me.ProcessTabKey(Not e.Shift)
Else
e.Handled = False
MyBase.OnKeyUp(e)
End If
End Sub

Related

How do I disable the change events

Currently I have several worksheets and plenty of values that are input or equal something. However with so many worksheets "talking" to each other, this appears to be causing issues. this occurs directly when a field is changed or button is pushed as I expect it to...
but it also happens indirectly when i'm capturing the data and writing it back to the userform with something like Form.textbox.value = range("A1") triggering the change event for the textbox.
Can someone comment if this is a code structure issue? Is there a best practice for what pieces of code should be separated?
I tried the application.enableevents = False but this does not seem to work with the user form the way i would expect. I found the link below and it seems to describe my issue to a tee but I'm not understanding what they mean by putting a public value in the "userform's module" the rest of disabling it and reenabling it makes sense
Link:
http://www.cpearson.com/excel/SuppressChangeInForms.htm
Can someone dumb this down for me or give a better option? Is this common?
Thanks,
Code after adding the public variable
Module where the information causes change event to trigger

Access cell color value of datagridview when it has been set via the cellpainting event args

My searches regarding this have only turned up how to set the color of a datagridview cell, whether that be in the formatting, cell painting, or databound events.
The code that I'm trying to work with is currently setting the cellstyle.backcolor through the datagridview event args in the cellpainting event:
Private Sub dgvAncientCode_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvResults.CellPainting
If dgvAncientCode.Columns(e.ColumnIndex).Name = "Blah" Then
e.CellStyle.BackColor = Color.Red
End If
This works--the specified cells do show up as red--but how can I access this value elsewhere in code? When I check the cell.style it's set to the default, yet it displays on the screen as red.
I know there is a lot wrong with this code, but I'm still relatively new and I'm supporting a lot of legacy stuff that's comprised of every worst practice you can think of. I did come up with a workaround for this by setting the cell.style.backcolor instead of the e.CellStyle.BackColor, which lets me access the value later, but I'm also trying to understand what was happening with the original code. I hate making things work without understanding why, mainly because I know I might be missing something important.
I apologize if this isn't clear or if it's been asked before. I've searched for a couple hours here and elsewhere, so hopefully I didn't miss it.
Edit for additional information: There is a button click event that iterates through the datagridview using a for each loop. That is being used to set the cell color of a custom excel export class. This did not work because none of the cells in the datagridview had a value on the cell.style.backcolor property, even though they show as red in the form. If the cell displays as Red in the form, I should be able to access that somehow--at least, I think I should.
Try one of this. Maybe it can help you. "yourdatagrid.ControlStyle", "yourdatagrid.ItemStyle".

Excel: Fixed Button Position

Needing some help attaching an Excel/VBA button on an Excel sheet. I need it to stay in the same position on the screen regardless of how I scroll or zoom. Preferably, I need this on the bottom left or right of the screen.
I have tried adding a button. Then, I right clicked on the button. Clicked on Format Controls -> Properties -> selected Don't Move or Size With Cells. Am I missing something that's making this not work?
Thanks!
I know this post is old, but here's to anyone it could be useful. The VisibleRange property of ActiveWindow can solve this problem. Use something like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveSheet.OLEObjects("MY_BUTTON'S_NAME")
.Top = ActiveWindow.VisibleRange.Top + ActiveWindow.VisibleRange.Height - 5
.Left = ActiveWindow.VisibleRange.Left + ActiveWindow.VisibleRange.Width - .Width - 5
End With
End Sub
Here is the idea that I put across the comment earlier today :) Typically we can get a Floating User Form by setting the Modal property of the form to be 0 which is indeed a Modeless state.
Basic Points to consider:
Look & Feel of the form to make it look like a Button (Not show title bar/Not Resizable/
Hidden Close Button etc)
Setting the position of the Button
Which Event should trigger the form-button (WorkBook Open)
What would you do with Form Initialize Event
Whcih Events should keep it stick to the same position alive
Further Points to consider:
You might only want to keep this button vissible for the workbook you are working, and if you open another instance of a workbook, do you still want to keep the button
If you minimize the Excel Window instance, how do you plan to manage the state of the button and keep it visible
Post about keep displaying a form even the workbook is minimized.
One other great reference I happend to see, (little bit technical) but worth the shot - at least to get to know the certain properties/methods that you could make use: Extending VBA User Form Control.
The article include the following info, and please note the last line as well :)
They give you access to capabilities that are not available from VBA or from the objects (UserForms, Workbooks, etc.,) that make up a VBA Project. When you call an API, you are bypassing VBA and calling directly upon Windows. This means that you do not get the safety mechanisms such as type checking that VBA normally provides. If you pass an invalid value to an API or (a very common mistake) use a ByRef parameter instead of a ByVal parameter, you will most likely completely and immediately crash Excel and you will lose all your unsaved work. I recommend that until you are confident that your API calls are solid you save your work before calling an API function.
Add new Row on the beginning of your WorkSheet and set your button on it, then:
Freeze Top Row
Right click → properties → placement → change to 3.

vb.net getsettings

I need a form width a checkbox "Don't show this again" for my winform,
but how can i make this so when my Form1 loads it checks the state of the checkbox using the getsettings option?
How can is save the checkbox state to the registery? and how do i get the state?
If Form2.Checkbox.checked = Getsettings() Then
Form2.showdialog
Else
Goto Skip
End IF
Skip:
Windows Forms has built-in support for that, you don't need to write any code. Select your check box. In the Properties window, scroll to the top on expand the "ApplicationSettings" node. Click on "Checked" and drop-down the combobox. Click New. Set the Name property to something meaningful.
Done. Your check box will always restore with the last selection that the user selected. Don't call Show() when it is turned on, dispose the form right away.
The main crux of the question is "how do I read and write value to and from the registry."
See here for a great overview complete with code.
You're beginner so here are a few tips on your code sample:
Name your objects more verbosely. Form2 will mean nothing to you a few hours from now.
Do not use Goto. Just don't. Please. You either show the dialog or you don't. The whole Else block with the Goto in it is just not necessary.
Few more things you'll need to do to get this working:
Handle the CheckBox checked event and write either a 1 or 0 (or T or F) to a registry value
Read that registry value in your GetSettings method
You might want to pass a parameter to GetSettings that specifies exactly which setting you need because you'll probably want to reuse that method for other settings as well

VBA Status Bar

I am working on a Word VBA macro app for 80 or so users. The office has high staff turnover, so training suffers, and so one of the self imposed requirements for this project is comprehensive, friendly documentation. However, to supplement this, and to save newbies having to open up a 100 page document when they want to try something new, I want a status bar on every userform (there are five) that provides contextual help. I find tooltips annoying.
I don't have a lot of experience, so I was wanting to
Essentially, I have a file containing every status string. (This is currently a text file, but I was wondering if I should use a spreadsheet or csv for ease of editing by other staff in future.) Every control has a MouseMove event which refers to a function: getStatus(cID) that opens the file, grabs the line and displays it in the status label. It also grabs a few parameters from the same line in the file, such as whether the label is clickable (to link to a page in the help file), and what colour the label should be.
So a few questions really:
Will the application be slow if a userform is constantly referring to a file? It feels fine to me, but I've been in it far too long, and I'm the only user accessing that file. There will be 80 constantly accessing it.
Is MouseMove over a control the best way? Should I instead use co-ordinates?
Most importantly (in terms of me having to do as little work as possible) is there some way to do this so that I do not have to have a MouseMove event on every single control? I have a good few hundred or so controls, each with their own identifier (well, not yet, but they will if this is the only way to do it). Maybe when the form loads I could load ALL the possible status lines so they're ready for whenever the control is moused over. But then, maybe the loading time is negligible?
Appreciate any ideas or thoughts - especially if VBA already has a whole range of functions to do this already and I'm just trying to reinvent the wheel. I can't use the application status bar, because the user rarely sees the application itself.
Thanks!
EDIT:
It is for both data entry, clicking around and a bit of document generation.
It is a controlled environment so macro security issues aren't a big concern for me - and if something goes wrong it's someone else's fault or problem :)
Is this data entry app or do they just click stuff? Because often the field with focus is different to the item the mouse is hovering over, this can cause a lot of confusion.
Constantly reading from a file is a huge waste of time and resources - it is much better to load them only once into an array or collection when the form is loaded.
On MouseMouse event is better than coordinates because you can move things around without worrying. It's a lot of code but you should be able to generate most of that if you have a list of control names because the code should be identical.
ie
Sub Control_MouseMove()
DisplayStatus(Control)
End sub
I would consider the StatusText property and ControlTipText property of controls for this kind of help.
StatusText
This example sets the status bar help text for the form field named "Age."
With ActiveDocument.FormFields("Age")
.OwnStatus = True
.StatusText = "Type your current age."
End With
ControlTipText
This can be assigned from the property sheet for the control.
Private Sub UserForm_Initialize()
MultiPage1.Page1.ControlTipText = "Here in page 1"
MultiPage1.Page2.ControlTipText = "Now in page 2"
CommandButton1.ControlTipText = "And now here's"
CommandButton2.ControlTipText = "a tip from"
CommandButton3.ControlTipText = "your controls!"
End Sub