Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
VSTO も当然ながら、Win フォームのコンポーネントを作成して、ドキュメントや作業ウィンドウから Win フォームを起動することができます。
作業ウィンドウで操作してしまえばよい部分はありますが、やはり、領域の問題などもあり、どうしても Win フォームを呼び出してから、値を取得しドキュメント上で利用するケースもあるかと思います。
このあたりは .NET の開発者であれば、あまり問題はないのかなと思いますが、.NET の開発がこれからという方にとっては不明な点があるかと思います。
今回は VSTO のドキュメント上から、Win フォームを呼び出して、DataGridView のデータをドキュメントに渡す方法についてご紹介したいと思います。
動き的には以下のような感じです。
手順:
1. コンテンツコントロールを3つ配置した Word ドキュメントを用意します。
2. Word 2007 ソリューションドキュメント上にボタンコントロールを配置します。
3. ソリューションで Windows フォームを作成し DataGridView を作成します。
※それぞれのボタンにDiaLog Result プロパティの値を設定します。
4. Form Load イベントに以下のような、DataGridView のデータを配置するコードを記述します。
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add(3);
dataGridView1.Rows[0].Cells[0].Value = "鈴木";
dataGridView1.Rows[0].Cells[1].Value = "suzuki";
dataGridView1.Rows[0].Cells[2].Value = "suzuki@micro.com";
dataGridView1.Rows[1].Cells[0].Value = "田中";
dataGridView1.Rows[1].Cells[1].Value = "tanaka";
dataGridView1.Rows[1].Cells[2].Value = "tanaka@micro.com";
dataGridView1.Rows[2].Cells[0].Value = "佐藤";
dataGridView1.Rows[2].Cells[1].Value = "sato";
dataGridView1.Rows[2].Cells[2].Value = "sato@micro.com";
}
5. DataGridView のセルをクリックした時のイベントとして以下を記述します。
string _selectedUserName;
string _selectedAlias;
string _selectedAddrss;
private void dataGridView1_CellMouseClick(object sender,EventArgs e)
{
_selectedUserName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
_selectedAlias = dataGridView1.CurrentRow.Cells[1].Value.ToString();
_selectedAddrss = dataGridView1.CurrentRow.Cells[2].Value.ToString();
}
6. ドキュメントに DataGridView の値を返すコードを記述します。
public string selectedUserName()
{
return _selectedUserName;
}
public string selectedAlias()
{
return _selectedAlias;
}
public string selectedAddress()
{
return _selectedAddrss;
}
public Form1()
{
InitializeComponent();
}
7.ドキュメントのボタンコントロールのコードを記述します。
if(fm.ShowDialog() != DialogResult.Cancel)
{
this.plainTextContentControl1.Text = fm.selectedUserName();
this.plainTextContentControl2.Text = fm.selectedAlias();
this.plainTextContentControl3.Text = fm.selectedAddress();
}
これで最初にご紹介した動きになると思います。
ちなみに VB 版は以下です。
VB版:
'Win フォームのコード
Public Class Form1
Private _selectedUserName As String
Private _selectedAlias As String
Private _selectedAddrss As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Rows.Clear()
DataGridView1.Rows.Add(3)
DataGridView1.Rows(0).Cells.Item(0).Value = "鈴木"
DataGridView1.Rows(0).Cells.Item(1).Value = "suzuki"
DataGridView1.Rows(0).Cells.Item(2).Value = "suzuki@microsft.com"
DataGridView1.Rows(1).Cells.Item(0).Value = "田中"
DataGridView1.Rows(1).Cells.Item(1).Value = "tanaka"
DataGridView1.Rows(1).Cells.Item(2).Value = "tanaka@microsft.com"
DataGridView1.Rows(2).Cells.Item(0).Value = "佐藤"
DataGridView1.Rows(2).Cells.Item(1).Value = "sato"
DataGridView1.Rows(2).Cells.Item(2).Value = "sato@microsft.com"
End Sub
Public ReadOnly Property selectedUserName()
Get
Return _selectedUserName
End Get
End Property
Public ReadOnly Property selectedAlias()
Get
Return _selectedAlias
End Get
End Property
Public ReadOnly Property selectedAddrss()
Get
Return _selectedAddrss
End Get
End Property
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
_selectedUserName = DataGridView1.CurrentRow.Cells(0).Value.ToString
_selectedAlias = DataGridView1.CurrentRow.Cells(1).Value.ToString
_selectedAddrss = _selectedAlias = DataGridView1.CurrentRow.Cells(2).Value.ToString
End Sub
End Class
'ドキュメントのコード
Public Class ThisDocument
Dim fm As New Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If fm.ShowDialog = DialogResult.Cancel Then
Exit Sub
Else
Me.PlainTextContentControl1.Text = fm.selectedUserName
Me.PlainTextContentControl2.Text = fm.selectedAlias
Me.PlainTextContentControl3.Text = fm.selectedAddrss
End If
End Sub
End Class
Comments
- Anonymous
September 27, 2008
PingBack from http://hoursfunnywallpaper.cn/?p=8132