这是我前面发的 combobox的判断与应用 的另一个帖,我的INI文件格式如下:
[广西分区]
广西,贵州,海南 = 4
安徽,福建,广东,湖北,湖南,江西,四川,云南,重庆 = 6
河南,江苏,山东,陕西,上海,浙江 = 9
北京,甘肃,宁夏,天津,西藏 = 10
河北,黑龙江,吉林,辽宁,内蒙古,青海,山西,新疆 = 17[广东分区]
广东 = 4
安徽,福建,广西,贵州,海南,河南,湖北,湖南,江苏,江西,上海,四川,云南,浙江,重庆 = 6
北京,河北,山东,山西,陕西,天津 = 9
甘肃,内蒙古,宁夏,青海 = 10
黑龙江,吉林,辽宁,西藏,新疆 = 17我程序有2个combobox控件,一个按钮和一个标签,2个combobox列表如下:安徽
北京
福建
甘肃
广东
广西
贵州
海南
河北
河南
黑龙江
湖北
湖南
吉林
江苏
江西
辽宁
内蒙古
宁夏
青海
山东
山西
陕西
上海
四川
天津
西藏
新疆
云南
浙江
重庆我想实现的是当我按下按钮后,判断combo1选的是什么,比如选的是"广西"将定位到[广西分区]关键字下,然后判断combo2选的是什么,比如选的是"安徽",就取得[广西分区]关键字下安徽后面等号的数值是 "6",最后将6显示在标签上,同样,combo1选的是广东就定位到[广东分区]下,然后通过combo2所选的读取下面的值并显示,代码要怎么写/????

解决方案 »

  1.   

    实例67:读写INI文件   
      实例说明   
      在本实例中,我们将应用VB.NET制作一个能够实现读写INI文件的应用程序。程序运行结果如图67-1所示。   
          
      图67-1     运行结果   
      技术要点   
       INI文件的格式   
       GetPrivateProfileInt()和GetPrivateProfileString()函数   
       WritePrivateProfileString()函数   
      实现步骤   
      ■ 新建项目   
      打开Visual   Studio.NET,选择“新建项目”,在项目类型窗口中选择“Visual   Basic项目”,在模板窗口中,选择“Windows应用程序”,在名称域中输入“RWIni”,然后选择保存路径。单击“确认”。   
      ■ 添加控件和菜单   
      向当前窗体上添加两个Button控件,用于控制读写INI文件;一个Group控件和两个RadioButton控件,用于控制读写整型数据还是字符串型;三个Label控件和三个TextBox控件,用于标注和输入节名、键名和值;其余两个Label控件,一个表示当前打开的文件名,另一个表示读写的状态。最后添加一个MainMenu控件,生成菜单“文件”、“退出”,其中“文件”下有一个子菜单“打开INI文件”。   
      ■ 设置属性   
      切换到“属性栏”,对窗体上的控件设置属性。详细情况见表67-1。   
      表67-1     窗体各控件的属性值   
      窗体/控件 属性 值   
      optInt value True   
      Lablel5 Name Lblfilename   
      Autosize true   
      BackSytle 0-Transparent   
      Button1 Text 读取   
      BackColor &H0080C0FF   
      Style 1-Graphic   
      TextBox1 Name txtSection   
      Text (空)   
      知识点:一个INI文件由若干节(Section)组成,每个节中又由若干个关键字(Keyword)和值组成。关键字是用来保存独立的程序设置和那些重要信息的,用于控制应用程序的某个功能;值可以是整型数或字符串。如果值为空,则表示应用程序已经为该关键字指定了缺省值。   
      INI文件的一般形式如下:   
      …………   
      [Section]   
      keyword=value   
      …………   
      INI文件有严格的格式要求:   
      (1)节名必须加“[”和“]”。   
      (2)对文件做注释,要以“;”开头。   
      (3)关键字后必须有“=”。   
      ■ 添加代码   
      keywordinfo   =   txtkeyword.Text   
      Valueinfo   =   txtvalue.Text   
      '   检查输入是否合法,不合法时,提示警告信息。   
      '   读取INI文件的内容   
      Private   Sub   Button1_Click(ByVal   eventSender   As   System.Object,   ByVal   eventArgs   As   System.EventArgs)   Handles   Button1.Click   
      Dim   readinfo   As   String   
      Dim   buffer   As   VB6.FixedLengthString   =   New   VB6.FixedLengthString(255)   
      Dim   Sectioninfo   As   String   
      Dim   Valueinfo   As   String   
      Dim   keywordinfo   As   String   
      Sectioninfo   =   txtsection.Text   
      If   Trim(Sectioninfo)   =   ""   Then   '   在此可以用len(Sectioninfo)=0来代替   
      MsgBox("请输入节名(Section)",   MsgBoxStyle.OKOnly,   "错误信息")   
      Exit   Sub   
      End   If   
      If   Trim(keywordinfo)   =   ""   Then   
      MsgBox("请输入关键字(Keyword)",   MsgBoxStyle.OKOnly,   "错误信息")   
      Exit   Sub   
      End   If   
      '   判断读取的是数字还是字符串   
      If   optint.Checked   Then   
      '   使用   GetPrivateProfileInt函数取回一个关键字的整型数值   
      readinfo   =   CStr(GetPrivateProfileInt(Sectioninfo,   keywordinfo,   -1,   AXCommonDialog1.FileName))   
      '   GetPrivateProfileInt函数返回取得的值,如果键没有找到,则返回由nDefault参数指定的值   
      If   CDbl(readinfo)   =   -1   Then   
      txtvalue.Text   =   "读取失败!"   
      Else   
      txtvalue.Text   =   readinfo   
      End   If   
      Else   
      '   使用   GetPrivateProfileString函数取回一个关键字的字符串数值   
      readinfo   =   CStr(GetPrivateProfileString(Sectioninfo,   keywordinfo,   "没有找到相对应的键值",   buffer.Value,   255,   AXCommonDialog1.FileName))   
      '   GetPrivateProfileString函数返回放入lpReturnedString缓冲区的字节数,   
      '   键值放在由参数lpReturnedString指定的缓冲区中。该缓冲区是一个由nSize字节大小的固定字符串   
      If   readinfo   =   "没有找到相对应的键值"   Then   
      txtvalue.Text   =   "读取失败!"   
      Else   
      txtvalue.Text   =   VB.Left(buffer.Value,   CInt(readinfo))   
      End   If   
      End   If   
      End   Sub   
      '   写入INI文件   
      Private   Sub   Button2_Click(ByVal   eventSender   As   System.Object,   ByVal   eventArgs   As   System.EventArgs)   Handles   Button2.Click   
      Dim   writeinfo   As   Boolean   
      Dim   buffer   As   VB6.FixedLengthString   =   New   VB6.FixedLengthString(255)   
      Dim   Sectioninfo   As   Object   
      Dim   Valueinfo   As   String   
      Dim   keywordinfo   As   String   
      Sectioninfo   =   txtsection.Text   
      keywordinfo   =   txtkeyword.Text   
      Valueinfo   =   txtvalue.Text   
      '   使用更新给定节中的新键值,或创建一个新键值,   
      '   或用来删除一个节或键值   
      writeinfo   =   WritePrivateProfileString(Sectioninfo,   keywordinfo,   Valueinfo,   AXCommonDialog1.FileName)   
      '   注意,WritePrivateProfileString函数设置成功,返回值为True,否则返回False   
      If   writeinfo   =   True   Then   
      Label4.Text   =   "写入INI文件成功!"   
      Else   
      Label4.Text   =   "错误!不能写入INI文件!"   
      End   If   
      End   Sub   
      '   打开INI文件   
      Public   Sub   mnuOpenINIFile_Popup(ByVal   eventSender   As   System.Object,   ByVal   eventArgs   As   System.EventArgs)   Handles   mnuOpenINIFile.Popup   
      mnuOpenINIFile_Click(eventSender,   eventArgs)   
      End   Sub   
      Public   Sub   mnuOpenINIFile_Click(ByVal   eventSender   As   System.Object,   ByVal   eventArgs   As   System.EventArgs)   Handles   mnuOpenINIFile.Click   
      '   设置过滤器   
      AXCommonDialog1.Filter   =   "*.INI|*.ini"   
      AXCommonDialog1.ShowOpen()   
      If   Len(AXCommonDialog1.FileName)   =   0   Then   Exit   Sub   
      '   打开有效文件后,按钮可用   
      Button1.Enabled   =   True   
      Button2.Enabled   =   True   
      '   显示当前打开的文件名   
      lblfilename.Text   =   "现在打开的文件是:"   &   AXCommonDialog1.FileName   
      End   Sub   
      ■ 运行程序   
      单击菜单“调试|启动”或单击       图标运行程序。   
      小结   
      INI文件是包含了应用程序特定信息的文本文件。比如将用户所作的选择以及各种变化的系统信息记录在INI文件中,通常是在应用程序运行时读取INI文件中的信息,退出应用程序时保存用户对运行环境的某些修改。 
    网上找的 INI 读取 写入方法
      

  2.   

    定位combo用listindex属性。判断可以用item集合。
      

  3.   

    建议你的ini这样写:
    [广西]
    4=广西,贵州,海南
    6=……
    9=……
    ……然后根据“广西”(发货省份)去轮询4,6,9……,目的省份到根据取得的值里面去找。因为读取ini是根据“=”前面的参数获得后面的值的,你的ini没有统一定义的参数,几乎无法用统一的代码去读取每个省份的区数。如果有数据库,我看还是应该搞个31行31列的表(比如球赛循环赛的成绩表一样)读取更快。
    或者二维数组(0,0 到 31,31)
      

  4.   


    同意楼上的, 主键值应该是简短,读取快速方便, 呵呵...第一次见到楼主这样的 .ini
      

  5.   

    Dim s(31, 31) As String
    Private Sub Command1_Click()
        Text1.Text = s(Combo1.ListIndex, Combo2.ListIndex)
    End SubPrivate Sub Form_Load()
    Dim i As Byte, n As Byte
    Combo1.AddItem "广西"
    Combo1.AddItem "安徽"
    Combo1.AddItem "河南"
    Combo1.AddItem "北京"
    Combo2.AddItem "广西"
    Combo2.AddItem "安徽"
    Combo2.AddItem "河南"
    Combo2.AddItem "北京"
    For i = 0 To 31
        For n = 0 To 31
            s(i, n) = 5 '示例:都作为5元
        Next n
    Next i
    End Sub新建工程,
    添加combol1,combol2,command1,text1
      

  6.   

    5楼你就别笑我了,我是菜鸟,能想出的也只有那些,数据库这种读取的不知道怎么弄,所以就想弄读取INI,我上面的格式不好读取,那要怎么写?拜托了....6楼写的任意组合都是得出5,不明白那是什么意思...我是菜鸟,别笑我...
      

  7.   


    哈哈哈 误会误会....只是想告诉你没人这样用.ini罢了说实在些 你这个问题应该是很单纯 可最大的问题是不明白你的目的,你想要什么?能说清楚些码?
      

  8.   

    回8楼,http://topic.csdn.net/u/20101216/19/830414fd-a4aa-4191-80af-b1d7187bf41b.html这是我前面问的,这是第二个帖,,就想弄个和EMS快递官网一样的资费计算器而已....因为每个省的分区表都不一样,所以所收取的资费也不一样,所以就想把每个省的分区表保存在INI里进行读取,这样就能知道该省到下一个省的资费是多少了....能有好的思路吗??
      

  9.   


    上面这句话 我错了, 虽然理论上是错的,但是以楼主的需求来说, INI这样写 反而是 "高效"我可以写得出算得上漂亮的代码 直接点击Combo2的任何一个省份 直接读出该省的 "值"但是楼主的思路 我仍不清楚, 因为既然分了 广东 广西 两个分区, 为何两区的省份有重覆的?江南区就是江南 江北就是江北 怎么可能有重复的?而且 Combo2 的每一笔内容 将是一个单独的省份, 用逗号拆分后,目的达到了, 但是当要读取时, 却仍然要去一一比对 似乎毫无效率可言.我现在的代码是
    一,Combo1只有广东与广西
    二.Combo2有两个区的所有省份,过滤掉重覆的
    三.直接点击Combo2 得到该省的 "值",不需再点 Combo1思路上与楼主不同, 因此代码我目前无法贴, 等你回复后再说....
      

  10.   

    没看明白你的问题,不过可以发一个自己解析INI的类,可以枚举节与关键字,可以取值,嘿嘿.http://www.m5home.com/blog/article.asp?id=541不知道能不能有帮助.
      

  11.   

    本帖最后由 bcrun 于 2010-12-20 20:43:41 编辑
      

  12.   

    Global AppDisk$, IniName$, aa$, i&, S
     
    忘了拿掉,改为Global AppDisk$, IniName$
      

  13.   

    谢谢楼上 zzhgb iStr = String(255, Chr(0))
    因为它的长度短, 哈哈... 255 改下便可.
      

  14.   

    不过我还是应该提醒, 上次我还被255整过呢, 再次感谢zzhgb, 我将在原代码上特别标记一下.
      

  15.   

    上面写到的[广东分区][广西分区]应该是[广东][广西]来的,为了看明白才加上分区二字的,去掉中括号,其实就是广西,广东了,也就是 Combo1 的下拉菜单,但是只写了广东和广西而已,还有几十个省的分区没写,写完后,Combo1 的下拉菜单就和我前面开头说的一样是:
    安徽
    北京
    福建
    甘肃
    广东
    广西
    贵州
    海南
    河北
    河南
    黑龙江
    湖北
    湖南
    吉林
    江苏
    江西
    辽宁
    内蒙古
    宁夏
    青海
    山东
    山西
    陕西
    上海
    四川
    天津
    西藏
    新疆
    云南
    浙江
    重庆测试了下你13楼的代码,是可以读出数值了,但是comboBOX里的省份是按26个字母排列的,combo1里的列表我都可以通过调整INI文件的各个分区的前后顺序来达到排列的目的,但是combo2的省份就很乱,不是按26个字母排序的,而是按我在INI写的顺序排列的,说白了,就是和combo1的排列不一样,这样看起来很乱,很来找,不知道有什么方法能实现他的列表能按26字母摆列呢???
      

  16.   

    要排序很简单 你只要将 combo的属性 sorted 设为 True即可代码再改了一下
    Option Explicit '强制必需宣告变量
    Dim i&, j&, k&, aa$, bb$, TmpStr$, S, S2 '定义各个用到的变量形态
    'Const CB_FINDSTRING = &H14C 判别Combo内容重覆的常量,还要多个API 麻烦
    Private Sub Form_Load()
       aa = INIReadTitle(vbNullString, vbNullString, IniName) '读出 [ ] 大纲要
       S = Split(aa, Chr(0)) '以Chr(0)分割纲要
       For i = 0 To UBound(S) '循环枚举所有[]纲要
          If Trim(S(i)) = "" Then Exit For '字符为空退出循环
          Combo1.AddItem S(i) 'Combo1里面添加纲要
       Next i
       Combo1.Text = "": Combo2.Text = ""
    End SubPrivate Sub Combo1_Click()
       Combo2.Clear '清空Combo2
       TmpStr = "" '清空缓冲变量TmpStr
       aa = INIReadTitle(Combo1.Text, vbNullString, IniName) '读取大纲要下面的所有项目集合
       S = Split(aa, Chr(0)) '使用每一个项目的分隔符Chr(0) 分开每一个独立的项目
       For j = 0 To UBound(S) '循环枚举所有独立的项目
          If Trim(S(j)) = "" Then Exit For '空的项目则退出循环
          S2 = Split(S(j), ",") '将此包含多个省份的项目用逗号分割出来
          For k = 0 To UBound(S2) '循环枚举此项目下的各别省份
             If Trim(S2(k)) = "" Then Exit For '空的省份则退出循环
             If InStr(TmpStr, S2(k)) = 0 Then '在缓冲变量TmpStr中查找, 如果 "不存在" 这个省份
                Combo2.AddItem S2(k) 'Combo2添加这个省份
                TmpStr = TmpStr & S2(k) & ","  '缓冲变量TmpStr再加入这个省份
             End If
          Next k
       Next j
    End SubPrivate Sub Combo2_Click()
       If Combo1.Text = "" Then MsgBox "请选择地区!": Exit Sub '如果Combo1没选择 退出本事件
       aa = INIReadTitle(Combo1.Text, vbNullString, IniName) '读进Combo1的地区纲要
       S = Split(aa, Chr(0)) '使用每一个项目的分隔符Chr(0) 分开每一个独立的项目
       For i = 0 To UBound(S) '循环枚举此纲要下所有独立的项目
          If InStr(S(i), Combo2.Text) > 0 Then '判断这个项目下是否包含Combo2所选择的省份
             bb = S(i) '将项目带入变量bb
             Label1.Caption = INIRead(Combo1.Text, bb, IniName) '从地区纲要与省份两条件中读进 值
             Exit For '退出循环
          End If
       Next i
    End Sub
      

  17.   

    19楼的代码是运行先一起读出 所有[]纲要 的,然后纲要下的是通过 所选纲要才相应读出的,这样好像是在资源上比较节省?还有数度比较快吗???嘻嘻,不知道理解对不...但是这样有缺点,就是一运行 Combo2 是空的,难看,还有就是当 Combo2 选好后,再选Combo1的话,Combo2又给刷新了.这样就使得程序两个 Combo 件好像不怎么连贯,这是我的观点,不知道这两句代码的 分别好处和不好的地方是什么呢???拜托帮解释下.
      

  18.   

    代码该给的都给你了 自己改改啊一.不想Combo2就为空 可在Form_Load里 combo2.additem 就用13F的代码二.点击Combo1时 Combo2必需清空刷新 因为每个地区各有不同的省份 不刷新清除的话
    选广东还有广西的 选广西的还有广东的 不是吗?
      

  19.   

    你要一开始就让 combo2有内容的话 你只要在Form_Load 最下面加上一行Combo1.ListIndex = 0
      

  20.   

    谢谢你的回答,很满意,最后问你个问题哦,前面为问了那么多的问题是想把取得的值付给程序中的 "X" ,而 "X" 的值是计算EMS邮费的关键值,下面所有代码中的 "x"都是要读取前面各个省份的相应值,也就是我说的把 数值 显示 在标签一样,,,,但是下面的代码好长,看的好晕,有办吧精简代码吗??               If Text1.Text > 0 And Text1.Text <= 0.5 Then Text2.Text = 20
                   If Text1.Text > 0.5 And Text1.Text <= 1 Then Text2.Text = 20 + x * (1 * 2 - 1)
                   If Text1.Text > 1 And Text1.Text <= 1.5 Then Text2.Text = 20 + x * (1.5 * 2 - 1)
                   If Text1.Text > 1.5 And Text1.Text <= 2 Then Text2.Text = 20 + x * (2 * 2 - 1)
                   If Text1.Text > 2 And Text1.Text <= 2.5 Then Text2.Text = 20 + x * (2.5 * 2 - 1)
                   If Text1.Text > 2.5 And Text1.Text <= 3 Then Text2.Text = 20 + x * (3 * 2 - 1)
                   If Text1.Text > 3 And Text1.Text <= 3.5 Then Text2.Text = 20 + x * (3.5 * 2 - 1)
                   If Text1.Text > 3.5 And Text1.Text <= 4 Then Text2.Text = 20 + x * (4 * 2 - 1)
                   If Text1.Text > 4 And Text1.Text <= 4.5 Then Text2.Text = 20 + x * (4.5 * 2 - 1)
                   If Text1.Text > 4.5 And Text1.Text <= 5 Then Text2.Text = 20 + x * (5 * 2 - 1)
                   If Text1.Text > 5 And Text1.Text <= 5.5 Then Text2.Text = 20 + x * (5.5 * 2 - 1)
                   If Text1.Text > 5.5 And Text1.Text <= 6 Then Text2.Text = 20 + x * (6 * 2 - 1)
                   If Text1.Text > 6 And Text1.Text <= 6.5 Then Text2.Text = 20 + x * (6.5 * 2 - 1)
                   If Text1.Text > 6.5 And Text1.Text <= 7 Then Text2.Text = 20 + x * (7 * 2 - 1)
                   If Text1.Text > 7 And Text1.Text <= 7.5 Then Text2.Text = 20 + x * (7.5 * 2 - 1)
                   If Text1.Text > 7.5 And Text1.Text <= 8 Then Text2.Text = 20 + x * (8 * 2 - 1)
                   If Text1.Text > 8 And Text1.Text <= 8.8 Then Text2.Text = 20 + x * (8.5 * 2 - 1)
                   If Text1.Text > 8.5 And Text1.Text <= 9 Then Text2.Text = 20 + x * (9 * 2 - 1)
                   If Text1.Text > 9 And Text1.Text <= 9.5 Then Text2.Text = 20 + x * (9.5 * 2 - 1)
                   If Text1.Text > 9.5 And Text1.Text <= 10 Then Text2.Text = 20 + x * (10 * 2 - 1)
                   If Text1.Text > 10 And Text1.Text <= 10.5 Then Text2.Text = 20 + x * (10.5 * 2 - 1)
                   If Text1.Text > 10.5 And Text1.Text <= 11 Then Text2.Text = 20 + x * (11 * 2 - 1)
                   If Text1.Text > 11 And Text1.Text <= 11.5 Then Text2.Text = 20 + x * (11.5 * 2 - 1)
                   If Text1.Text > 11.5 And Text1.Text <= 12 Then Text2.Text = 20 + x * (12 * 2 - 1)
                   If Text1.Text > 12 And Text1.Text <= 12.5 Then Text2.Text = 20 + x * (12.5 * 2 - 1)
                   If Text1.Text > 12.5 And Text1.Text <= 13 Then Text2.Text = 20 + x * (13 * 2 - 1)
                   If Text1.Text > 13 And Text1.Text <= 13.5 Then Text2.Text = 20 + x * (13.5 * 2 - 1)
                   If Text1.Text > 13.5 And Text1.Text <= 14 Then Text2.Text = 20 + x * (14 * 2 - 1)
                   If Text1.Text > 14 And Text1.Text <= 14.5 Then Text2.Text = 20 + x * (14.5 * 2 - 1)
                   If Text1.Text > 14.5 And Text1.Text <= 15 Then Text2.Text = 20 + x * (15 * 2 - 1)
                   If Text1.Text > 15 And Text1.Text <= 15.5 Then Text2.Text = 20 + x * (15.5 * 2 - 1)
                   If Text1.Text > 15.5 And Text1.Text <= 16 Then Text2.Text = 20 + x * (16 * 2 - 1)
                   If Text1.Text > 16 And Text1.Text <= 16.5 Then Text2.Text = 20 + x * (16.5 * 2 - 1)
                   If Text1.Text > 16.5 And Text1.Text <= 17 Then Text2.Text = 20 + x * (17 * 2 - 1)
                   If Text1.Text > 17 And Text1.Text <= 17.5 Then Text2.Text = 20 + x * (17.5 * 2 - 1)
                   If Text1.Text > 17.5 And Text1.Text <= 18 Then Text2.Text = 20 + x * (18 * 2 - 1)
                   If Text1.Text > 18 And Text1.Text <= 18.5 Then Text2.Text = 20 + x * (18.5 * 2 - 1)
                   If Text1.Text > 18.5 And Text1.Text <= 19 Then Text2.Text = 20 + x * (19 * 2 - 1)
                   If Text1.Text > 19 And Text1.Text <= 19.5 Then Text2.Text = 20 + x * (19.5 * 2 - 1)
                   If Text1.Text > 19.5 And Text1.Text <= 20 Then Text2.Text = 20 + x * (20 * 2 - 1)
                   If Text1.Text > 20 And Text1.Text <= 20.5 Then Text2.Text = 20 + x * (20.5 * 2 - 1)
                   If Text1.Text > 20.5 And Text1.Text <= 21 Then Text2.Text = 20 + x * (21 * 2 - 1)
                   If Text1.Text > 21 And Text1.Text <= 21.5 Then Text2.Text = 20 + x * (21.5 * 2 - 1)
                   If Text1.Text > 21.5 And Text1.Text <= 22 Then Text2.Text = 20 + x * (22 * 2 - 1)
                   If Text1.Text > 22 And Text1.Text <= 22.5 Then Text2.Text = 20 + x * (22.5 * 2 - 1)
                   If Text1.Text > 22.5 And Text1.Text <= 23 Then Text2.Text = 20 + x * (23 * 2 - 1)
                   If Text1.Text > 23 And Text1.Text <= 23.5 Then Text2.Text = 20 + x * (23.5 * 2 - 1)
                   If Text1.Text > 23.5 And Text1.Text <= 24 Then Text2.Text = 20 + x * (24 * 2 - 1)
                   If Text1.Text > 24 And Text1.Text <= 24.5 Then Text2.Text = 20 + x * (24.5 * 2 - 1)
                   If Text1.Text > 24.5 And Text1.Text <= 25 Then Text2.Text = 20 + x * (25 * 2 - 1)
                   If Text1.Text > 25 And Text1.Text <= 25.5 Then Text2.Text = 20 + x * (25.5 * 2 - 1)
                   If Text1.Text > 25.5 And Text1.Text <= 26 Then Text2.Text = 20 + x * (26 * 2 - 1)
                   If Text1.Text > 26 And Text1.Text <= 26.5 Then Text2.Text = 20 + x * (26.5 * 2 - 1)
                   If Text1.Text > 26.5 And Text1.Text <= 27 Then Text2.Text = 20 + x * (27 * 2 - 1)
                   If Text1.Text > 27 And Text1.Text <= 27.5 Then Text2.Text = 20 + x * (27.5 * 2 - 1)
                   If Text1.Text > 27.5 And Text1.Text <= 28 Then Text2.Text = 20 + x * (28 * 2 - 1)
                   If Text1.Text > 28 And Text1.Text <= 28.5 Then Text2.Text = 20 + x * (28.5 * 2 - 1)
                   If Text1.Text > 28.5 And Text1.Text <= 29 Then Text2.Text = 20 + x * (29 * 2 - 1)
                   If Text1.Text > 29 And Text1.Text <= 29.5 Then Text2.Text = 20 + x * (29.5 * 2 - 1)
                   If Text1.Text > 29.5 And Text1.Text <= 30 Then Text2.Text = 20 + x * (30 * 2 - 1)
                   If Text1.Text > 30 And Text1.Text <= 30.5 Then Text2.Text = 20 + x * (30.5 * 2 - 1)
                   If Text1.Text > 30.5 And Text1.Text <= 31 Then Text2.Text = 20 + x * (31 * 2 - 1)
                   If Text1.Text > 31 And Text1.Text <= 31.5 Then Text2.Text = 20 + x * (31.5 * 2 - 1)
                   If Text1.Text > 31.5 And Text1.Text <= 32 Then Text2.Text = 20 + x * (32 * 2 - 1)
                   If Text1.Text > 32 And Text1.Text <= 32.5 Then Text2.Text = 20 + x * (32.5 * 2 - 1)
                   If Text1.Text > 32.5 And Text1.Text <= 33 Then Text2.Text = 20 + x * (33 * 2 - 1)
                   If Text1.Text > 33 And Text1.Text <= 33.5 Then Text2.Text = 20 + x * (33.5 * 2 - 1)
                   If Text1.Text > 33.5 And Text1.Text <= 34 Then Text2.Text = 20 + x * (34 * 2 - 1)
                   If Text1.Text > 34 And Text1.Text <= 34.5 Then Text2.Text = 20 + x * (34.5 * 2 - 1)
                   If Text1.Text > 34.5 And Text1.Text <= 35 Then Text2.Text = 20 + x * (35 * 2 - 1)
                   If Text1.Text > 35 And Text1.Text <= 35.5 Then Text2.Text = 20 + x * (35.5 * 2 - 1)
                   If Text1.Text > 35.5 And Text1.Text <= 36 Then Text2.Text = 20 + x * (36 * 2 - 1)
                   If Text1.Text > 36 And Text1.Text <= 36.5 Then Text2.Text = 20 + x * (36.5 * 2 - 1)
                   If Text1.Text > 36.5 And Text1.Text <= 37 Then Text2.Text = 20 + x * (37 * 2 - 1)
                   If Text1.Text > 37 And Text1.Text <= 37.5 Then Text2.Text = 20 + x * (37.5 * 2 - 1)
                   If Text1.Text > 37.5 And Text1.Text <= 38 Then Text2.Text = 20 + x * (38 * 2 - 1)
                   If Text1.Text > 38 And Text1.Text <= 38.5 Then Text2.Text = 20 + x * (38.5 * 2 - 1)
                   If Text1.Text > 38.5 And Text1.Text <= 39 Then Text2.Text = 20 + x * (39 * 2 - 1)
                   If Text1.Text > 39 And Text1.Text <= 39.5 Then Text2.Text = 20 + x * (39.5 * 2 - 1)
                   If Text1.Text > 39.5 And Text1.Text <= 40 Then Text2.Text = 20 + x * (40 * 2 - 1)
     
      

  21.   

    晕死了..........
    Dim i&, x&
    Private Sub Command1_Click()
       x = 1 'x自己改
       If Text1.Text > 0 And Text1.Text <= 0.5 Then Text2.Text = 20: Exit Sub
       For i = 0 To 39.5 Step 0.5
           If Val(Text1.Text) > i And Val(Text1.Text) <= i + 0.5 Then Text2.Text = 20 + x * ((i + 0.5) * 2 - 1)
       Next i
    End Sub
      

  22.   

    晕死了..........
    Dim i&, x&
    Private Sub Command1_Click()
      x = 1 'x自己改
      If Val(Text1.Text) > 0 And Val(Text1.Text) <= 0.5 Then Text2.Text = "20": Exit Sub
      For i = 0 To 39.5 Step 0.5
      If Val(Text1.Text) > i And Val(Text1.Text) <= i + 0.5 Then Text2.Text =Cstr( 20 + x * ((i + 0.5) * 2 - 1))
      Next i
    End Sub
      

  23.   

    如果你想在任何一个条件成立时停止判断的话 就随时加上 Exit For 结束For...Next 循环
      

  24.   

    我这菜鸟确实是晕死了,写的我要晕完了,不过用了你的代码后我死的更惨了,我都写成下面的,但是点计算后,就给我循环的不能动,CPU占用50%.......怎么回事....                     If Val(Text1.Text) > 0 And Val(Text1.Text) <= 0.5 Then Text2.Text = "20": Exit Sub
                             For ii = 0 To 39.5 Step 0.5
                                  If Val(Text1.Text) > ii And Val(Text1.Text) <= ii + 0.5 Then Text2.Text = CStr(20 + x * ((ii + 0.5) * 2 - 1))
                             Next ii
                         Exit For '退出循环
      

  25.   

    改为 For ii = 0.5 To 39.5 Step 0.5你的CPU耗用与你的一堆 If  没多大关习 因为它们是一转眼间的事, 高效的话 已告诉你 if条件成立时即Exit For 退出循环
      

  26.   

    Exit For '退出循环   这据话写到循环外了,造成循环一直没停止,弄的死循环,下面这样写终于好了,
    If Val(Text1.Text) > 0 And Val(Text1.Text) <= 0.5 Then Text2.Text = "20": Exit Sub
                             For ii = 0.5 To 39.5 Step 0.5
                                  If Val(Text1.Text) > ii And Val(Text1.Text) <= ii + 0.5 Then Text2.Text = CStr(20 + x * ((ii + 0.5) * 2 - 1))
                             Next ii
                         Exit For '退出循环
      

  27.   

    还是不行,用我29楼的代码就死循环,,,,用下面代码没作用                     If Val(Text1.Text) > 0 And Val(Text1.Text) <= 0.5 Then Text2.Text = "20": Exit Sub
                             For ii = 0.5 To 39.5 Step 0.5
                                  If Val(Text1.Text) > ii And Val(Text1.Text) <= ii + 0.5 Then Text2.Text = CStr(20 + x * ((ii + 0.5) * 2 - 1))
                               Exit For '退出循环
                         Next ii
    到底什么问题哦,,,,,
      

  28.   

    If Val(Text1.Text) > 0 And Val(Text1.Text) <= 0.5 Then Text2.Text = "20": Exit Sub
    For ii = 0.5 To 39.5 Step 0.5
      If Val(Text1.Text) > ii And Val(Text1.Text) <= ii + 0.5 Then Text2.Text = CStr(20 + x * ((ii + 0.5) * 2 - 1)): Exit For '退出循环
    Next ii
    exit for放错地方了
      

  29.   

    32楼的代码和31楼的代码不是一样的吗????还是不行,我在Text1输入的是1,然后X的值是你13楼的代码从INI中取得的值,一运行CPU就到50%,然后程序最后就到了没有响应了,怎么回事,下面是所有的代码,帮我看看什么问题,,,,Option Explicit '强制必需宣告变量'修改 Combobox 的下拉菜单高度
    Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
     
     '定义各个用到的变量形态
    Dim i&, j&, k&, aa$, bb$, TmpStr$, S, S2
    Dim cd, cd1, x
    Dim ii&'Const CB_FINDSTRING = &H14C 判别Combo内容重覆的常量,还要多个API 麻烦Private Sub Form_Load()
      aa = INIReadTitle(vbNullString, vbNullString, IniName) '读出 [ ] 大纲要
      S = Split(aa, Chr(0)) '以Chr(0)分割纲要
          For i = 0 To UBound(S) '循环枚举所有[]纲要
               If Trim(S(i)) = "" Then Exit For '字符为空退出循环
                   Combo1.AddItem S(i) 'Combo1里面添加纲要
          Next i
                 TmpStr = "" '缓冲内容清空,目的是过滤重覆的省份
           For i = 0 To Combo1.ListCount - 1 '循环读取所有的纲要
              aa = INIReadTitle(Combo1.List(i), vbNullString, IniName) '读取大纲要下面的所有项目集合
               S = Split(aa, Chr(0)) '使用每一个项目的分隔符Chr(0) 分开每一个独立的项目
           For j = 0 To UBound(S) '循环枚举所有独立的项目
              If Trim(S(j)) = "" Then Exit For '空的项目则退出循环
                     S2 = Split(S(j), ",") '将此包含多个省份的项目用逗号分割出来
           For k = 0 To UBound(S2) '循环枚举此项目下的各别省份
              If Trim(S2(k)) = "" Then Exit For '空的省份则退出循环
                 If InStr(TmpStr, S2(k)) = 0 Then '在缓冲变量TmpStr中查找, 如果 "不存在" 这个省份
                     Combo2.AddItem S2(k) 'Combo2添加这个省份
                     TmpStr = TmpStr & "," & S2(k) '缓冲变量TmpStr再加入这个省份
                 End If
          Next k
          Next j
          Next i
      Combo1.Text = "==寄件人省份==": Combo2.Text = "==收件人省份=="
      
      '========================================================
      '修改 Combobox 的下拉菜单高度
       cd = Screen.TwipsPerPixelY  '每象素的缇数
       cd1 = 300 '下拉框高度,这个值自已定
         MoveWindow Combo1.hwnd, Combo1.Left / cd, Combo1.Top / cd, Combo1.Width / cd, cd1, True
         MoveWindow Combo2.hwnd, Combo2.Left / cd, Combo2.Top / cd, Combo2.Width / cd, cd1, True
    '========================================================End SubPrivate Sub Command1_Click()
          If Combo1.Text = "==寄件人省份==" Then MsgBox "请选择寄件人省份!": Exit Sub '如果Combo1没选择 退出本事件
                aa = INIReadTitle(Combo1.Text, vbNullString, IniName) '读进Combo1的地区纲要
                S = Split(aa, Chr(0)) '使用每一个项目的分隔符Chr(0) 分开每一个独立的项目
                For i = 0 To UBound(S) '循环枚举此纲要下所有独立的项目
                     If InStr(S(i), Combo2.Text) > 0 Then '判断这个项目下是否包含Combo2所选择的省份
                         bb = S(i) '将项目带入变量bb
                          'Label2.Caption = INIRead(Combo1.Text, bb, IniName) '从地区纲要与省份两条件中读进 值
                           x = INIRead(Combo1.Text, bb, IniName) '从地区纲要与省份两条件中读进 值
                                
                If Val(Text1.Text) > 0 And Val(Text1.Text) <= 0.5 Then Text2.Text = "20": Exit Sub
                     For ii = 0.5 To 39.5 Step 0.5
                         If Val(Text1.Text) > ii And Val(Text1.Text) <= ii + 0.5 Then Text2.Text = CStr(20 + x * ((ii + 0.5) * 2 - 1)): Exit For '退出循环
                Next ii
                
                Exit For '退出循环
          End If
                Next i
    End Sub
      

  30.   

    32楼的代码和31楼的代码怎么会一样???For ii = 0.5 To 39.5 Step 0.5
      If Val(Text1.Text) > ii And Val(Text1.Text) <= ii + 0.5 Then
         Text2.Text = CStr(20 + x * ((ii + 0.5) * 2 - 1))
         Exit For '退出循环
       End If
    Next ii这样你可以看得清楚明白些你的CPU卡与如何获取 ini 与你的if 判断 扯不上关系, 问题出在你其它代码的部份.
      

  31.   

    34F 的 Command1_Click代码写规范些 你可以看出你的问题在哪里
      

  32.   

    我看出的问题好像是把你给的那句循环语句插到 For i = 0 To UBound(S) 这句循环语句的里面了,但是我把你给的那循环语句插入到Command1_Click的最后还是没用,一运行程序就卡的只能强制关闭VB
      

  33.   

    新建工程,添加combo1,combo2,text1,command1Dim s(30, 30) As String
    Private Sub Command1_Click()
        Text1.Text = s(Combo1.ListIndex, Combo2.ListIndex)
    End Sub
    '你只提供了2个省份的数据,代码也只有发货省市为广东广西的结果才正确,其他的都是5
    Private Sub Form_Load()
    Dim i As Byte, n As Byte, ShengShi, Sheng As String
    Sheng = "安徽,北京,福建,甘肃,广东," & _
            "广西,贵州,海南,河北,河南," & _
            "黑龙江,湖北,湖南,吉林,江苏," & _
            "江西,辽宁,内蒙古,宁夏,青海," & _
            "山东,山西,陕西,上海,四川," & _
            "天津,西藏,新疆,云南,浙江,重庆"
    ShengShi = Split(Sheng, ",")
    For i = 0 To 30
        Combo1.AddItem ShengShi(i)
        Combo2.AddItem ShengShi(i)
        For n = 0 To 30
            
            Select Case i
                Case 5 '广西
                    Select Case n
                        Case 5, 6, 7 '表示广西,贵州,海南
                            s(i, n) = 4
                        Case 1, 3, 18, 25, 26
                            s(i, n) = 10
                        Case 9, 14, 20, 22, 23, 29
                            s(i, n) = 9
                        Case 8, 10, 13, 16, 17, 19, 21, 27
                            s(i, n) = 17
                        Case Else
                            s(i, n) = 6
                    End Select
                Case 4 '广东
                    Select Case n
                        Case 4
                            s(i, n) = 4
                        Case 1, 8, 20, 21, 22, 25
                            s(i, n) = 9
                        Case 3, 17, 18, 19
                            s(i, n) = 10
                        Case 10, 13, 16, 26, 27
                            s(i, n) = 17
                        Case Else
                            s(i, n) = 6
                    End Select
                Case Else
                    s(i, n) = 5
            End Select
        Next n
    Next i
    End Sub