从表单到类获取组合框文本 [英] Geting combobox text from form to class

查看:79
本文介绍了从表单到类获取组合框文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多,但是看不到找到我问题的答案.我想从form1的组合框中获取选定的文本,并使用它从类文件中搜索文件.我希望做的一个例子是

 如果(line.Contains(Form1.Combobox1.SelectedItem))
  {
    /*  在此处搜索代码*/
  } 



其中Combobox1.SelectedItem是在form1上选择的.我是C#的新手,可以在Vb中完成此操作,但是想学习一种新的语言.目前尚不清楚为什么要使用Contains.看起来您只需要一个选定项目的文本即可:

 对象 selectedItem = Form1.Combobox1.SelectedItem;
如果(selectedItem!= ){// 重要检查
    字符串 selectedText = selectedItem.ToString();
    //  ... 
} 



请帮个忙:请不要留下难看的自动生成的名称,例如Form1Combobox1,将它们重命名为所有语义名称.这些自动生成的名称甚至违反Microsoft命名约定-通过FxCop测试您的程序集是否可见.为什么觉得您有一个重构引擎?

—SA


进行了更多搜索,我找到了编制代码的方法.正如我说的那样,我仍在学习,所以我现在虽然糊涂了,但充其量是肮脏的,但我的主旨是.我不会张贴修复原因,因为我说它确实很脏.

@SAKryukov
谢谢你的指导.自动生成的MS名称只是我要执行的操作的一个示例(认为控件的类型将与答案有关).我所指的类文件"是当您向项目中添加类"时,因此我的所有代码都不会在表单"文件中.至于FxCop,我似乎在安装它时遇到了问题,但这就是另一个话题.至于vb或vb.net,我以为vb.net,但是在研究了2个im之间的区别之后,我不得不说只是普通的vb.


使用此

 字符串 a = Form1.Combobox1.Text; // 在这里,您将获得在comboBox中选择的选定文本
// 希望这对您有帮助 



那么您可以将"a"变量分配给搜索类


I have searched and searched but cant see to find an answer to my problem. I want to get the selected text from a combobox on form1 and use it to search in a file from a class file. an example of what im looking to do is

if (line.Contains(Form1.Combobox1.SelectedItem))
  {
    /*search code here*/
  }



where Combobox1.SelectedItem is what was chose on form1. I''m new to C# and could accomplish this in Vb but am wanting to learn a new language.

解决方案

The question is not clear; and it is not clear why do you use Contains. It looks like you just need a text of a selected item:

object selectedItem = Form1.Combobox1.SelectedItem;
if (selectedItem != null) { //important check
    string selectedText = selectedItem.ToString();
    //...
}



Please do yourself a favor: never leave ugly auto-generated names like Form1, Combobox1, renamed all of them to give them some semantic names. Those auto-generated names even violate Microsoft naming conventions — test your assembly by FxCop to see. Why do you thing you have a refactoring engine?

—SA


A bit more searching and i was able to find a way to work out the code. as i said i''m still learning so what i muddle though at this point is dirty at best, but i am getting the gist of it. i wont post my fix cause as i said it''s real dirty.

@SAKryukov
thanks for the direction. the auto-generated MS names were just an example of what i was wanting to do(thought the type of control would be relevant to the answer). the "class file" i was referring to was when you add a "Class" to the project so all my code would not be in the "Form" file. As for FxCop i seem to have a problem installing it but thats another topic. As for the vb or vb.net, i thought vb.net but after looking into the differences between the 2 im gonna have to say just plain-old vb.


Use this

string a=Form1.Combobox1.Text; //Here you will get the selected text that you have choose in comboBox 
//Hope this will help you



then you can assign the ''a'' variable to your search class


这篇关于从表单到类获取组合框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆