在我的 WinForm 上,我希望用户能够在单击按钮时“重置”或“清除”日期时间选择器。我的目标是将其设置为 00:00 AM,但我无法这样做。
我试过了:
dateTimePicker1.Value = ("00:00: AM");
int reset = int.Parse("00:00: AM");
dateTimePicker1.Value = reset;
Run Code Online (Sandbox Code Playgroud)
日期时间选择器采用自定义格式: hh:mm:tt
任何帮助表示赞赏!
我正在使用Visual Studio 2010; 通常当我想添加鼠标点击以外的事件时,我会转到事件选项卡并查找事件,单击它,它会将我带到代码中的那个位置.
但是,我找不到任何Closing表格的事件.
我已经尝试手动添加它,但我认为VS正在处理的幕后更多的东西:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)
什么是为FormClosing事件"写"所有内容的最有效方法?或者我如何必须手动输入?
这是WinForms的形式.
我有点困惑.如果系统的当前用户名是!=Administrator或Administrator2,我想隐藏一个按钮,但实现我想要的目标的唯一方法是使用&&而不是||.
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
if
(userName != @"PC\Administrator" && userName != @"PC\Administrator2")
{
button2.Hide();
}
Run Code Online (Sandbox Code Playgroud)
但是,在我的形式打开的另一个地方button2,如果我使用==它然后似乎工作?
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
var adminform = new AdminForm();
if (userName == @"PC\Administrator" || userName == @"PC\Administrator2")
{
adminform.Show();
}
Run Code Online (Sandbox Code Playgroud)
知道为什么我在第一个例子中使用||时无法使用!=?
我试图在线添加一行文本到txt文件,但我尝试的代码是替换文件中的所有txt而不是添加新行,即使在添加之后Enviorment.Newline.
这是我尝试过的:
private void submithsbtn_Click(object sender, EventArgs e)
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
string peak = peakvaluelbl.Text;
string dir = @"ftp://example.com/file.txt";
string name = nametbox.Text;
client.UploadString(dir, name + " recived a peak score of: " + peak + Environment.NewLine);
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能够达到这个结果?
我正在尝试将字符串的第一个字母更改为大写
我在这方面看到了其他问题,但即使我应用了他们所说的内容,我仍然无法管理正确的结果.
public string FirstLetterToUpper(string str)
{
if (str == null)
return null;
if (str.Length > 1)
return char.ToUpper(str[0]) + str.Substring(1);
return str.ToUpper();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "test text";
CapitalizeFirstLetter(label1.Text);
}
Run Code Online (Sandbox Code Playgroud)
而不是输出
Test text
Run Code Online (Sandbox Code Playgroud)
它仍然存在
test text
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我的目标是在用户的本地IP无法启动时执行代码10.80.
我找不到一种不容易出错的方法,例如:
这是我必须得到的IP:
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
}
}
iplabel.Text = localIP;
Run Code Online (Sandbox Code Playgroud)
然后我尝试将其转换为a int来检查它是否是<或>:
string ipstring = iplabel.Text.Replace(".", "");
int ipnum = int.Parse(ipstring);
if (ipnum > 1080000000 && ipnum < 1080255255)
{//stuff}
Run Code Online (Sandbox Code Playgroud)
但问题是,如果有一个2位数的IP值,例如10.80.22.23它将无法正常工作,因为它检查的数字大于该范围.
有没有更好的解决方案来检查C#中int或IP地址的前x位数?
我试图增加一个int的值:counter每次stakebtn按下时增加1 .
这是我到目前为止所尝试的:
private void stakebtn_Click(object sender, EventArgs e)
{
int counter = 0;
counter++;
currentstakes.Text = counter.ToString();
}
Run Code Online (Sandbox Code Playgroud)
但是,这仅适用一次 - currentstakes标签不会超过1.
有人能够看到我错在哪里吗?