If you've played games you might know what I mean. How the words are spelled out letter by letter instead of the whole text being displayed kinda like pokemon or some other game.
this is what I have so far:
Dim strTitle As String = " "
If IO.File.Exists("npcCraig.txt") = False Then
outfile = IO.File.CreateText("save.txt")
End If
infile = IO.File.OpenText("npcCraig.txt")
Do Until infile.Peek = -1
strTitle = infile.Read 'reads character or should at least
lblTitle.Text = lblTitle.Text + strTitle
System.Threading.Thread.Sleep(1000)
Loop
infile.Close()
outfile.Close()
It runs but form1 doesn't show up at all because of "System.Threading.Thread.Sleep(1000)".
I tried using that as a way to delay it but that didn't work obviously.
If you can could you also tell me how to put a break in it or something so that when the user presses a key the text loads completely and the player can keep going. I'm at a lost with that.
ANY HELP AT ALL WOULD BE A AMAZING!! My textbook and the rest of the internet was of no help
My best shot is this:
Public Class Form1
Dim M As Integer = 1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim T As String = "" + TextBox1.Text
Label1.Text = Label1.Text + Mid(T, M, 1)
M = M + 1
End Sub
'You can use any trigger here
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub
End Class
Related
I need to understand if there is a possibility, within VB.NET, to be able to read the columns of an Excel file and write them out to a CSV file in a different order.
In practice, the Excel file we are sent has 6 columns: "amount", "branch", stock "," proposal "," quantity "," type ". The company management system accepts the text file with the columns in a different order: "branch", "stock", "amount", "quantity", "type", "proposal". This creates a problem for me because when I go to convert it my ERP fails to recognize that the column is in a different position.
I arrive at the concrete question, I would like to have the possibility to read the columns and make it possible through a script to be able to position them according to the position I decide.
I tried this code for import and convert to txt, but I need another script:
Imports System.IO
Imports ExcelDataReader
Imports System.Text
Public Class Form1
Dim tables As DataTableCollection
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "(*.xls)|*.xls|(*.xls)|*.xlsx"}
If ofd.ShowDialog() = DialogResult.OK Then
txtFileName.Text = ofd.FileName
Using Stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(Stream)
Dim result As DataSet = reader.AsDataSet(New ExcelDataSetConfiguration() With {
.ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
.UseHeaderRow = True}})
tables = result.Tables
cboSheet.Items.Clear()
For Each table As DataTable In tables
cboSheet.Items.Add(table.TableName)
Next
End Using
End Using
End If
End Using
End Sub
Private Sub cboSheet_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSheet.SelectedIndexChanged
Dim dt As DataTable = tables(cboSheet.SelectedItem.ToString())
dgProposte.DataSource = dt
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim writer As TextWriter = New StreamWriter("C:\Users\antonio\Desktop\Prova.txt")
For i As Integer = 0 To dgProposte.Rows.Count - 2 Step +1
For j As Integer = 0 To dgProposte.Columns.Count - 1 Step +1
writer.Write(vbTab & dgProposte.Rows(i).Cells(j).Value.ToString() & vbTab & "")
Next
writer.WriteLine("")
Next
writer.Close()
MessageBox.Show("Dati Esportati")
End Sub
The tables that you get from importing the Excel sheet(s) have their column names set, and you can index the column by its name.
So, and with a little adjustment to factor out some methods:
Imports System.IO
Imports ExcelDataReader
Public Class Form1
Dim tables As DataTableCollection
Private Sub WriteToCsv(tableName As String, filename As String)
Dim columnWriteOrder = {"branch", "stock", "amount", "quantity", "type", "proposal"}
Using writer As TextWriter = New StreamWriter(filename)
Dim tbl = tables(tableName)
For i As Integer = 0 To dgProposte.Rows.Count - 2
Dim vals As New List(Of String)
For j As Integer = 0 To columnWriteOrder.Length - 1
Dim val = tbl.Rows(i).Item(columnWriteOrder(j)).ToString()
vals.Add(val)
Next
writer.WriteLine(String.Join(vbTab, vals))
Next
End Using
End Sub
Private Sub PopulateSheetNames()
cboSheet.Items.Clear()
For Each table As DataTable In tables
cboSheet.Items.Add(table.TableName)
Next
End Sub
Private Sub cboSheet_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSheet.SelectedIndexChanged
If cboSheet.SelectedIndex >= 0 Then
Dim tableName = cboSheet.SelectedItem.ToString()
Dim dt As DataTable = tables(tableName)
dgProposte.DataSource = dt
End If
End Sub
Private Sub bnLoad_Click(sender As Object, e As EventArgs) Handles bnLoad.Click
Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls", .InitialDirectory = "C:\temp"}
If ofd.ShowDialog() <> DialogResult.OK Then
Exit Sub
End If
txtFileName.Text = ofd.FileName
Using Stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(Stream)
Dim edsc = New ExcelDataSetConfiguration() With {
.ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
.UseHeaderRow = True}}
Dim result As DataSet = reader.AsDataSet(edsc)
tables = result.Tables
End Using
End Using
PopulateSheetNames()
End Using
End Sub
Private Sub bnSaveAsCsv_Click(sender As Object, e As EventArgs) Handles bnSaveAsCsv.Click
If cboSheet.SelectedIndex < 0 Then
MessageBox.Show("Please select a sheet name.", "No sheet name selected", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Dim sheetName = cboSheet.SelectedItem.ToString()
If Not String.IsNullOrEmpty(sheetName) Then
WriteToCsv(sheetName, "C:\temp\Prova.csv")
MessageBox.Show("Dati Esportati.", "Dati Esportati", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
I changed the names of the buttons because "Button1" and "Button2" are not descriptive.
(I set the .InitialDirectory of the OpenFileDialog because it was convenient for me.)
Why you don't map a DTO of your data table?
Public Class MioDto
Property campoUno As String
Property campoDue As String
'...ecc
End Class
and then you can fill a dto in a cicle or so...
Dim a As New MioDto() With {.campoUno="...", campoDue="..."}
or if you want you can use
https://github.com/AutoMapper/AutoMapper
When you have a Dto class filled you can use it for generate your txt with youor preferred order.
I am trying to make a program that utilises a textbox that has text options listed in it that can be clicked on.
As a textbox example:
[Selection:<1><2><3>]
so the user could then as example click on (over the text) <2> to select the 2nd option or <3> so select the 3rd option. The idea comes from the AutoCAD commands prompt which uses a similar system.
How would I achieve something like this in vb.net code (if its even possible)?
Try this:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
Dim SplitText As String() = TextBox1.Text.Split(CChar("<"), ">")
Dim SelectedText As String = GetSelectedText()
Dim Options As New List(Of String)
If Not SelectedText = "" Then
For i = 0 To SplitText.Length - 1
If IsNumeric(SplitText(i)) Then
Options.Add("<" & SplitText(i) & ">")
End If
Next
For i = 0 To Options.Count - 1
If SelectedText = Options(i) Then
'Put your code here if it is the current option in the loop equals the selected option.
'I added a messagebox just so you can see the current option.
MessageBox.Show("You selected option: " & Options(i))
End If
Next
End If
End Sub
Private Function GetSelectedText()
Dim CursorPosition As Integer = TextBox1.SelectionStart
Dim SelectedNumber As String = ""
Dim NumberLength As Integer = 0
If CursorPosition = 0 Or CursorPosition = TextBox1.Text.Length Then
Return ""
End If
Do Until Mid(TextBox1.Text, CursorPosition - NumberLength, 1) = "<"
NumberLength += 1
Loop
SelectedNumber = Mid(TextBox1.Text, CursorPosition - NumberLength, NumberLength + 1)
NumberLength = 0
CursorPosition += 1
Do Until Mid(TextBox1.Text, CursorPosition + NumberLength, 1) = ">"
NumberLength += 1
Loop
SelectedNumber &= Mid(TextBox1.Text, CursorPosition, NumberLength + 1)
If IsNumeric(SelectedNumber.Remove(0, 1).Remove(SelectedNumber.Length - 2, 1)) Then
Return SelectedNumber
Else
Return ""
End If
End Function
I put this inside of the textbox click event, and it works. I did not try putting the code in any other events. I assume that the textbox is named: TextBox1.
Here's a quick example showing how to build a "menu" with a LinkLabel:
Public Class Form1
Private menuItems() As String = {"cat", "dog", "fish"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim menu As String = "Selection: " & String.Join(", ", menuItems)
LinkLabel1.Text = menu
LinkLabel1.Links.Clear()
For Each item In menuItems
LinkLabel1.Links.Add(menu.IndexOf(item), item.Length)
Next
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Dim i As Integer = LinkLabel1.Links.IndexOf(e.Link)
Label1.Text = menuItems(i)
End Sub
End Class
Output:
Its an assignment for a high school class, and I've tried so many things and looked up a lot of things, and just can't get it! The assignment is to make a magic word, whatever the user wants it to be. It's confusing but I want to learn! Any suggestions would be great! I have tried what is in the code below, but I don't know how to specify to add it to the beginning of a label, the assignment is to have a label, and have buttons that are able to add a character in a textbox to the beginning, middle, and end of a label. This is due wednesday 10/20, so please if you know anything about visual basic your help would be greatfully appreciated. thanks!
Here is what I have tried! It only adds a character of a string once to the label, but not again, this is the only code I tried to add to the beginning but have not yet tried to add to the middle and end.
Dim MagicLetter As String
Dim NewString As String
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
MagicLetter = TextBox1.Text
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NewString = Len(Label2.Text)
NewString = Mid(MagicLetter, 1, 0)
NewString = MagicLetter.Insert(1, 0)
If MagicLetter = TextBox1.Text Then
NewString = Mid(MagicLetter, 1, 1)
End If
Label3.Text = "Magic Word: " & MagicLetter
NewString = MagicLetter & Label2.Text
The problem lies here
NewString = Len(Label2.Text)
NewString = Mid(MagicLetter, 1, 0)
NewString = MagicLetter.Insert(1, 0)
What you do here is you write 3 times into the same variable NewString so in the end only the last value NewString = MagicLetter.Insert(1, 0) is in the variable because the 2 before got overwritten by the next one. So these three lines still do the same if you delete the first 2.
Then you don't need any global variables:
Dim MagicLetter As String
Dim NewString As String
You can do it with local variables inside the Button1_Click procedure. Always use local variables over global ones if you can.
Also you don't need the TextBox1_TextChanged event because you are not interested in every change of this text box. You only want to know its content when you click the button.
So we can do everything in the Button1_Click procedure
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OriginalText As String
OriginalText = Label3.Text ' here we get the text from the label
Dim MagicLetter As String
MagicLetter = TextBox1.Text ' here we get the magic letter from the textbox
Dim NewText As String
NewText = OriginalText ' our new text should be created from the original text
' now we add the magic letter infront
NewText = MagicLetter & NewText
' now we add the magic letter in the end
NewText = NewText & MagicLetter
' now we add the magic letter in the middle
Dim TextLength As Long
TextLength = Len(NewText) ' here we get the length of our text (we need to split it in the middle)
Dim LeftPart As String
LeftPart = Mid(NewText, 1, CLng(TextLength / 2)) ' here we get the left part of the text
Dim RightPart As String
RightPart = Mid(NewText, Len(LeftPart) + 1) ' here we get the right part of the text
' now we add the magic letter between the left and right part
NewText = LeftPart & MagicLetter & RightPart
' finall we write the new text into our label
Label3.Text = NewText
End Sub
Public Class FormMagicWord
Private Function GenerateMagicWord(MagicLetter As Char, Type As String)
'Declare the MagicWord as the label, which is set as just "Magic" in the designer
Dim MagicWord As String = LblMagicWord.Text
'Use a case statement (which is just a cleaner if/else if/else)
Select Case Type
Case "Beginning"
'Combine the MagicLetter and the MagicWord into the MagicWord string.
MagicWord = MagicLetter & MagicWord
Case "Middle"
'Set the initial "midpoint" as 0 in-case the label is empty.
Dim MidPoint As Integer = 0
'Get the middle of the MagicWord string if its length > 0. I used Math.Floor() which will round down to the nearest whole number, so if the length was 9: 9/2 = 4.5 it would round down to 4.
'Alternatively you can use Math.Ceiling() which does the opposite, it rounds up to the next whole number, so if the length was 9: 9/2 = 4.5 it would round up to 5.
'It's cast as an integer (CInt) because we only care about whole numbers for this
If MagicWord.Length > 0 Then
MidPoint = CInt(Math.Floor(MagicWord.Length / 2))
End If
'Insert the MagicLetter at the midpoint of the MagicWord string.
MagicWord = MagicWord.Insert(MidPoint, MagicLetter)
Case "End"
'Combine the MagicWord and the MagicLetter into the MagicWord string.
MagicWord = MagicWord & MagicLetter
Case Else
'Not used, but this would be the "else" equivalent for a Select/Case/Switch statement
End Select
'Return the MagicWord string
Return MagicWord
End Function
'I've changed the handler to manage all three buttons: (BtnBeginning, BtnMiddle, BtnEnd) because the logic is the same for all of them.
'I've also changed the default sender object to Btn as a button, so it explicitly knows what type of control we're handling
Private Sub BtnBeginning_Click(Btn As Button, e As EventArgs) Handles BtnBeginning.Click, BtnMiddle.Click, BtnEnd.Click
'Get the magic letter as a single character, which is all we need.
'The designer also has the max length of the TxtMagicLetter textbox set to 1
Dim MagicLetter As Char = TxtMagicLetter.Text
'Call the GenerateMagicWord function passing the arguments of the letter and the text of the button (Beginning, Middle, End) which will run through the select statement to determine how to format the string
Dim MagicWord As String = GenerateMagicWord(MagicLetter, Btn.Text)
'Finally, set the MagicWord label as the returned string
LblMagicWord.Text = MagicWord
End Sub
End Class
Here's the designer code as well so you can just copy/paste the buttons/textbox/labels.
Here's how to access the code behind the design:
View Designer Code in Visual Studio 2010
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class FormMagicWord
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TxtMagicLetter = New System.Windows.Forms.TextBox()
Me.BtnBeginning = New System.Windows.Forms.Button()
Me.BtnMiddle = New System.Windows.Forms.Button()
Me.BtnEnd = New System.Windows.Forms.Button()
Me.LbLMagicLetter = New System.Windows.Forms.Label()
Me.LblMagicWordLabel = New System.Windows.Forms.Label()
Me.LblMagicWord = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'TxtMagicLetter
'
Me.TxtMagicLetter.Location = New System.Drawing.Point(249, 12)
Me.TxtMagicLetter.MaxLength = 1
Me.TxtMagicLetter.Name = "TxtMagicLetter"
Me.TxtMagicLetter.Size = New System.Drawing.Size(246, 20)
Me.TxtMagicLetter.TabIndex = 0
'
'BtnBeginning
'
Me.BtnBeginning.Location = New System.Drawing.Point(12, 38)
Me.BtnBeginning.Name = "BtnBeginning"
Me.BtnBeginning.Size = New System.Drawing.Size(157, 33)
Me.BtnBeginning.TabIndex = 1
Me.BtnBeginning.Text = "Beginning"
Me.BtnBeginning.UseVisualStyleBackColor = True
'
'BtnMiddle
'
Me.BtnMiddle.Location = New System.Drawing.Point(175, 38)
Me.BtnMiddle.Name = "BtnMiddle"
Me.BtnMiddle.Size = New System.Drawing.Size(157, 33)
Me.BtnMiddle.TabIndex = 2
Me.BtnMiddle.Text = "Middle"
Me.BtnMiddle.UseVisualStyleBackColor = True
'
'BtnEnd
'
Me.BtnEnd.Location = New System.Drawing.Point(338, 38)
Me.BtnEnd.Name = "BtnEnd"
Me.BtnEnd.Size = New System.Drawing.Size(157, 33)
Me.BtnEnd.TabIndex = 3
Me.BtnEnd.Text = "End"
Me.BtnEnd.UseVisualStyleBackColor = True
'
'LbLMagicLetter
'
Me.LbLMagicLetter.AutoSize = True
Me.LbLMagicLetter.Location = New System.Drawing.Point(172, 12)
Me.LbLMagicLetter.Name = "LbLMagicLetter"
Me.LbLMagicLetter.Size = New System.Drawing.Size(66, 13)
Me.LbLMagicLetter.TabIndex = 4
Me.LbLMagicLetter.Text = "Magic Letter"
'
'LblMagicWordLabel
'
Me.LblMagicWordLabel.AutoSize = True
Me.LblMagicWordLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LblMagicWordLabel.Location = New System.Drawing.Point(8, 141)
Me.LblMagicWordLabel.Name = "LblMagicWordLabel"
Me.LblMagicWordLabel.Size = New System.Drawing.Size(112, 24)
Me.LblMagicWordLabel.TabIndex = 5
Me.LblMagicWordLabel.Text = "Magic Word"
'
'LblMagicWord
'
Me.LblMagicWord.AutoSize = True
Me.LblMagicWord.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LblMagicWord.Location = New System.Drawing.Point(135, 141)
Me.LblMagicWord.Name = "LblMagicWord"
Me.LblMagicWord.Size = New System.Drawing.Size(0, 24)
Me.LblMagicWord.TabIndex = 6
Me.LblMagicWord.Text = "Magic"
'
'FormMagicWord
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.LblMagicWord)
Me.Controls.Add(Me.LblMagicWordLabel)
Me.Controls.Add(Me.LbLMagicLetter)
Me.Controls.Add(Me.BtnEnd)
Me.Controls.Add(Me.BtnMiddle)
Me.Controls.Add(Me.BtnBeginning)
Me.Controls.Add(Me.TxtMagicLetter)
Me.Name = "FormMagicWord"
Me.Text = "Magic Word"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TxtMagicLetter As TextBox
Friend WithEvents BtnBeginning As Button
Friend WithEvents BtnMiddle As Button
Friend WithEvents BtnEnd As Button
Friend WithEvents LbLMagicLetter As Label
Friend WithEvents LblMagicWordLabel As Label
Friend WithEvents LblMagicWord As Label
End Class
Dim magicWord As String = "abcdef"
Label1.Text = $"{TextBox1.Text}{String.Concat(magicWord.Take(magicWord.Length \ 2))}{TextBox1.Text}{String.Concat(magicWord.Skip(magicWord.Length \ 2))}{TextBox1.Text}"
1abc1def1
When magicWord = "abcdefg" (odd number of characters),
1abc1defg1
the inserted string is not quite in the middle, but the requirement is not clear in your question.
This doesn't include validation such as TextBox.Text should be a char, and magic word length being odd or even. Integer division \ is used to pass an integral number of characters to Take and Skip.
This may not be usable since it doesn't utilize Mid or Len, but I posted it for posterity
NewString = Len(Label2.Text)
You have a problem here Len(String) returns an Integer and you have already declared NewString As String.
NewString = Mid(MagicLetter, 1, 0)
On the very next line, you throw away the value of NewString and assign something else. This is a bit silly because Mid(string, StartIndex, Length) Since the length is 0 this gets you and empty string. Another way this is confusing is the second parameter, 1. In .net indexes for things start at 0 but this "index" starts with 1. Let's just move away from old vb6 methods and use the .net improvements.
NewString = MagicLetter.Insert(1, 0)
Again another assignment. NewString is getting tired. A funny thing about String in .net is that it is immutable (not able to be changed). Under the hood the compiler throws out the old string and creates an entirely new one everytime a string changes. Another problem with this line is the second parameter of Insert takes String. 0 is not a String, it is an Integer.
The backward slash indicate Integer Division.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MagicLetter As String = TextBox1.Text
Dim MagicWord = "antiestablishmentarianism"
Label1.Text = MagicWord & MagicLetter
Label2.Text = MagicLetter & MagicWord
Dim WordMiddle = MagicWord.Length \ 2
Label3.Text = MagicWord.Insert(WordMiddle, MagicLetter)
End Sub
I'm starting a ne project, in VB. And I have a problem. So maybe I don't understand the logic - can you explain it to me?
In my function Feuil1_BeforeDoubleClick i would like to wait for Button1_Clickto end.
But i don't know how to achieve this.
Here's the relevant code:
My Sheet1 :
Imports System.Threading.Tasks
Imports Microsoft.Office.Interop.Excel
Public Class Feuil1
Friend actionsPane1 As New ActionsPaneControl1
Public list As String
Public Sub Feuil1_BeforeDoubleClick(Target As Range, ByRef Cancel As Boolean) Handles Me.BeforeDoubleClick
If Target.Column <> 1 Then
If Target.Row = 16 Then
Globals.ThisWorkbook.ActionsPane.Controls.Add(actionsPane1)
Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = True
'marche pas SendKeys.Send("{ESC}")
'
'wait here for the end of Button1_click
Target.Value = list
list = ""
End If
End If
MsgBox("doubleclick end")
End Sub
End Class
And there is my actionpane1 :
Public Class ActionsPaneControl1
Friend Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer
Dim itemChecked As Object
Const barre As String = " / "
For Each itemChecked In CheckedListBox1.CheckedItems
Globals.Feuil1.list = Globals.Feuil1.list + itemChecked.ToString() + barre
Next
' Boucle pour reset la list
For i = 0 To (CheckedListBox1.Items.Count - 1)
CheckedListBox1.SetItemChecked(i, False)
Next
Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = False
End Sub
End Class
Example taken from https://www.daniweb.com/programming/software-development/threads/139395/how-to-check-if-a-button-was-clicked . Credit is given.
Basicly declare a variable at form level and then set it to true whenever the button is clicked. Reset it when appropriate
Dim bBtnClicked As Boolean = False
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If bBtnClicked = True Then
MessageBox.Show("This button is clicked already ....")
Else
MessageBox.Show("This button is clicked First time ....")
End If
bBtnClicked = True
End Sub
Alternatively whatever it is that you want to happen after the button is clicked, just put that code in the handler for the button-click event.
So i think about the problem, and it seems I didn't understand my function doubleclick, was not working after the double click but before ! that was my mistake.
So i change my function to detect, the change of selection in my sheet.
Then i call my action pane.
And work with the event of the button of the action pane.
There is the entire code ( maybe because i don't explain me correctly)
my Sheet1.vb :
Imports Microsoft.Office.Interop.Excel
Public Class Feuil1
Friend actionsPane1 As New ActionsPaneControl1
Public list As String
Public cell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Handles Me.SelectionChange
If Target.Column <> 1 Then
If Target.Row = 16 Then
Globals.ThisWorkbook.ActionsPane.Controls.Add(actionsPane1)
Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = True
cell = Target
End If
End If
End Sub
End Class
and my actionpanecontrol1.vb :
Public Class ActionsPaneControl1
Friend Button1Click = False
Friend Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer
Dim itemChecked As Object
Const barre As String = " / "
For Each itemChecked In CheckedListBox1.CheckedItems
Globals.Feuil1.list = Globals.Feuil1.list + itemChecked.ToString() + barre
Next
' Boucle pour reset la list
For i = 0 To (CheckedListBox1.Items.Count - 1)
CheckedListBox1.SetItemChecked(i, False)
Next
Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = False
Button1Click = True
Globals.Feuil1.cell.Value = Globals.Feuil1.list
Globals.Feuil1.list = ""
End Sub
End Class
Thanks a lot for all your reply
I'm having a problem with the following when I try to 'Clear' the textboxes in my program. It's a beginner's class so I should be able to answer fix the error with what I've learned so far. The program is basically calculating Interest rate and the growth in capital whenever I press NextYear. When I press the "Clear" button it shows me the error and points me at the syntax capital = CDbl(txtInitialCap.Text) under txtRate_TextChangedevent.
Private year As Integer
Private capital As Double
Private Sub btnNextYear_Click(sender As Object, e As EventArgs) Handles btnNextYear.Click
Dim interestAmount, interestRate As Double
'
' Assign value to InterestRate variable from text property
'
interestRate = CDbl(txtRate.Text)
'
' Calculate new values for year, interest amount and capital
'
year = year + 1
interestAmount = capital * interestRate
capital = capital + interestAmount
'
' Assign the values to the textboxes
'
txtYear.Text = year
txtInterest.Text = interestAmount
txtCapital.Text = capital
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtInitialCap.Text = vbNullString
txtRate.Text = vbNullString
txtYear.Text = vbNullString
txtInterest.Text = vbNullString
txtCapital.Text = vbNullString
End Sub
Private Sub txtInitialCap_LostFocus(sender As Object, e As EventArgs) Handles txtInitialCap.LostFocus
'
' Reinitialise the capital variable since probably it was changed
'
capital = CDbl(txtInitialCap.Text)
End Sub
Private Sub txtInitialCap_TextChanged(sender As Object, e As EventArgs) Handles txtInitialCap.TextChanged
'
' Reinitialise the year variable and the textboxes
'
year = 0
txtYear.Text = vbNullString
txtInterest.Text = vbNullString
txtCapital.Text = vbNullString
End Sub
Private Sub txtRate_TextChanged(sender As Object, e As EventArgs) Handles txtRate.TextChanged
year = 0
capital = CDbl(txtInitialCap.Text)
txtYear.Text = vbNullString
txtInterest.Text = vbNullString
txtCapital.Text = vbNullString
End Sub
I know that this is an old post. I'll answer in case someone else finds this. In txtRate_TextChanged, you need to check that
txtInitialCap.Text != ""
in case that text is "", you can't convert it to double