我正在与Winforms
一起工作,我有一个关于使这些更动态的问题。例如,我可以创建一个winform
,其中包含一组显示数据的文本框,但是我将如何实现这种动态?就用户可以看到的文本框数量而言,取决于发现了哪些数据?动态对话框
我知道我可以沿的下方的线做一些事情:
TextBox tb = new TextBox();
在我的情况我有过一堆文件读取,如果一个$
被找到,那么将出现一个提示,询问申请用户输入一个合适的值,但是,如果文档有很多值需要更新,那么这是很多对话框。因此,解决此问题的一个好方法是让对话框显示在最后(读取文件之后)所有需要更新的值,并且用户可以一次更新所有这些值。
我看到的问题是需要显示的值的数量可以是任何从1开始的值,这意味着循环需要考虑这一点。
我现在的代码如下:
foreach (FileInfo fi in rgFiles)
{
current++;
fileProcessBar.Value = current/count * 60 + 40;
string[] alllines = File.ReadAllLines(fi.FullName);
for (int i = 0; i < alllines.Length; i++)
{
if (alllines[i].Contains("$"))
{
// prompt
int dollarIndex = alllines[i].IndexOf("--");
Regex regex = new Regex(@"(--.{1,100})");
var chars = regex.Match(alllines[i]).ToString();
string PromptText = chars.Replace("-", "");
string promptValue = CreateInput.ShowDialog(PromptText, fi.FullName);
if (promptValue.Equals(""))
{
}
else
{
alllines[i] = alllines[i].Replace("$", promptValue);
File.WriteAllLines(fi.FullName, alllines.ToArray());
}
}
}
提示方法:
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 600,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top = 15, Width = 500, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 52, Width = 500 };
Button confirmation = new Button() { Text = "Add", Left = 450, Width = 100, Top = 72, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
prompt.MaximizeBox = false;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
我的问题是如何才能winform
更加动态的在规模和所显示的内容?如何在不指定大小和位置的情况下创建新表单?但仍然不是一团糟?
那么你的问题是什么?目前尚不清楚我们如何提供帮助。 –
@RobAnthony,我编辑了我的问题,我希望这有助于。对不起,我对C#有点新了, – Tom
现在还不完全清楚。所以想象你的文件中有3个'$'值。这应该导致3个文本框来自提示?整个表单应该有1个接受按钮? –