注册 登录
编程论坛 VB.NET论坛

VB.NET中API下拉框存储中文失败求解答

zgtjnhxglhj 发布于 2018-01-31 16:16, 3016 次点击
VB,NET  新建一窗体一个按钮一个下拉框下拉框,GroupBox1中放入三行任何中文,
   API读写控件
    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String,        ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As
String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
    Dim 存储 As String
    Dim 读取 As Long
    Dim 内容 As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        内容 = Str(256)
        读取 = GetPrivateProfileString("任务目标", "场景", "", 内容, 256, Application.StartupPath & "\数据库.txt")
        ComboBox1.Text = 内容
    End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        存储 = WritePrivateProfileString("任务目标", "场景", ComboBox1.Text, Application.StartupPath & "\数据库.txt")
End Sub



点击按钮发生英文能保存,中文点击一次下拉框中文内容会减少一个中文,直到最后变成问号。难道是VS2015和2017不支持API求教!!!







[此贴子已经被作者于2018-1-31 16:23编辑过]

4 回复
#2
xyxcc1772018-02-03 18:55
有些API函数在NET环境中是不适用的,处理文件有类,为什么还要用API函数呢
#3
zcsor2018-02-06 19:03
这个是一个读写INI文件的API函数,在某些老的编程模型中还是使用INI,不过建议楼主用XML。
楼主这个API声明有点问题,其中写入的字符应该用StringBuilder。另外,中文变量名,我就曰了啊。给你改改,你的问题不是这个问题,而是你这编程是易语言教的么?!
#4
zcsor2018-02-06 19:08
Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1
    <DllImport("kernel32", CharSet:=CharSet.Unicode, SetLastError:=True)>
    Private Shared Function WritePrivateProfileString(lpAppName As String, lpKeyName As String, lpString As String, lpFileName As String) As Boolean
    End Function
    <DllImport("kernel32", CharSet:=CharSet.Unicode, SetLastError:=True)>
    Private Shared Function GetPrivateProfileString(lpAppName As String, lpKeyName As String, lpDefault As String, lpReturnedString As StringBuilder, nSize As Integer, lpFileName As String) As Integer
    End Function
    Dim 存储 As String
    Dim 读取 As Integer
    Dim 内容 As StringBuilder

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        内容 = New StringBuilder(512)
        读取 = GetPrivateProfileString("任务目标", "场景", "", 内容, 内容.Capacity, Application.StartupPath & "\数据库.txt".Replace("\", "\\"))
        ComboBox1.Text = 内容.ToString
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        存储 = WritePrivateProfileString("任务目标", "场景", ComboBox1.Text, Application.StartupPath & "\数据库.txt")
    End Sub

End Class
#5
zcsor2018-02-06 19:08
其中数据库.txt:
[任务目标]
场景=1
1