taiyuアイコン

taiyuのVBプログラム


 チップ集(Tips)-①

モッピー!お金がたまるポイントサイト

チップ集(Tips 集)CGIダウンロードよくある質問(サポート) ソース提供相互リンク集  
チップ集(Tips 集)
○○システム

モッピー!お金がたまるポイントサイト
   ★印の項目をクリックしてください。

Tips   ①  2  3  

★【ComboBox】に項目を書き込む
  Private Sub Form1_Load(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load
    With ComboBox1
       Items.Add("イカ")
       Items.Add("イクラ")
       Items.Add("ウニ")
       Items.Add("エビ")
       BackColor = Color.PowderBlue
       Font = New Font("MS Pゴシック", 12)
       Text = "ウニ" 'テキストボックスに最初に表示して置く項目を設定
    End With
  End Sub
★【ComboBox】選択されている項目を取得する
  Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
    Dim si As String = ComboBox1.SelectedItem
    Debug.WriteLine(si)
  End Sub 

★【ComboBox】選択されている項目のインデックスを取得する
  Private Sub Button2_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button2.Click
    Dim si As Integer = ComboBox1.SelectedIndex
    Debug.WriteLine(si)
  End Sub 
★【ComboBox】ドロップダウンリストの表示項目数を変更する
  Private Sub Button3_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button3.Click
    If ComboBox1.MaxDropDownItems = 10 Then
      ComboBox1.MaxDropDownItems = 5
    Else
      ComboBox1.MaxDropDownItems = 10
    End If
  End Sub
★【ComboBox】ドロップダウンリストの表示・非表示をプログラムから実行
  Private Sub Button4_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button4.Click
    ComboBox1.DroppedDown = Not ComboBox1.DroppedDown
    ComboBox1.DroppedDown = True
  End Sub
★【ComboBox】ドロップダウンリスト部の表示幅を変更する
  Private Sub Button5_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button5.Click
    ComboBox1.DropDownWidth = 250
  End Sub
★【ComboBox】登録されていない項目だけを調べて追加書き込みする
  Private Sub Button6_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button6.Click
    Dim x As Integer = -1
    x = ComboBox1.FindStringExact(ComboBox1.Text, x)
    If x <> -1 Then
      MessageBox.Show("すでに登録されています")
      ComboBox1.Text = ""
    Else
      If MessageBox.Show("追加登録しますか?", "追加登録", _
        MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
        ComboBox1.Items.Add(ComboBox1.Text)
        ComboBox1.Text = ""
      End If
    End If
  End Sub
★MSHFlexGridにContextMenuStripを割り当てる
  MSHFlexGrid1.ContextMenuStrip = ContextMenuStrip1
★FrameをFormの中央に配置する
  UpdFrame.Left = CInt(Me.Width / 2 - UpdFrame.Width / 2)
  UpdFrame.Top = CInt(Me.Height / 2 - UpdFrame.Height)
------------------------------------------------------
・フォームの最小サイズを(Width,Height)とする
  Me.MinimumSize = New Size(702, 627)
・フォームの最大サイズを(Width,Height)とする
  Me.MaximumSize = New Size(702, 627)
  Me.Size = New Size(702, 627)
------------------------------------------------------
・xxxxフォームをモーダル ダイアログボックスとして表示
  xxxx.ShowDialog(Me)
★MDI子フォームを全て閉じる
  For Each ChildForm As Form In Me.MdiChildren
    ChildForm.Close()
  Next
★フォームの表示
・フォームの最大化ボタンの表示、非表示を切り替える
  Me.MaximizeBox = Not Me.MaximizeBox
・フォームの最小化ボタンの表示、非表示を切り替える
  Me.MinimizeBox = Not Me.MinimizeBox
・フォームのコントロールボックスの表示、非表示を切り替える
 コントロールボックスを非表示にすると最大化、最小化、閉じるボタンも消える
  Me.ControlBox = Not Me.ControlBox
★フォームFrm_001をMDIの子フォームに設定する
  Frm_001.MdiParent = Me
------------------
・MDIでアクティブな子フォームを取得する
  Dim currentForm As String
  currentForm = Me.ActiveMdiChild.Name
★OSやHOSTの情報を取得する
・OSのVersionを取得する
  MajoVer = Environment.OSVersion.Version.Major
・ホスト名を取得する
  Dim strHostName As String = System.Net.Dns.GetHostName()
・OSやHOSTの情報を取得するAPI
  Public Declare Function GetPcInfo Lib "sakinfo" _
      (ByVal osinfo As String, ByVal username As String, _
      ByVal hostname As String, ByVal ipaddress As String, _
      ByVal errmsg As String) As Long
  rcd = GetPcInfo(osinfo, username, hostname, ipaddress, errmsg)
★ファイル/フォルダの存在チェック
・ファイルが存在しているか調べる
  If System.IO.File.Exists(DBfile) Then
・フォルダが存在しているか調べる
  If System.IO.Directory.Exists(FolderName) Then
・ルートディレクトリかの判断
  Dim MyPath As String = Application.StartupPath
  If MyPath.EndsWith("\") = False Then
    MyPath &= "\"
  End If
----------------------------------------------
・実行ファイルのあるパス
  ExePath = Application.ExecutablePath

★ファイル名、フォルダ名の取得
・'(例)User32.dll を取得
  FileName = IO.Path.GetFileName(DBfile)
・(例)C:\Winnt\System32 を取得
  FolderName = IO.Path.GetDirectoryName(DBfile)
・(例)User32 を取得
  FileName = IO.Path.GetFileNameWithoutExtension(DBfile)
★テキストファイルへの書込み
  Try
    Dim HogeFile As String = "c:\hogehoge.txt"
    Dim sw As New System.IO.StreamWriter _
       (HogeFile, False, _
        System.Text.Encoding.GetEncoding("Shift-JIS")) '932))
    ' TextBox.Textの内容をすべて書き込む
    sw.Write(TextBox.Text)
    ' 閉じる
    sw.Close()
  Catch ex As Exception
    MessageBox.Show("データ書き込みに失敗しました。(E806)" & _
       vbCrLf & ex.Message, "テキスト書込み", _
       MessageBoxButtons.OK, MessageBoxIcon.Error)
  End Try
★テキストファイルの読み込み
  Dim HogeFile As String = "c:\hogehoge.txt"
  If System.IO.File.Exists(HogeFile) Then
    '=== 読み込み ======================
    Dim Reader As New IO.StreamReader _
       (MemoFile, System.Text.Encoding.GetEncoding("Shift-JIS"))
    TextBox.Text = Reader.ReadToEnd
    Reader.Close()
    '===================================
  Else
    MemoBox.Text = vbNullString
  End If

★ファイルの削除
  Dim HogeFile As String = "c:\hogehoge.txt"
  If System.IO.File.Exists(HogeFile) Then
    IO.File.Delete(HogeFile)
  End If

★レジストリからの読み取り
・(例)「HKEY_CURRENT_USER\Software\Hoge\System」にあるデータの値を取得
  Dim KeyName As String
  Dim cDBpath As String
  KeyName = "HKEY_CURRENT_USER\Software\Hoge\System"
  cDBpath = My.Computer.Registry.GetValue(KeyName, "DbPath", "失敗")
---> My.Settingsに変更
  cDBpath = My.Settings.DBFile
★レジストリへの書き込み
・(例)「HKEY_CURRENT_USER\Software\Hoge\System」に値を書き込む
  Dim KeyName As String
  Dim cDBpath As String = "C:\Windows"
  KeyName = "HKEY_CURRENT_USER\Software\Hoge\System"
  My.Computer.Registry.SetValue(KeyName, "DbPath", cDBpath)
---> My.Settingsに変更
  My.Settings.DBFile = cDBpath
★INIファイルから読み込み
  Dim strBuffer As New System.Text.StringBuilder
  strBuffer.Capacity = 256  'バッファサイズ
  Dim ret As Integer
・INIファイルよりキーの値を読み込み(整数値を取得する場合)
  iniFileName = MyPath & iniFileName
  myHeight = GetPrivateProfileInt("Form1Size", "Height", 0, iniFileName)
・INIファイルよりキーの値を読み込み(文字列を取得する場合)
  ret = GetPrivateProfileString("ItemName", "step1", "ほげ", _
  strBuffer, strBuffer.Capacity, iniFileName)
  '-- 成功すると0以外の値が返る --
  If ret <> 0 Then
    myStep1 = strBuffer.ToString
  Else
    GoTo Geterr
  End If
★アクティブウィンドウ/最前面ウィンドウの取得
・アクティブウィンドウのハンドルを取得
  Dim Activehdl As IntPtr = GetActiveWindow()
  Dim ahandle As String = Space(128)
  GetClassName(Activehdl, ahandle, 128)
  Label1.Text = ahandle
・最前面ウィンドウのハンドルを取得
  Dim forehdl As IntPtr = GetForegroundWindow()
  Dim fhandle As String = Space(128)
  GetClassName(forehdl, fhandle, 128)
  Label2.Text = fhandle
----------------------------------------------
・最前面ウィンドウに設定
  SetForceForegroundWindow(Me.Handle)
★半角かどうかのチェック
  intCheckChar = Strings.Asc(Strings.Mid(s, i, 1))
  If intCheckChar < 0 Or intCheckChar > 255 Then
    '半角
  End If

★数値かどうかのチェック
  If IsNumeric(cHankaku) Then
    '数値
  End If

★全角文字を半角文字に変換
  cHankaku = StrConv(Txt_name.Text, VbStrConv.Narrow)


【All About】専門家がガイドするテーマ別情報サイト

Copyright © Since 2008 Exterior taiyu All rights reserved