如果语句带有变量字符串输入 [英] If statement with variable string input
问题描述
我想在userform上创建一个用户输入,用户可以在其中定义逻辑if语句。例如,文本框可以是用户可以输入以下逻辑(H1> a)或(H2> a和H3< b)。>
这应该自动添加到if语句。
如有任何建议,请
我尝试了什么:
我使用了很多文本框并修改/限制了文本框中的输入,例如文本框1可以,你可以只选择带>的H1标志等,但这太有限了。
不同的方法
看看 DataTable.Select方法(字符串)(System.Data) [< a href =https://msdn.microsoft.com/en-us/library/det4aw50%28v=vs.110%29.aspx\"target =_ blanktitle =New Window> ^ ]
获取与给定表达式匹配的所有行
很多可能性如H1就像F *
看看这个CodeProject文章: Dynamic Expresso [ ^ ]
它是用C#编写的,但你可以把它用作VB.NET的DLL。
它允许你要做这样的事情(也是C#,我不是流利的机智h VB.NET,我希望你能读懂它):
string expression = // 表达式,例如(H1> 1)或(H2> 1和H3 <2)
var interpreter = new Interpreter();
interpreter.SetVariable( H1,dataTable.Rows [currentRow] [ H1]);
interpreter.SetVariable( H2,dataTable.Rows [currentRow] [ H2]);
interpreter.SetVariable( H3,dataTable.Rows [currentRow] [ H3]);
var detectedIdentifiers = interpreter.DetectIdentifiers(expression);
if (detectedIdentifiers.UnknownIdentifiers.Any())
{
// 显示有关无效标识符的错误消息
// (例如,用户输入H4或完全不同的东西)
}
bool result = interpreter.Eval< bool>(表达式);
Good Evening. I have datatable with 5 columns through which I loop. Within the loop is an if statement checking the entry (row) in each of the columns individually and giving a certain outcome. The columns are defined as H1, H2, H3, H4, H5.
I want to create on the userform an user input, where the user can define the logic of the if statement. For instance, a text box maybe, that the user can input the following logic (H1>a) or (H2>a And H3<b).>
This should then be added to the if statement automatically.
Any suggestions, please
What I have tried:
I have using a lot of text box and fixing/limiting the inputs in the textbox, for instance textbox 1 can you can only choose H1 with > signs etc, but this is too limited.
different approach
have a look at DataTable.Select Method (String) (System.Data)[^]
gets you all the rows matching the given Expression
lots of possibility's likeH1 like "F*"
Take a look at this CodeProject article: Dynamic Expresso[^]
It's written in C# but you can use it as a DLL from VB.NET.
It allows you to do something like this (also C#, I'm not "fluent" with VB.NET, I hope you can read it):
string expression = // expression entered by user, e.g. "(H1>1) or (H2>1 and H3<2)" var interpreter = new Interpreter(); interpreter.SetVariable("H1", dataTable.Rows[currentRow]["H1"]); interpreter.SetVariable("H2", dataTable.Rows[currentRow]["H2"]); interpreter.SetVariable("H3", dataTable.Rows[currentRow]["H3"]); var detectedIdentifiers = interpreter.DetectIdentifiers(expression); if (detectedIdentifiers.UnknownIdentifiers.Any()) { // show error message about invalid identifiers // (e.g. user entered "H4" or something completely different) } bool result = interpreter.Eval<bool>(expression);
这篇关于如果语句带有变量字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!