Vb.net combobox formatstring property doesn't work - vb.net

I have a form created in VB.net. It is used to get some information form a user. The form is not bound to any data source.
A combobox on this form is used to enter a cost. I want the value entered by the user to be displayed using currency format. I have used the Format String Dialog that opens from the ellipses button on the FormatString property of the combobox and selected Currency. This put C2 into the FormatString property.
When I run my application, this format is not applied to the value entered into the combobox at the time the number is entered or when I leave the combobox.
What am I missing?

Set the FormattingEnabled Property to True.

The FormatString property works only for data-bound controls. However, the input in a control can still be formatted with the ToString() method on a Change or Leave event.
The code sample below will format the text in the combo box to the default currency once the focus leaves control. Error handling can be done in the else clause:
private void comboBox1_Leave(object sender, EventArgs e)
{
string s = comboBox1.Text;
decimal result;
if (Decimal.TryParse(s, out result))
{
comboBox1.Text = result.ToString("C2");
}
}

Related

How do you format a datagridview column that is connected to a datasource?

Current screenshot:
I'd like to format the displayed value in the datagridview in the picture to two decimals, i wanted to make the change at display so i still have my orignal values untouched.
I've tried it placing the format in code
before, (At Form's New Sub)
after assigning the datagridview's datasource, (at Form's New Sub)
and also placing it in the designer, (datagridviewtextboxcolumn.defaultcellstyle)
still the values in the datagridview didnt change
You can put this in form load:
dataGridView.Columns["Total"].DefaultCellStyle.Format = "N2";
OR
private void dataGridView_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == indexOfColumn)
{
e.CellStyle.Format = "N2";
}
}

if text box First word "/" and Item description then form "itemsearch"

If textbox first word "/" and Item description then form "itemsearch" show and "itemsearch" textbox itemname = without "/" text that enter in last form
For Example:
/what is item
in Itemsearch form textbox.text = "what is item"
Add an event handler for LostFocus of the TextBox
XAML
<TextBox x:Name="tBox" LostFocus="tBox_LostFocus" ...
and handle it in your code behind file
private void tBox_LostFocus(object sender, RoutedEventArgs e)
{
tBox.Text = tBox.Text.Trim('/');
}
Anyway, if you want to implement a VIM-like search (looks sort of to me) you should save your search term without the / to a separate string. Users might find it confusing when the content of a recently filled out text box changes automatically

Require a textbox to have value in FIllable PDF

I have a fillable PDF file. I would like to require that a TextBox has a value when the user saves the PDF document i.e. the value is not blank.
Here is what I tried:
(1) Setting the "Required" field on the "TextBox".
PROBLEM: That didn't do much except color the textbox red.
(2) I tried to use the following code in the "onBlur" event:
f = getField(event.target.name)
if (f.value.length == 0)
{
f.setFocus()
//Optional Message - Comment out the next line to remove
app.alert("This field is required. Please enter a value.")
}
PROBLEM: If the user never clicks this box there is no problem
(3) I tried to use the "Validation" tab and run a custom JavaScript.
PROBLEM: If you don't click on the box there is no validation so it is perfectly happy to leave the textbox blank if the user forgets to fill it in
OK, I am out of ideas... Anyone?
Since you are using Acrobat JavaScript I assume you use a viewer that supports and executes Acrobat JavaScript. In this situation you can set the document's WillSave action to a custom JavaScript action and perform validation here. I'm not sure if you can cancel the save operation but at least you can display an alert if the validation fails.
UPDATE: This script will loop through all the fields and display and alert if the field value is empty.
for ( var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
var field = this.getField(fieldName);
if (field.value.length == 0)
{
field.setFocus()
app.alert("Field " + fieldName + " is required. Please enter a value.")
}
}
Put the script in document's Will Save action and it will run every time the user saves the form. In Acrobat you set this in Tools > JavaScript > Set Document Actions and select Document Will Save.

DataGridView editing cells on WinForms

I have a Windows Form VS2010 .NET 4 project with a standard DataGridView bound to a datasource on a form.
The grid has a text column that I want to be a point and edit at the character clicked to.
Like normal textbox/editors when you click on the character you want to adjust. If possible I would also like to use the UP/DOWN keys to move between rows but would like the cursor to move to the same character position obviously in the same column without selecting the entire text.
I have tried a few things:
DataGridView1.ClearSelection()
DataGridView1.BeginEdit(False)
The BeginEdit just puts the cursor at the end of the text, which means another click to point to the character position for editing.
I know a Commercial grid like DevExpress defaults to editing in which you can click to the correct character position with one click but obviously costs money.
I have tried in the DataGridView1_EditingControlShowing event
If TypeOf e.Control Is System.Windows.Forms.DataGridViewTextBoxEditingControl Then
Dim tb As TextBox = e.Control
tb.SelectionStart = 5
tb.SelectionLength = 5
End If
But this does nothing.
I am just trying to remove the two or three clicks to get to the character position that needs adjustment.
I haven't looked at a Custom DataColumn as yet.
Any suggestions would be greatly appreciated.
There is no good out of the box way of doing this. The closest there is is to set the EditMode of the grid to EditOnEnter but that means you only need two clicks, not three.
You will need to write your own column type.
Someone has done just that here.
I haven't checked if that example handles up and down - if it doesn't then you were on the right track with the SelectionStart and SelectionLength properties, just grab the caret position of the cell you are leaving and apply it to the new cell.
It turns out that setting these properties is a little bit more involved that I remembered (possibly because I was already using a MaskedTextBox custom column type last time I did this).
The code below (in c# but the principle holds for vb.Net and I can give the vb code if you can't convert it yourself) works happily - could be tidied up by putting it into a custom control but I'll leave that as an exercise :)
First I add a handler for the EditingControlShowing event:
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewTextBoxEditingControl t = e.Control as DataGridViewTextBoxEditingControl;
current_control = t;
t.Leave += new EventHandler(t_Leave);
}
In the method above current_control is a form level private variable. The event handler for t looks like this:
void t_Leave(object sender, EventArgs e)
{
cell_caret_pos = current_control.SelectionStart;
}
There again we have a class level private field - cell_caret_pos.
Then what I found was that to set SelectionStart and SelectionLength you need to work within the CellEnter event handler:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.BeginEdit(false);
DataGridViewTextBoxEditingControl editControl =
(DataGridViewTextBoxEditingControl)dataGridView1.EditingControl;
if (cell_caret_pos != 0)
{
editControl.SelectionStart = cell_caret_pos;
editControl.SelectionLength = 0;
}
}

Dojo OnKeyPress Handler: TextBox value is blank

I have a Dojo form that does not contain a submit button. Instead, I added an onkeypress handler to calls a method when Enter is pressed. The problem I am having is that when I hit enter before blurring off the current field, the _process method thinks that field is empty.
Or in other words: type in field1. hit tab. type in field2. hit enter. field2 is blank unless i click off the field or shift-tab back.
Any ideas?
dojo.connect(dijit.byId("fkrform"),"onKeyPress",function(e) {
if (e.keyCode == dojo.keys.ENTER) {
_process();
}
and the method it calls:
function _process()
{
var field1 = dijit.byId("field1").value;
var field2 = dijit.byId("field2").value;
alert(username);
alert(password);
...do stuff...
}
The fields are of dojoType: dijit.form.TextBox, and the form is: dijit.form.Form
Use dijit.byId('field1').get('value') instead of directly try to access the property "value". In your example you saved the value in the variable field1 and field2 and in the alert you use the variable username and password could be the answer why you don't get anything. But you still should use the get method to get a property instead of directly access the property.
When you press "Enter" your form will submit. So you need to connect to the "onSubmit" event on the form, instead of onkeyPress or onKeyUp.
The first example i created prints the value of the input box on every key someone pressed in the console.
http://jsfiddle.net/a8FHg/
But what you really wanted was hooking into the submit. I modified the example. The new example connects to "onSubmit" and creates an alert box with the text of the user input.
http://jsfiddle.net/a8FHg/1/
For completness if jsfiddle doesn't work some day. You JavaScript should looks like this.
dojo.ready(function(){
var form = dijit.byId('form');
var box = dijit.byId('box');
var submit = function(event) {
dojo.stopEvent(event);
alert("User input was " + box.get('value'));
};
dojo.connect(form, 'onSubmit', submit);
});
Assuming your form in your HTML has the id form and your box have the id box.