|
In Winforms, a TextBox has a MultiLine Property as well as an AcceptsReturn property.
But when used in a DataGridView via the DataGridViewTextBoxColumn, there are no such properties.
So at the moment, my DataGridView has a label, informing users to press Shift+Enter for a new line.
There must be a better way to accept the Return key for a new line in a textbox within a DataGridView.
modified on Thursday, October 2, 2008 7:09 AM
|
|
|
|
|
Assuming a datagridview control named dgvRows:
Private Sub dgvRows_KeyDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles dgvRows.KeyDown
' Detect the Enter key
If e.KeyCode = Keys.Return Then
' Determine the currently selected column
Select Case dgvRows.SelectedCells(0).ColumnIndex
Case 0
' Determine the currently selected row
Select Case dgvRows.SelectedCells(0).RowIndex
Case 0
' Code to manipulate the selected cell at (0,0)
Case 1
' Code to manipulate the selected cell at (0,1)
'
'
Case 99
' Etc
End Select
Case 1
'
'
Case 99
End Select
End If
End Sub
|
|
|
|
|
Hey George
Thanks for the answer but that doesn't work, I've tried it with the Keypress event as well.
The problem is that that event only fires when the entire cell is selected, but if the focus is on the control inside the cell, - in this case, the TextBox - the event doesn't fire for any keys pressed.
If you got this to work I would appreciate some more help.
Kind Regards
|
|
|
|
|
I love a worthwhile challenge. It's taken all morning, but, I think I've got it. The key is overriding the ProcessCmdKey method in the DataGridView class so that the Enter key can be detected and Ctrl^Enter substituted.
Step 1 is to add an extended DataGridView to your project where the override takes place.
Public Class myDataGridView
Inherits DataGridView
Protected Overrides Function ProcessCmdKey( _
ByRef msg As System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys) _
As Boolean
'** When the Enter key is pressed in the textbox column,
'** substitute Ctrl^Enter
If keyData = Keys.Enter Then
SendKeys.SendWait("^{ENTER}")
Return True
Else
Return MyBase.ProcessDialogKey(keyData)
End If
End Function
End Class
Unfortunately, this means the DataGridView control will have to be completely built and positioned on the form at runtime to take advantage of the override. Here's the simple code I used for testing. You will have to flesh it out for your purposes.
Public Class Form1
Dim dgv As New myDataGridView
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
'** Add the datagridview to the form
Controls.Add(dgv)
dgv.Left = 10
dgv.Top = 10
'** Add a textbox column
Dim dgvCol As New DataGridViewTextBoxColumn
With dgvCol
.HeaderText = "Text Column"
.Name = "chdrTextCol"
.Width = 130
End With
dgv.Columns.Add(dgvCol)
dgvCol = Nothing
'** Add a row
dgv.Rows.Add()
End Sub
End Class
|
|
|
|
|
Hi
Thanks for all your trouble.
I tried your code, had to set the ".DefaultCellStyle.WrapMode = True".
But when I press enter, the focus remains on the current cell, and the cell becomes selected, instead of adding a line into the textbox.
It does however fire on the key event within the textbox, which is by far the closest I have come to a solution so far.
How would I go about substituting the Enter key with a Shift^Enter, instead of a Ctrl^Enter? Seeing that when using the datagridview, pressing Shift+Enter gives the desired result, but not pressing Ctrl+Enter.
I don't totally understand the "^{ENTER}" syntax and how it works...
|
|
|
|
|
|
Im new to VB.NET. Working on developing Windows Application using Visual Studio 2003 platform. I have a target from which I read data and show it in a datagrid. I need to convert this datagrid into a spreadsheet which can either be a MS .xls file or OpenOffice .xls file.
Thanx in advance
|
|
|
|
|
|
Ashfield is right, there are plenty of articles on this site and on the net, but here is one possible approach (which is working for me, while you read this):
1. make an excel file with some demo data and make it look the way you want your app to make it look.
2. save it as xml
3. open this xml file in vs2003, so you can see what it looks like
4. use some loops to write the values from your dataset and the xml tags into a big string.
5. write the string to an xls file with a StreamWriter
6. Bob's your uncle.
Because you wrote the file as an xml file, it can easily be opened with Excel, OpenOffice or any other program that can usually read Excel files.
Good luck.
My advice is free, and you may get what you paid for.
|
|
|
|
|
Thanx Ashfield and Johan for replying..
I got the problem fixed. What Iv done is that I write the data from Datagrid into CSV file format which then can be opened in both MS Office and Open Office Spreadsheet.
|
|
|
|
|
Hi
I am having a little bit of trouble understanding how to read property values from objects in the Active Directory that are not included in the standard DirectoryServices.AccountManagement namespace which is new in .NET 3.5 ..
At current this is the only reference I have which I am not following.. not a very good C# guy!
http://costoda.blogspot.com/2008/01/getting-information-from-active.html[^]
If someone could suggest some examples on how to do the same as that link above in VB instead that would be awesome!
Kind Regards,
Tim
|
|
|
|
|
Does anyone know anything on this topic? I don't want to sound impatient!
Tim
-------------------------
Visit my Blog:
http://tim.raimes.com.au/
|
|
|
|
|
|
That tool was really useful, especially since it was free as I have seen expensive tools that do similar things! Thanks for that.
Ok So this has helped me build the code into a VB Class.. I am stuggling with implementing the same class into my project.. as it might get confusing to go deeper into the actually code I am working on can someone provide a rough example of how to "get" and "set" values on custom properties constructed in a class. My memory needs refreshing..
Does that make sense?
Tim
-------------------------
Visit my Blog:
http://tim.raimes.com.au/
|
|
|
|
|
|
Hello. I did something that would be considered very stupid to any experienced programmer and now would like to revert to the code in my last successful build. I am able to run the last successful build, how can I move forward in editing the associated code? Is this possible? I am using Visual Basic 2008 Express. My frustration is growing!
- Craig
"That rug really tied the room together."
|
|
|
|
|
Sorry. Not unless you are using some form of source control where you can check out a past version. There are two kinds of people out there. Those that have already experienced what you're going through - and those who will.
Look into using subversion, or some other version control software.
If it's just a small change you need to see, you might can open the old build in Reflector and see if you can get what you need...
|
|
|
|
|
You might want to look at version control tools, such as subversion.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
hai all,
i am new to .net, i have been given a task to create a vb.net app To send an email from the application using smtp gateway. Can anybody help me with this, kindly reply me as soon as possible.
Thanks in advance.
|
|
|
|
|
take a look at the system.net.mail classes
there you can create a Mailmessage and send it with a smtpclient
(you will have to set the host of the smtpclient)
something like
imports system.net.mail
dim msg as new mailmessage(senderemail,recemail,subject,body)
dim client as new smtpclient
client.host="smtpserver"
client.send(msg)
(This is from memory so could be it doesn't compile but it should help you on your way)
|
|
|
|
|
can you please help me out in detail,
i am getting the error message as "Declaration expected", even after i declared them as
Dim senderemail As String
Dim recemail As String
Dim subject As String
Dim body As String
Dim msg As New MailMessage(senderemail, recemail, subject, body)
Dim client As New SmtpClient
Do i need to add any other classes to make it work
thank you for your reply.Kindly please guide me through this also.
Waiting for reply.
|
|
|
|
|
Do a search on this site or on google. You will find 100s of tutorials about this subject.
Any suggestions, ideas, or 'constructive criticism' are always welcome.
|
|
|
|
|
try typing the entire path to the class I seem to remeber having to do that too
dim msg as new system.net.mail.mailmessage
dim client as new system.net.mail.smtpclient
|
|
|
|
|
Thanks Tom, i was able to do it.
|
|
|
|
|
I tried to make a simple application using VB.NET that is able to import excel data into sql. I got some tutorial from Bytes Forum. I've tried to follow the instruction to check msexcl40.dll, it's there but I still got an error said that the program could not find installable ISAM. Can someone help me?
Private Function frmUploader_upload() As Boolean<br />
Dim strConnection As String<br />
Dim strCommand As String<br />
Dim excelConnection As System.Data.OleDb.OleDbConnection<br />
Dim excelCommand As System.Data.OleDb.OleDbCommand<br />
<br />
strConnection = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=Excel 8.0;Persist Security Info=False", Me.tbFile.Text)<br />
excelConnection = New System.Data.OleDb.OleDbConnection(strConnection)<br />
strCommand = "INSERT INTO [OBDC; Driver={SQL Server}; Server=" & Me.tbServer.Text & "; Database=" & Me.tbDatabase.Text & "; Trusted_Connection=yes].[Sl_Logproof_200311] SELECT * FROM [" & Me.cbSheet.Text & "$];"<br />
excelCommand = New System.Data.OleDb.OleDbCommand(strCommand, excelConnection)<br />
<br />
Try<br />
excelConnection.Open()<br />
excelCommand.ExecuteNonQuery() 'got error after this line<br />
Catch ex As Exception<br />
MsgBox(ex.Message)<br />
Return False<br />
Finally<br />
excelConnection.Close()<br />
End Try<br />
<br />
Return True<br />
End Function
Best Regard,
Indra Permana Rusli
|
|
|
|