动态对话框
我正在与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
更加动态的在规模和所显示的内容?如何在不指定大小和位置的情况下创建新表单?但仍然不是一团糟?
制作具有一定尺寸的新窗体。然后将一个FlowLayoutPanel添加到与窗体几乎相同的宽度和几乎相同的高度。留出足够的空间,你所需要的按钮:
在面板属性中设置的领域AutoSize
为true,AutoSizeMode
到GrowAndShrink
:
不要忘记指定FlowDirection
:
this.panel.FlowDirection = FlowDirection.TopDown;
现在,你只需要它增加了你的提示控制的FlowLayoutPanel
的控制(这将在自动方式进行订购)的方法:
public void AddToCanvas(string text)
{
this.flowLayoutPanel1.Controls.Add(new Label() {Text = text});
this.flowLayoutPanel1.Controls.Add(new TextBox());
Resize();
}
和resize方法来调整形式电流控制量里面:
public void Resize()
{
Size s = new Size();
s.Height = this.flowLayoutPanel1.Height + this.button_Accept.Height +
(this.flowLayoutPanel1.Controls.Count * 10) + y_offset;
s.Width = this.flowLayoutPanel1.Width + 10;
this.MaximumSize = s;
this.Size = s;
}
有了这个输入:
随机文本
$名称
$地址
随机文本
$年龄
随机文本
$星球
$大陆
随机文本
$ StarSystem
我得到以下结果:
编辑:
创建一个变量你(通过线你循环之前)读取一个文件的内容后:
DynamicPrompt dp = new DynamicPrompt("YourCaption");
for (int i = 0; i < alllines.Length; i++)
{
if (alllines[i].Contains("$"))
{
开始循环,如果你到了重要的一行致电
dp.AddToCanvas(PromptText);
谢谢你,我只是有点困惑,我会如何在我的循环中调用它。 – Tom
@Tom我做了一个编辑,看看。您需要为每个文件创建一个新表单,然后您可以在要添加的每个重要行上调用add方法 –
@Tom您是否还在努力将其应用于您的问题? –
那么你的问题是什么?目前尚不清楚我们如何提供帮助。 –
@RobAnthony,我编辑了我的问题,我希望这有助于。对不起,我对C#有点新了, – Tom
现在还不完全清楚。所以想象你的文件中有3个'$'值。这应该导致3个文本框来自提示?整个表单应该有1个接受按钮? –