我有一些组合框设置为下拉列表,用户可以在其中选择一个数字.我也有一个清除按钮,应该清除组合框中的文本,但我似乎无法得到它.我试过了:
//doesn't work
cboxHour.Text = "";
Run Code Online (Sandbox Code Playgroud)
和
//doesn't work
cboxHour.ResetText();
Run Code Online (Sandbox Code Playgroud)
这似乎应该是如此直接,但我只是没有得到它.
我开发的所有程序都使用默认的Windows设计模板:

除了改变表单的颜色外,基本上我编写的每个程序都有这种布局.我想知道人们如何创建更多的自定义GUI.我尝试在谷歌上查找但我显然不知道如何用词来搜索.当我说自定义GUI时,我的意思是:

我认为它归结为定制什么负载,但我不知道在哪里看,或研究什么.
我试图通过使用计时器延迟我的方法中的事件,但我不一定了解如何使用计时器等待.
我将我的计时器设置为2秒,但是当我运行此代码时,最后一次调用运行没有2秒延迟.
Timer timer = new Timer();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * (2); // Timer will tick evert second
timer.Enabled = true; // Enable the timer
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "first";
timer.Start();
label1.Text = "second";
}
Run Code Online (Sandbox Code Playgroud)
因此,当我单击我的按钮时,它会立即将label1显示为"second",而不是更改为"first",等待2秒,然后更改为"second".我在这里阅读了许多关于使用计时器而不是thread.sleep的线程,但我似乎无法找到/弄清楚如何实际实现它.
我试图Trim()在多行字符串上使用,但只有第一行Trim().我似乎无法弄清楚如何从每行的开头删除所有空格.
string temp1 = " test ";
string temp2 = @" test
line 2 ";
MessageBox.Show(temp1.Trim());
//shows "test".
MessageBox.Show(temp2.Trim());
//shows "test"
" line2 ".
Run Code Online (Sandbox Code Playgroud)
我可以在多行字符串上使用Trim/ TrimStart/ TrimEnd吗?
我的程序根据程序是否第一次运行来设置其显示.为了确定程序是否第一次运行我当前正在使用a
//this boolean exists within my programs settings
Setting boolean FirstRun = True;
Run Code Online (Sandbox Code Playgroud)
当程序运行时,它调用一个方法来检查该bool值的状态并相应地采取行动:
if(Properties.Settings.FirstRun == true)
{ lblGreetings.Text = "Welcome New User";
//Change the value since the program has run once now
Properties.Settings.FirstRun = false;
Properties.Settings.Save(); }
else
{ lblGreetings.Text = "Welcome Back User"; }
Run Code Online (Sandbox Code Playgroud)
它似乎工作得非常有效,但是如果.exe文件被移动并从一个新位置启动它认为它是第一次运行,我担心我是以一种混乱的方式这样做,也许存在更高效测试首次运行的程序的方式.有一个更好的方法吗?
我在表单上有大约20个文本字段,用户可以填写.我想提示用户在任何文本框中输入任何内容时都要考虑保存.现在,对它的测试真的很长而且很混乱:
if(string.IsNullOrEmpty(txtbxAfterPic.Text) || string.IsNullOrEmpty(txtbxBeforePic.Text) ||
string.IsNullOrEmpty(splitContainer1.Panel2) ||...//many more tests
Run Code Online (Sandbox Code Playgroud)
有没有办法可以使用类似于任何数组的数组,其中数组是由文本框组成的,我是这样检查的?还有哪些方法可以非常方便地查看自程序启动以来是否有任何更改?
我应该提到的另一件事是有一个日期时间选择器.我不知道是否需要测试它,因为datetimepicker永远不会为null或为空.
编辑:我将答案纳入我的程序,但我似乎无法使其正常工作.我按如下所示设置测试并继续触发Application.Exit()调用.
//it starts out saying everything is empty
bool allfieldsempty = true;
foreach(Control c in this.Controls)
{
//checks if its a textbox, and if it is, is it null or empty
if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)))
{
//this means soemthing was in a box
allfieldsempty = false;
break;
}
}
if (allfieldsempty == false)
{
MessageBox.Show("Consider saving.");
}
else //this means nothings new in the form so we can close it …Run Code Online (Sandbox Code Playgroud) 我正在尝试自己学习Sharepoint,以便我们可以在我的工作中实现一个网站.我遇到的麻烦就是在服务器上创建一个它想要的网站.有没有办法在我的本地机器上创建一个网站,以便我可以开始修改一些设计功能?
我的程序有一个按钮,当点击它打开一个openfiledialog来选择一张图片:
private string ChoosePicture()
{
fDialog.Title = "Select Picture";
fDialog.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp; *.gif*;*.jpg";
fDialog.InitialDirectory = "C:";
fDialog.ShowDialog();
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
//returns a string for the directory
return fDialog.FileName.ToString();
}
Run Code Online (Sandbox Code Playgroud)
使用dialogresult框上的检查还没有解决我的问题:
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
DialogResult res = fDialog.ShowDialog();
if (res == DialogResult.OK)
{
//returns a string for the directory
return fDialog.FileName.ToString();
}
return null;
Run Code Online (Sandbox Code Playgroud)
如果我选择图片并完成文件选择,代码可以正常工作.但是,如果我在两者之间的任何时候取消该过程,我会得到例外情况"路径不是合法形式".我不确定哪个部分我想象我可以用a来处理这个问题try-catch,但是我不肯定哪个部分导致了这个问题?如果我try catch调用ChoosePicture()方法,我至少可以阻止它崩溃程序,但是当fdialogbox中没有选择图片时仍然会抛出异常.
要清除我的文本框,我在表单中使用以下代码:
foreach (Control c in this.Controls)
{
if (c is TextBox || c is RichTextBox)
{
c.Text = "";
}
}
Run Code Online (Sandbox Code Playgroud)
但现在我的文本框位于TabControl中.如何对文本框运行相同类型的检查,如果控件是文本框,则将值设置为"".我已经尝试过使用:
foreach(Control c in tabControl1.Controls)
Run Code Online (Sandbox Code Playgroud)
但这没效果.
我很好奇是否有办法只使用URL将某人发送到给定页面的特定行.Citebite执行此操作,但它链接到缓存页面而不是实际的实时页面.http://citebite.com/
您也可以使用PDF进行类似的操作,在其中为您指定页面#:链接到列出的pdf中的第6页. http://mises.org/journals/scholar/long.pdf#page=6
我已经做了很多寻找,我发现的只是在HTML中嵌入链接,这不是我想要的.理想情况下,我希望能够只发送一个跳转到特定文本行/突出显示它的URL(就像Citebite一样,但不必使用Citebite).