我正在與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個接受按鈕? –