标题:VB.NET中API下拉框存储中文失败求解答
只看楼主
zgtjnhxglhj
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-1-24
结帖率:0
已结贴  问题点数:20 回复次数:4 
VB.NET中API下拉框存储中文失败求解答
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编辑过]

搜索更多相关主题的帖子: 下拉框 存储 中文 String ByVal 
2018-01-31 16:16
xyxcc177
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:26
帖 子:197
专家分:1249
注 册:2017-7-8
得分:10 
有些API函数在NET环境中是不适用的,处理文件有类,为什么还要用API函数呢
2018-02-03 18:55
zcsor
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:19
专家分:105
注 册:2018-2-5
得分:10 
这个是一个读写INI文件的API函数,在某些老的编程模型中还是使用INI,不过建议楼主用XML。
楼主这个API声明有点问题,其中写入的字符应该用StringBuilder。另外,中文变量名,我就曰了啊。给你改改,你的问题不是这个问题,而是你这编程是易语言教的么?!
2018-02-06 19:03
zcsor
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:19
专家分:105
注 册:2018-2-5
得分:0 
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
2018-02-06 19:08
zcsor
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:19
专家分:105
注 册:2018-2-5
得分:0 
其中数据库.txt:
[任务目标]
场景=1
2018-02-06 19:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-484568-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.096149 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved