Determine initial text selection in DataGridView Cell - vb.net

A little question on use of DataGridView.
When I enter a Cell, text is all selected (in blue in the example). But I want to set the caret position at the right side of the text, for my user can delete the last character without clicking twice to place caret.
Have you some idea, because I don't find the good property...
I try to change behaviour when I enter in cell (edit with F2, in simple click), but test is always all select

Use DataGridView1.CellClick event and BeginEdit to allow casting Cell in textbox. After that set cursor position using textbox.select (from,Length) :
Private Sub cellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView1.BeginEdit(False)
Dim tbCell As TextBox = CType(DataGridView1.EditingControl, TextBox)
tbCell.Select(tbCell.Text.Length, 0)
End Sub

The functioning of DataGridView has always been a problem for me.
tuyau2poil, your answer was perfect for my first question. I integred the code in DataGridView_CellDoubleClick and it's OK. Thousand of thanks
For my second question, I think the way is on Jimy anwser:
Now I search, when I select the cell with 1 click and press "Del", to delete the last character. That works, but when I type a new character + Enter, that delete all the cell for write only the new charactere.
This is for my users have to modify only the last number of the serial number enter in cell...
"21000_001" to become "21000_002" with "Del" + key press "2" + Enter .
Actually I've got "21000_001" then "2"
I think solution is arount of DataGridView_EditingControlShowing
Thanks

Related

Textbox: missing first character typed on the TextBox

I have 2 text boxes to accept info but only one of them is need at the same time (One is for selection of the first letter and the other is for selection of any part of the name)
So I need to delete the text of the other textbox when the user start typing on the textbox
Something like this work:
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
TextBox1.Text = ""
End Sub
But there's a problem, the first character the user type is not displayed, I think is because the first character typed is used to launch the event.
Example:
TextBox1.Text = "A"
when the user click on TextBox2 and type michael the "m" is lost
I don´t want to use the "GotFocus" event because a simple TAB pulsed to jump from TextBox1 to another control can delete the text on TextBox1 when the user stop on TextBox2, even if he don´t want to type anything there
How can I manage this mistake?
Right answer:
From #Plutonix
"No user is going to expect this behavior and you will go mad trying to divine when to execute and when not to. A better thing might be to select the text on the Enter event - at least that has been seen before by users"

Populating a textbox, if another textbox is populated

i've got two textboxes in my form.
According to title, when I write something in one textbox (a random one), i need that at the same time, in the other textbox a text (given in the code) appears.
1 letter for 1 letter
1)Example with random text:
1 textbox ) How are you?
2 textbox) Let's just c
2)Example with random text:
1 textbox ) What is the aim of the project?
2 textbox) Let's just chill out for a mome
thanks so much
You will need to determine which responses you would like to which questions. However, the tackle to letter for letter problem. I would use the substring method and get the length of the current text.
Dim Response as String = "Hello World!"
Private Sub Text(ByVal..) Handles Textbox1.changed
Textbox2.text = Response.Substring(0, Textbox1.text.length)
End Sub
As the text changes in the textbox you are typing in, this will output the response corresponding to the length of the text input. Since the event is textbox.changed, each character entered will update the second textbox
You need to use an event based code here. I would use a loop per stroke of the key and make the event "Key Press." Write the code onto the textbox you are inserting data into.

I have a "bug/glitch" in my vb program need assistance

I have a program that replaces every letter of the alphabet with something new inside the same textbox by clicking a button then buy clicking the button it translates it back to original text.
However when i run this program, yes it does work as needed but not 100%
Because after i click the button to translate, (yes it shows translated)
If delete the translated text and type something new, it goes back to the old text.
(this does not happen if i click the button again, it shows the orignal text. But if i type something new it will translate it.)
Any suggestions?
This is a continuation of your other question. You will need to reset your Boolean variable when you type in your TextBox i.e.
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
bConverted = False
End Sub

Dynamic Textboxes

I have a gridview - GridView1.
I am adding rows to this Gridview1 dyanamically through code.
One of the columns is a dropdownlist that reads from a sqldatasource.
The textboxes, dropdownlists & sql datasources are all arrays.
If I change the value of the dropdownlist, it maintains its state even after the page reloads on any button click event.
This is ok.
However, the values of the textboxes are not maintained.
Say I enter "Hello World" in the textbox & click "Add" button, I want to collect the text value in dropdownlist(which I can read) & the value in textbox(which returns blank.)
Please suggest a method so that on add button click I can retrive the value I had typed in the textbox.
Each textbox has a unique id & I tried using the ID to get its value
eg
protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) handles add.click
{
Dim valueinText = gettext(1).text
}
now if I type "Hello World" in textbox: gettext(1),
Reults:
valueinText = ""
Thanks in advance
You will have to reset the values for unbound textboxes that get their value from code when you post back. In your Page Load Event...
If Page.IsPostback Then
'Code that uses dropdownlistAtoZ.SelectedValue
'to fill in correct value for Textboxes.
End If
I was trying to create the same textboxes on every page_load event. But this just erased their values & their client_ID kept changing.
Instead, I tried making all textboxes Shared and created them only once on add click event, added them to a Row (which is again shared) and on page_load if page.postback = true I just added the row to the table again. If you don't do so, the row will not be shown on reload.
This solved my problem(now the values entered in Textbox were not cleared like before).
I accessed the value by classname.textboxname(i).text.
Now the solution seems obvious, but I spent a few days trying to solve this.
Thanks!

While in edit mode, advance to next cell and stay in edit mode for VB.NET DataGridView?

I have a DataGridView in which one column has data that the user needs to align by adding spaces. For example, the first two rows might contain:
kumbu
kuimbiu
And the user needs to be able to line up the letters that match by adding spaces. Something like this:
ku mb u
kuimbiu
Now in order to do that with the DataGridView, the user must enter edit mode in the top cell, add spaces, hit enter, re-enter edit mode in the bottom cell, and then add spaces. Our users would like to be able to, while in edit mode in the top cell, hit the down arrow and advance to the second cell while staying in edit mode, saving clicks or F2 hits.
Is there a good way to do this? I have tried trapping the down arrow key press, leaving edit mode, advancing a cell, and then entering edit mode with the grid's BeginEdit method, but this does not do what I want.
Any ideas?
When leaving the cell capture the edit status in a class variable. When the user presses down or enter, the next cell will begin edit mode, but only if the previous cell was in edit mode. You can add additional logic if you want it to be based upon columns.
Private blnEditMode As Boolean = False
Private Sub dgv_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEnter
If blnEditMode Then
dgv.BeginEdit(False)
End If
End Sub
Private Sub dgv_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellLeave
blnEditMode = dgv.IsCurrentCellInEditMode
End Sub