|
Shhh!
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
|
|
|
|
|
|
Thanks for the help guys
|
|
|
|
|
I have a project that I'm working on and my boss wants me to add the buttons to the tool strip at runtime based on different things (those I'll worry about later). I've gotten pretty far on my own but am now stuck on the "click event". I can get the buttons to display but can't get them to do anything once they are there other than look good. I'm in the testing stage now so it's nothing impressive but as I said it works up to the point of displaying in the manner I need it to.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Create a button for the tool strip
Dim btnSomething As New ToolStripButton
btnSomething.Text = "Test 1"
btnSomething.Image = Image.FromFile("F:\VisualBasic\ToolStripTest\images\eventlog.ico")
btnSomething.TextImageRelation = TextImageRelation.ImageAboveText
btnSomething.ToolTipText = "This is Just a Test"
Dim btnSomething2 As New ToolStripButton
btnSomething2.Text = "Test 2"
btnSomething2.Image = Image.FromFile("F:\VisualBasic\ToolStripTest\images\eventlog.ico")
btnSomething2.TextImageRelation = TextImageRelation.ImageAboveText
btnSomething2.ToolTipText = "This is Just a Test again"
'Display a separator with followed by the two created buttons followed by another separator
ToolStrip1.Items.Add(New System.Windows.Forms.ToolStripSeparator)
ToolStrip1.Items.Add(btnSomething)
ToolStrip1.Items.Add(btnSomething2)
ToolStrip1.Items.Add(New System.Windows.Forms.ToolStripSeparator)
End Sub
Thank you all in advance...
modified on Friday, April 30, 2010 12:37 PM
|
|
|
|
|
AddHandler btnSomething.Click, AddressOf ClickRoutine 'add handler here
where click Routine is some predefined subroutine
Thanks for taking the time, now go away and grow up and return in a newer, more polite and less shouty and ignorant form. - Dalek Dave
|
|
|
|
|
I have added
btnSomething.ToolTipText = "This is Just a Test"
AddHandler btnSomething.Click, AddressOf ClickRoutine
(included the line above it to show location)
and I get the following error message:
Method 'Public Sub ClickRoutine()' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'
if I make it ClickRoutine() I get:
'AddressOf' operand must be the name of a method (without parentheses).
I did create the following:
Sub ClickRoutine()
MessageBox.Show("It works")
End Sub
Now if I add a second button to the form I can call that no problem:
AddHandler btnSomething.Click, AddressOf Button2_Click
|
|
|
|
|
I seem to have answered by own question in a round about way. I have made the following changes and it now works:
btnSomething.ToolTipText = "This is Just a Test"
AddHandler btnSomething.Click, AddressOf ClickRoutine
and
Private Sub ClickRoutine(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("It works")
End Sub
by adding the ByVal sender As System.Object, ByVal e As System.EventArgs that seems to allow it to work. I guess we learn something new every day...
|
|
|
|
|
Hi
You need to add a delegate to the ToolStripButton to handle it's Click event. First create the routines that you want to fire when the user clicks on the button (remember that it must have the same signature as a 'normal' click event handler). For example in your app you could create 2 routines like so
Private Sub btnSomething_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(CType(sender, ToolStripButton).ToolTipText & ", " & CType(sender, ToolStripButton).Text)
End Sub
Private Sub btnSomething2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(CType(sender, ToolStripButton).ToolTipText & ", " & CType(sender, ToolStripButton).Text)
End Sub
Then when you create the buttons add your delegates like this
Dim btnSomething As New ToolStripButton
btnSomething.Text = "Test 1"
btnSomething.TextImageRelation = TextImageRelation.ImageAboveText
btnSomething.ToolTipText = "This is Just a Test"
AddHandler btnSomething.Click, AddressOf btnSomething_Click
Dim btnSomething2 As New ToolStripButton
btnSomething2.Text = "Test 2"
btnSomething2.TextImageRelation = TextImageRelation.ImageAboveText
btnSomething2.ToolTipText = "This is Just a Test again"
AddHandler btnSomething2.Click, AddressOf btnSomething2_Click
ToolStrip1.Items.Add(New System.Windows.Forms.ToolStripSeparator)
ToolStrip1.Items.Add(btnSomething)
ToolStrip1.Items.Add(btnSomething2)
ToolStrip1.Items.Add(New System.Windows.Forms.ToolStripSeparator)
then you should get the click events to fire. See thread 2 or 3 down for further example
Happy Coding
|
|
|
|
|
Ok so this is now working great, and I really do appreciate the help. Of three forums this is the only one I've gotten a response on.
Now I've been trying to figure out how to get this to work with a module. The "buttons" need to be available to different forms in the project thus the reason for adding them at run time.
I've tried:
ToolStrip1.Items.Add(Call Buttons(btnSomething2))
as I have used Call 'sub in the module'('variable') in the past and it has worked. Though when I try it in this respect I get "Expression Expected" where Call is.
This is the Module I created:
Module ToolStripBtns
Sub Buttons()
Dim btnSomething As New ToolStripButton
btnSomething.Text = "Test 1"
btnSomething.Image = Image.FromFile("F:\VisualBasic\ToolStripTest\images\eventlog.ico")
btnSomething.TextImageRelation = TextImageRelation.ImageAboveText
btnSomething.ToolTipText = "This is Just a Test"
AddHandler btnSomething.Click, AddressOf btnSomething_Click
End Sub
Private Sub btnSomething_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(CType(sender, ToolStripButton).ToolTipText & ", " & CType(sender, ToolStripButton).Text)
End Sub
End Module
I trimmed out some of the "junk" to make it shorter...
Thank you again!
|
|
|
|
|
Hi
Try this
Module ToolStripBase
Public Enum ButtonFunction
Something1
Something2
End Enum
Public Function CreateButton(ByVal ButtonImage As Image, ByVal ButtonText As String, ByVal ButtonToolTipText As String, ByVal MyFunction As ButtonFunction) As ToolStripButton
Dim btnSomething As New ToolStripButton
btnSomething.Text = ButtonText
btnSomething.Image = ButtonImage
btnSomething.TextImageRelation = TextImageRelation.ImageAboveText
btnSomething.ToolTipText = ButtonToolTipText
Select Case MyFunction
Case ButtonFunction.Something1
AddHandler btnSomething.Click, AddressOf SomethingClick
Case ButtonFunction.Something2
AddHandler btnSomething.Click, AddressOf Something2Click
End Select
Return btnSomething
End Function
Private Sub SomethingClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(CType(sender, ToolStripButton).ToolTipText & ", " & CType(sender, ToolStripButton).Text)
End Sub
Private Sub Something2Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(CType(sender, ToolStripButton).ToolTipText & ", " & CType(sender, ToolStripButton).Text)
End Sub
End Module
Then from each form you can create each button as it is needed like this
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyToolStrip As New ToolStrip
Dim Button1 As ToolStripButton = ToolStripBase.CreateButton(Nothing, "Test 1", "This a test", ButtonFunction.Something1)
Dim Button2 As ToolStripButton = ToolStripBase.CreateButton(Nothing, "Test 2", "This another test", ButtonFunction.Something1)
MyToolStrip.Items.Add(Button1)
MyToolStrip.Items.Add(Button2)
MyToolStrip.Parent = Me
End Sub
Happy Coding
|
|
|
|
|
This so far has been the best idea/method for doing what I need to. I'm just wondering is there a way to pass a value from the Form to the module? For example I created a copy button that would be used to copy selected text. I have another version with a Class that has the copy function locally to the form and works fine, but I can't see a way to get that working from the Module. I'm basically typing about 90% of the code on each form which isn't really much of a savings. I understand that this part of the project is a bit out of my league and I do appreciate your help. Thank you again!!
|
|
|
|
|
Hello every one. My objective is to read data from .xls and process in VB.net application. I do have a desktop application which even reads data from a remote DB. I have the following clarification and issues
- The application will be installed on different systems and we cannot make it sure that all the systems should have MS Office installed and that too a particular version
- Can’t we somehow access data from and to excel files without using Ms Excel components, so that the application should also work on a system where MS Excel is not installed also
Regards
Nishkarsh
|
|
|
|
|
You can read/write Excel sheets using OleDb, an example of which you can find here[^]. I'm not quite sure, but I don't think you need Excel installed at all in order to use this method. ANy other method requires Excel to be installed.
|
|
|
|
|
Hi Dave,
Thanks for the input. I will be trying it now to read from Excel sheet and write to it. Can you please provide me with some samples in VB.net.
Regards
Nishkarsh
modified on Monday, May 3, 2010 8:40 AM
|
|
|
|
|
|
Hi Nishkarsh
I have used a free library from CarlosAg for a while and it is really brilliant, as it seems to cover all bases. Here is the link CarlosAg[^]. He also has other links to other free Excel libraries out there. Definitely worth a visit.
Happy Coding
|
|
|
|
|
Thanks a lot for the response. I will try it today
|
|
|
|
|
Hi Zimvbcoder
I am trying to read the help manual which I downloaded along with the DLL. Can you please send me any example code (Vb.net if possible).
Regards
Nishkarsh
modified on Monday, May 3, 2010 6:57 AM
|
|
|
|
|
Hi Nishkarsh,
Carlos has a code generator on his site which will generate the code for any excel spreadsheet. This can be found here CarlosAg Code Generator[^]. If you design a sample spreadsheet, then you can use the code generator to generate the code for any .Net language. Most often you can use the code straight off, just ensuring the correct values go in the right places, otherwise a bit of refactoring is all that is needed.
Happy Coding
|
|
|
|
|
Hi Zimvbcoder,
It is great. Works really good. One of the easiest thing to output a Excel sheet.
The only other thing which i want to know is how to read from a Excel sheet.
Regards
Nishkarsh
|
|
|
|
|
Hi Zimvbcoder,
The lib is really great for making good looking XLS file output. I am having some issues and clarifications, can you please help me out.
1. Dose the lib provides any method to read from the excel sheet?
2. I read data from DB and used the codes given to create xls, is looks fine but when i went to save as option i realized it that the format is XML. Am i doing some thing wrong or it gives output in XML format only. Coz when i try to read the data from the generated file (in VB.net using OLEDB) i get file format error. And i Have to manually go and re-save the file in XLS format.
Any idea?
Regards
Nishkarsh
|
|
|
|
|
Hi Nishkarsh
I am afraid the library is only for generating reports, and cannot read Excel spreadsheets. The only way I know of reading Excel spreadsheets without Excel installed on the machine is by using ADO.Net and connecting to the spreadsheet like a database. Unfortunately, I have never used this method and can't give any advice. As the file generated by CarlosAg library is in Spreadsheet XML format, what I do is if I want to read or add to the file, I treat it as a purely XML file and then do my business with XML.
As far as saving the file generated, the format of the generated file is Spreadsheet XML, but you can save this as an .xls file and Excel opens this file without an issue, just warning you that the file format is not correct the first time you open it. If you ignore that and open it, then you can save as any format you like from Excel itself. What I normally do is use a FileSaveDialog to get the user to select the name of the file and I use a filter to make sure it only selects .xls files like this :
Dim sfdReport As New SaveFileDialog
sfdReport.Filter = "Excel Files (*.xls)|*.xls"
sfdReport.ShowDialog()
book.save(sfdReport.FileName)
That way the file is only saved as .xls.
Happy Coding
|
|
|
|
|
Hi zimvbcoder,
Thanks for the input, i am doing the same as of now.
I will tel you what exactly i wanted to achieve. I have a application which has a long list of items and values (some thing like price list) which the client wants to update on his own from time to time. He is asking that give a provision so that the list is available in Excel he can update it and then the list can be updated in the system.
SO i am creating a application in which i am using CarlosAg to read from DB and create a excel file. Which client will update and then i will read it from the excel using OLEDB and update the DB (I know this is not the best way of updating a DB but client is finally the god)
Now CarlosAg gives output in XML format and even if i name it as .XLS then also OLEDB dose not read it. So i have to tel the client that please go to file menu and "Save As" .xls before he dose the step 2. This makes the solution not fully automated and chances are they mis out that step and we end up it issues...
Please do let me know if there is some thing else which we can do about it...
Regards
Nishkarsh
|
|
|
|
|
Hi Nishkarsh.
Personally what I would do is to include a place in the app where the client can update the products, rather than saving to Excel and then reading from the spreadsheet. This way you cut out the Excel part altogether. If the customer really insists on a spreadsheet, then I would tell him that Excel needs to be installed on the machine, and use the Microsoft Interop Assemblies to read and write to Excel format. I agree with you that relying on the client to do anything in the right order is just going to cause headaches, and I do not know of a library that can read Excel files without a reference to the Excel dll. Sorry I can't be any more help.
|
|
|
|
|
Hi zimvbcoder,
I need a bit of help in reading .xml file. I was able to convince my client to have xml instead of xls as both can oven in excel interface. This will avoid 1 manual steep to of saving .xml as .xls. No i tried reading data from .xml files as i am new to it i am having some issues.
I am using XmlTextReader class for the same and looking for XmlNodeType.Text, it seams to be working but it dives me some data before and after the data life author and versions also. Which makes my work harder as i only want the files which displayed in the excel interface when i use it to open the .xml file.. Any idea?
|
|
|
|