VB.Net初心者です。
VB.netのフォーム上でPowerPointプレゼンテーションを表示するアプリを作っています。
- 最初の画面はメニュー画面で、プレゼンテーションの題名を表示したボタンが並んでいる。
- ボタンを押すとそのボタンに応じたプレゼンテーションが全画面で立ち上がる。
- プレゼンテーションは閲覧者が自分で操作し、閲覧できる。
- escキーを押すとプレゼンが終了し、メニュー画面に戻る。
- ボタンは50個程度作る必要がある。
これらの条件をVisualStudio2015を使い、Windowsフォームアプリケーションで以下の形でどうにか実現しました。(あちこちのWebページからのコピペのつぎはぎですが)
50個のボタンは動的に生成し、Cドライブ直下、「pptData」フォルダにある、(ボタン名).ppsxファイルを開く形です。
PowerPointの作業画面を見せたくないので「PPTApp.Top = 900」で画面の外に追い出しています。
Imports Microsoft.Office
Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
Public Class Form1
Public Sub New()
InitializeComponent()
FormBorderStyle = FormBorderStyle.None
WindowState = FormWindowState.Maximized
StartPosition = FormStartPosition.CenterScreen
Show()
End Sub
'ボタンコントロール配列のフィールドを作成 Private testButtons() As Button 'フォームのLoadイベントハンドラ Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load 'ボタンコントロール配列の作成 testButtons = New Button(50) {} 'ボタンコントロールのインスタンス作成し、プロパティを設定する SuspendLayout() Dim i As Integer For i = 1 To testButtons.Length - 1 'インスタンス作成 testButtons(i) = New Button 'ボタンプロパティ設定 testButtons(i).Name = "Button" + i.ToString() testButtons(i).Text = i.ToString '(ボタン色、サイズ等のプロパティ省略) 'イベントハンドラに関連付け AddHandler testButtons(i).Click, AddressOf testButtons_Click Next i 'フォームにコントロールを追加 Controls.AddRange(testButtons) ResumeLayout(False) End Sub Private Sub testButtons_Click(ByVal sender As Object,ByVal e As EventArgs) 'クリックされたボタンのNameをファイル名に Dim activeButton As New Button activeButton = CType(sender, Button) Dim PPTApp As PowerPoint.Application = New PowerPoint.Application PPTApp.Visible = True Dim PPTFileName As String = activeButton.Text & ".ppsx" Dim PPTFilePath As String = "C:\pptData\" & PPTFileName 'PowerPointを開く Dim PPTPresentations As PowerPoint.Presentations = PPTApp.Presentations PPTPresentations.Open(PPTFilePath, WithWindow:=Core.MsoTriState.msoTrue) 'PPT作業画面を画面の外で表示 PPTApp.Top = 900 End Sub
End Class
問題はボタンのTextで、現状だと整数の番号しか表示できず、任意のテキストを表示できないことになります。エクセルでリストを作り、csvファイルに出力したもの
1,題名1
2,題名2
3,題名3
・
・
・
から2次元配列を作り、題名を取得しようとして以下のコードを作りました(これもほとんどコピペですが)
Private Sub Form1_Load() Handles MyBase.Load 'csvファイル読み込み Dim strArr()() strArr = Test("C:\pptData\dataName.csv") End Sub Public Function Test(ByVal fileName As String) ' StreamReader の新しいインスタンスを生成する Dim cReader As New System.IO.StreamReader(fileName, System.Text.Encoding.Default) Dim textLines As New List(Of String()) ' 読み込んだ結果をすべて格納するための変数を宣言する Dim stResult As String = String.Empty ' 読み込みできる文字がなくなるまで繰り返す While (cReader.Peek() >= 0) ' ファイルを 1 行ずつ読み込む Dim stBuffer As String = cReader.ReadLine() ' 読み込んだものを追加で格納する Dim cols() As String cols = Split(stBuffer, ",") textLines.Add(cols) End While cReader.Close() Dim strArr()() As String strArr = textLines.ToArray Return strArr End Function
ここから、ボタンのTextプロパティに ボタン名 i に対応する strArrの値を代入する方法がどうしてもわかりません。どのようにすればよいでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。