How can i solve KeyPress with backspace issue - c#

I'm pretty new with this and need help.
I have a private sub:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox1.KeyPress
Dim strCurrency As String = ""
strCurrency = strCurrency & e.KeyChar
If strCurrency.Length = 0 Then
textbox1.Text = ""
ElseIf strCurrency.Length > 3 Then
MessageBox.Show(e.KeyChar)
textbox1.Refresh()
textbox1.Text = strCurrency.Substring(0, strCurrency.Length - 3) & "." & strCurrency.Substring(strCurrency.Length - 3, 2)
End If
textbox1.Select(strCurrency.Length, 0)
End Sub
The point is, i want to put "." on text box like the currency format. Example: 123.456, 12.345 etc.
When i input the number, it work well, but when i backspace, its gonna be wrong.
I already debug using Messagebox.show(strCurrency) and Messagebox.show(strCurrency.Lenght) and count it with this logic:
textbox1.Text = strCurrency.Substring(0, strCurrency.Length - 3) & "." & strCurrency.Substring(strCurrency.Length - 3, 2)
But the result went wrong. The result be 1.2 when the textbox i input 12345 and i backspace 1 times.

Related

Translating "Bodies to Parts" Macro in CATIA V5 from VBA to C#

I'm trying to convert the Bodies from Parts to Parts in a Product in CATIA. I found a VBA script that works fine, but I have to translate it to C#.
The VBA script is the following:
Sub GreateProductsFromBodies_SelectAllBodies()
On Error Resume Next
Set CATIA = GetObject(, "CATIA.Application")
'Declare variables
Dim oPartDoc As PartDocument
Dim oPart As Part
Dim oProductDoc As ProductDocument
Dim oProduct As Product
'Create a new ProductDoc and rename it's PartNumber equals to Partdoc's PartNumber
Set oPartDoc = CATIA.ActiveDocument
Set oProductDoc = CATIA.Documents.Add("Product")
oProductDoc.Product.PartNumber = oPartDoc.Product.PartNumber
'Arrange windows use "Title Vertically" ,then active window contain Partdoc
CATIA.Windows.Arrange catArrangeTiledVertical
CATIA.Windows.Item(1).Activate
'Check the Body's name use "For ... Next"loop . If Body's name duplicate,then rename.
Dim j As Integer, k As Integer
For j = 1 To oPartDoc.Part.Bodies.Count
For k = 1 To oPartDoc.Part.Bodies.Count
If oPartDoc.Part.Bodies.Item(j).name = oPartDoc.Part.Bodies.Item(k).name And j <> k Then
oPartDoc.Part.Bodies.Item(j).name = oPartDoc.Part.Bodies.Item(k).name & "_Rename_" & j
End If
Next
Next
'Copy Bodies from PartDocument
Dim i As Integer, ProductPN As String, FinalProductPN As String
For i = 1 To oPartDoc.Part.Bodies.Count
With oPartDoc.Selection
.Clear
.Add oPartDoc.Part.Bodies.Item(i)
.Copy
.Clear
End With
'Modify the Product's PartNumber,replace "\" and "."to "_" ,then delete Space
ProductPN = oPartDoc.Part.Bodies.Item(i).name
If Right(ProductPN, 1) = "\" Then
ProductPN = Left(ProductPN, Len(ProductPN) - 1)
End If
FinalProductPN = Replace(Replace(Replace(ProductPN, "\", "_"), ".", "_"), " ", "") 'Replace "\" and "."to "_",Delete Space
'Paste Body in Product's Part as Result
Set oProduct = oProductDoc.Product.Products.AddNewComponent("Part", FinalProductPN) 'Add Part
With oProductDoc.Selection
.Clear
.Add oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent.Part
.PasteSpecial "CATPrtResultWithOutLink"
.Clear
End With
oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent.Part.Update
Next
'Use Msgbox to echo the complete flag
MsgBox "All the select Bodies had been created as a PartDocument successfully !" & Chr(13) & _
">>> The Partdocument's Bodies's count : " & oPartDoc.Part.Bodies.Count & Chr(13) & _
">>> The ProductDocument's PartDocument's count : " & oProductDoc.Product.Products.Count, _
vbOKOnly + vbInformation, "#LSY >>> CATIAVBAMacro of Part to Product >>> Run Result"
End Sub
I translated every line, except the line:
oProductDoc.Selection.Add oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent.Part
I found no corresponding property in C#, cause the last property Part is missing in C#.
I wrote:
oProductDoc.Selection.Add(oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent.??????);
I'm very thankfull for every help!
I solved it, if someone else is in this situation:
I had to cast the line as PartDocument, whish gives me the needed .Part Property!
Before the selection:
PartDocument partDoc = oProductDoc.Product.Products.Item(i).ReferenceProduct.Parent as PartDocument;
And in the required line:
oProductDoc.Selection.Add(partDoc.Part);

C# + Excel. NamedRange.Formula

Is there a limit on the length of the formula?
If I use:
string form = "=ЕСЛИ(ЛЕВСИМВ(C2;3)=\"101\";\"цту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"102\";\"сзту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"103\";\"юту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"104\";\"пту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"105\";\"уту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"106\";\"сту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"107\";\"двту\";\"скту\")))))))";
xlWorkSheet.Range["D2"].FormulaLocal = form;
That's all okay.
But if I use:
string form = "=ЕСЛИ(ЛЕВСИМВ(C2;3)=\"101\";\"цту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"102\";\"сзту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"103\";\"юту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"104\";\"пту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"105\";\"уту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"106\";\"сту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"107\";\"двту\";ЕСЛИ(ЛЕВСИМВ(C2;3)=\"108\";\"скту\";\"fuck\"))))))))";
xlWorkSheet.Range["D2"].FormulaLocal = form;
That error takes off:
HRESULT: 0x800A03EC
It seems there is a limit to the length of string written to .FormulaLocal (and other forms of .Formula)
By experimentation in VBA, it apears to be 258 characters. I can find some references on various blogs to this suggesting a limit of about 256, but can find an official source.
Interestingly, this limit doesn't apply if the string doesn't include a = prefix.
Tested on Excel 2010.
For completness, here's the test code
Sub Demo()
Dim s As String, ss As String, f As String
Dim r As Range
Dim i As Long
Set r = [A20]
s = "=""This is a test of string length: "
ss = "z"
On Error GoTo EH
For i = 1 To 350
f = s & ss & """"
Debug.Print ""
Debug.Print Len(f)
r.FormulaLocal = f
Debug.Print "OK"
ss = "z" & ss
Next
Exit Sub
EH:
Debug.Print "Fail"
End Sub
And the last few lines of the result
257
OK
258
OK
259
Fail
It gets weirder: this code works
Sub Demo()
Dim s As String, ss As String, f As String
Dim r As Range
Dim i As Long
Set r = [A20]
s = "IF(A1=1,AAA999,FALSE)"
ss = "z"
r.FormulaLocal = "=" & s
On Error GoTo EH
For i = 1 To 60
Debug.Print ""
Debug.Print Len(r.FormulaLocal)
r.FormulaLocal = Replace(r.FormulaLocal, "AAA999", s)
Debug.Print "OK"
Debug.Print Len(r.FormulaLocal)
Next
Exit Sub
EH:
Debug.Print "Fail"
End Sub
What this does is builds up the formula up in sections, each as a valid Formula in its own right. Fails at about 1000 characters (1024 maybe).
Check the following page for the Excel limits:
https://support.office.com/en-nz/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
In particular, it says:
Length of formula contents - 8,192 characters
Internal length of formula - 16,384 bytes

Delete Matching Braces in Visual Studio

In Visual Studio I can jump from/to opening/closing brace with the Control+] shortcut.
Is there a shortcut that will allow me to delete both braces at once (maybe with a macro/extension)?
e.g.
foo = ( 1 + bar() + 2 );
When I am on the first opening brace I would like to delete it and its matching brace to get
foo = 1 + bar() + 2;
There isn't an inherent way to do this with Visual Studio. You would need to implement a macro in order for this.
If you choose the macro route you'll want to get familiar with the Edit.GoToBrace command. This is the command which will jump you from the current to the matching brace. Note it will actually dump you after the matching brace so you may need to look backwards one character to find the element to delete.
The best way to implement this as a macro is to
Save the current caret position
Execute Edit.GoToBrace
Delete the brace to the left of the caret
Delete the brace at the original caret position
Make a macro to press Ctrl+] twice and then backspace, then Ctrl+minus and a delete.
The Ctrl+minus moves the cursor back in time.
It's not quite as simple as JaredPar suggested but I'm no Macro expert either.
This works for (), {} and []
Sub DeleteMatchingBrace()
Dim sel As TextSelection = DTE.ActiveDocument.Selection
Dim ap As VirtualPoint = sel.ActivePoint
If (sel.Text() <> "") Then Exit Sub
' reposition
DTE.ExecuteCommand("Edit.GoToBrace") : DTE.ExecuteCommand("Edit.GoToBrace")
If (ap.DisplayColumn <= ap.LineLength) Then sel.CharRight(True)
Dim c As String = sel.Text
Dim isRight As Boolean = False
If (c <> "(" And c <> "[" And c <> "{") Then
sel.CharLeft(True, 1 + IIf(c = "", 0, 1))
c = sel.Text
sel.CharRight()
If (c <> ")" And c <> "]" And c <> "}") Then Exit Sub
isRight = True
End If
Dim line = ap.Line
Dim pos = ap.DisplayColumn
DTE.ExecuteCommand("Edit.GoToBrace")
If (isRight) Then sel.CharRight(True) Else sel.CharLeft(True)
sel.Text = ""
If (isRight And line = ap.Line) Then pos = pos - 1
sel.MoveToDisplayColumn(line, pos)
sel.CharLeft(True)
sel.Text = ""
End Sub
Then add a shortcut to this macro in VS.

How to replace multiple character in textbox keypress event using vb.net or c#?

When i am pressing any key in the textbox it will return "A" Character.
Code sample:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.KeyChar = "A"
End Sub
so i want to get multiple character. how to get?
code sample:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.KeyChar = "AB"
End Sub
the above coding not working. the e.keychar get only one character that is "A" only return. How to get all the characters.
You could append to the TextBox that "AB" and stop the event
var text = sender as TextBox;
text.Text = text.Text + "AB";
e.Handled = true;
Or you just can use TextChanged
Inside the text box's keypress event, you're substituting whatever key was captured (e.keychar) with the keyChar 'A'. If you want all keys to come through the text box, remove the code that substitutes 'A' for whatever key was pressed.
Comment out your line of code, in other words, and all keys will pass through to the text box.
Did you want to limit their entry to keys A-Z?

How do you jump to a particular row in a DataGridView by typing (a la Windows Explorer details view)

I have a .NET Winforms app in C# with a DataGridView that's read-only and populated with some number of rows. I'd like to have functionality similar to Windows Explorer's (and many other applications) details view for example.
I'd like the DataGridView to behave such that when it has focus if you start typing, the current row selection will jump to the row where the (string) value of cell 0 (i.e. the first column in the row) starts with the characters you typed in.
For example, if I have a DataGridView with 1 column and the following rows:
Bob
Jane
Jason
John
Leroy
Sam
If the DataGridView has focus and I hit the 'b' key on my keyboard, the selected row is now "Bob". If I quickly type in the keys 'ja', the selected row is Jane. If I quickly type in the letters 'jas', the selected row is Jane. If I hit the 'z' key, nothing is selected (since nothing starts with Z).
Likewise if Jane is currently selected and I keep typing the letter 'j', the selection will cycle through to Jason, then John, then back to Jane, each time I hit the 'j' key.
I've been doing some Googling (and "stackoverflowing" :-)) for awhile and cannot find any examples of this type of functionality. I have a rough idea in my head to do this via some sort of short lived timer thread, collecting the keystrokes on KeyPress events for the DataGridView, and selecting rows based on those collected keystrokes matching a Cells[0].Value.StartsWith() type of condition. But it seems like there has to be an easier way that I'm just not seeing.
Any ideas would be much appreciated. Thanks!
I have not seen any built-in functionality like this on DataGridView. I'm pretty sure you'll have to "roll your own".
Are you looking for easier ways to do the "partial search" for the strings?
I wrote a method that will select row letter typed, perhaps you can modify per your needs. The function is called in the KeysPress event handler of the DataGridView.
Method:
'user types letter in dgv, method will select the column starting with that letter if it exists or else next letter existing in dgv
Public Shared Sub GoToLetterTypedInDataGridView(ByVal dgv As DataGridView, ByVal columnName As String, ByVal columnPosition As Integer, ByVal letterTyped As Char)
Try
Dim dt As DataTable = dgv.DataSource
Dim letter As Char = letterTyped
Dim dv As DataView = New DataView(dt)
Dim hasCount As Boolean = False
While (Not hasCount)
dv.Sort = columnName
dv.RowFilter = columnName & " like '" & letter & "%'"
If dv.Count > 0 Then
hasCount = True
Dim x As String = dv(0)(columnPosition).ToString()
Dim bs As New BindingSource
bs.DataSource = dt
dgv.BindingContext(bs).Position = bs.Find(columnName, x)
dgv.CurrentCell = dgv(0, bs.Position)
Else
If letter = "z" Then
letter = "a"
ElseIf letter = "Z" Then
letter = "A"
Else : letter = Chr(Asc(letter) + 1)
End If
End If
End While
Catch ex As Exception
Dim stackframe As New Diagnostics.StackFrame(1)
Throw New Exception("An error occurred in routine, '" & stackframe.GetMethod.ReflectedType.Name & "." & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'." & Environment.NewLine & " Message was: '" & ex.Message & "'")
End Try
End Sub
Then to call:
Private Sub dgvNew_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles dgvNew.KeyPress
Try
If dgvNew.RowCount > 0 Then
GoToLetterTypedInDataGridView(dgvNew, "columnName", 0, e.KeyChar)
End If
Catch ex As Exception
Dim stackframe As New Diagnostics.StackFrame(1)
Throw New Exception("An error occurred in routine, '" & stackframe.GetMethod.ReflectedType.Name & "." & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'." & Environment.NewLine & " Message was: '" & ex.Message & "'")
End Try
End Sub

Categories