我有一个表格OpenFileDialog用于选择图像并显示它pictureBox.在表单打开之前,用户可以打开然后根据需要多次保存打开的图像.我想要做的是,在每次新的选择 - 保存之后,删除之前保存的图像(如果有的话).问题是,当我实现代码时,我能够第一次删除图像,如果我继续使用当前打开的表单保存图像,我会收到资源正在使用的错误.我所做的是Dispose()图像,但我想我不是在正确的地方做的.
这是我打开和加载图像的方式:
private void btnExplorer_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Select file";
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = fileNameFilter;
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FileName = prefixFilter;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
pictureBox1.InitialImage = new Bitmap(openFileDialog1.FileName);
pictureBox1.ImageLocation = openFileDialog1.FileName;
selectedFile = pictureBox1.ImageLocation;
selectedFileName = openFileDialog1.SafeFileName;
pictureBox1.Load();
}
catch (Exception ex)
{
logger.Error(ex.ToString());
MessageBox.Show("Error loading image!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在同一个类中,我有这个方法,如果我需要删除旧图像,我会调用它:
public void …Run Code Online (Sandbox Code Playgroud) 今天我注意到我的代码中有一个错误,我以为我是在不久前处理过的.我正在处理Windows Forms应用程序,我form_load经常使用该事件.为了处理Form_load事件中出现错误并抛出异常的情况,我使用以下代码:
catch (Exception ex)
{
LogErrorAndShowMessage(ex, Resources.ERROR_LOAD);
//Otherwise Error : "Cannot call Close() while doing CreateHandle()".
this.BeginInvoke(new InvokeDelegate(CloseTheForm));
LoadForm<Materials>(closeAlreadyOpened : false);
}
Run Code Online (Sandbox Code Playgroud)
这就是我如何声明委托和方法:
public delegate void InvokeDelegate();//used if form load fails
private void CloseTheForm()
{
this.Close();
}
Run Code Online (Sandbox Code Playgroud)
从今天起它运作良好.发生了什么 - 在Form_load我设置DataSource我的ComboBoxes 的事件中,碰巧从SetDataSource方法中抛出了一个错误,最终得到的错误与我在使用之前得到的相同BeginInvoke- Cannot call Close() while doing CreateHandle().
这是一种例外行为,如果从其他地方抛出异常代码仍然正常工作,只有在设置数据源期间我得到了错误.
任何可能导致这种异常行为的建议以及如何解决?
PS
这就是我创建新表单的方式:
protected void LoadForm<T>(ToolStripButton/*ToolStripDropDownButton*/ formButton,
string buttonText,
long? loadEntityId = null,
bool closeAlreadyOpened = …Run Code Online (Sandbox Code Playgroud) 我有这个问题:
var sole = SoleService.All().Where(c => c.Status != 250)
.Select(x => new {
ID = x.ID, Code = new {CodeName = x.Code + x.Country},
Name = x.Name
})
.Distinct()
.OrderBy(s => s.Code);
Run Code Online (Sandbox Code Playgroud)
像这样它给我一个错误.我想要的是组合Code和Country喜欢concat字符串,所以我可以使用新的变量作为我的数据源.就像是 -001France
PS
我正在使用并正在运行的是:
var sole = SoleService.All().Where(c => c.Status != 250)
.Select(x => new {
ID = x.ID, Code = x.Code, Name = x.Name, Country = x.Country })
.Distinct()
.OrderBy(s => s.Code);
Run Code Online (Sandbox Code Playgroud)
所以我需要修改这个查询,以便我可以Code +Country用作一个变量.以上是我认为可行的尝试.
情况就是这样 - 我需要使用a的Text属性,ToolStripItem我需要在此之前清除字符串中的所有空格.但是我尝试了三种非常常见的(在我看来)场景,它们都没有返回没有空格的字符串.这是我尝试过的:
string tempBtnText = tempItem.Text;
Run Code Online (Sandbox Code Playgroud)
tempBtnText在我使用Text属性的方法中定义.我觉得这样比较容易.然后我尝试了那些:
tempBtnText.Replace(" ", String.Empty);
tempBtnText = Regex.Replace(tempItem.Text, @"^\s*$\n", string.Empty);
string tempBtnTexts = Regex.Replace(tempItem.Text, @"\s+", "");
Run Code Online (Sandbox Code Playgroud)
所有这些都返回了原始形式的字符串(带有空格).删除空格的唯一方法是使用此方法:
public string RemoveWhitespace(string input)
{
return new string(input.ToCharArray()
.Where(c => !Char.IsWhiteSpace(c))
.ToArray());
}
Run Code Online (Sandbox Code Playgroud)
我在这里发现了类似的帖子SO.但我真的不明白为什么上述所有方法都不起作用.我开始认为与我正在使用ToolStripItemText属性这一事实有关,但正如我刚才所说,我声明了我自己的字符串变量,该变量取得了Text属性的值.
我不知道.有人能告诉我,这种行为的原因是什么.并不是说使用另一种方法清除空白区域是一个很大的问题,但是不工作的选项更加紧凑和可读,我想尽可能使用其中一种方法.
我有一个从字母一个预定义的字符串数组A到Q:
string[] SkippedAreasArray = new string[] {"A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"};
Run Code Online (Sandbox Code Playgroud)
在TextBoxWindows窗体内部,用户可以像这样输入skippedAreas:A,B,C,D...有验证和限制只能使用字母和逗号,因此输入保证采用这种格式.
我所做的是获取用户输入并填充另一个字符串数组:
string[] SkippedAreasFromForm = new string[17];
...
SkippedAreasFromForm = (txtSkippedAreas.Text).Split(',');
Run Code Online (Sandbox Code Playgroud)
现在是我寻求帮助的棘手部分.用户必须输入Number of areas例如- 3.这意味着他与唯一的工作A,B和C.如果区域的数量为2,那么他只能用A和B,如果区域的数量为4,然后A,B,C并D提供等.
我需要的是检查SkippedAreasFromForm填充了用户输入的数组中是否存在与上述条件不匹配的区域.
这在编码方面意味着什么 - 我需要从中获取每个元素 SkippedAreasFromForm,从预定义中获取它的整数值,SkippedAreasArray并查看该值是否等于或大于(> =)他输入的值为"区域数".如果存在超出所选数字范围的区域,则应显示错误.
我现在拥有的是:
foreach (string …Run Code Online (Sandbox Code Playgroud) 我想在我的asp.net mvc 3视图中使用一些虚拟数据.以下代码是应将数据传递给视图的控制器的一部分.
List<KeyValuePair<int, int>> dummyData = new List<KeyValuePair<int, int>>();
dummyData.Add(new KeyValuePair<int, int>(1,1));
dummyData.Add(new KeyValuePair<int, int>(1,2));
dummyData.Add(new KeyValuePair<int, int>(2,1));
dummyData.Add(new KeyValuePair<int, int>(3,1));
dummyData.Add(new KeyValuePair<int, int>(4,1));
dummyData.Add(new KeyValuePair<int, int>(4,2));
dummyData.Add(new KeyValuePair<int, int>(4,3));
dummyData.Add(new KeyValuePair<int, int>(4,4));
Run Code Online (Sandbox Code Playgroud)
顾名思义,这是我的虚拟数据.这背后的想法是第一个数字表示RowNumber形式表,第二个数字表示ColumnNumber.我想以某种方式组合与同一行相关但具有不同ColumnNumbers的记录.为此,我选择使用二维数组:
int dummyCount = dummyData.Count;
List<KeyValuePair<int, int>>[,] dummyArray = new List<KeyValuePair<int, int>>[dummyCount,dummyCount];
int index1 = -1;
int index2 = 0;
for (int i = 0; i < dummyCount; i++)
{
int tempColNum = 1;
if (dummyData[i].Value != tempColNum)
{
dummyArray[index1, index2].Add(dummyData[i]);
index2++;
}
else
{
index1++; …Run Code Online (Sandbox Code Playgroud) 我知道这是非常标准的过程,但由于对表创建方式的一些自定义要求,我很难找到从表中读取数据并将其传递回控制器的方法.
我使用数据库中的记录构建我的表,每个记录包含rowNo和columnNo,问题是每个raw可能具有不同的列数.我找不到一个可以构建它的正确的插件,所以我做了一个简单的方法,创建一个我传递给视图的字符串.
只是想知道我在这里谈论的是代码中的一个片段,你可以看到我如何创建<td>..</td>:
sb.Append("<td>" + innerArray[a][0].QuestionText +
"<input type='textbox' value=" +
innerArray[a][0].FieldValue + "></td>");
Run Code Online (Sandbox Code Playgroud)
我知道使用StringBuilder不是很聪明,+但现在它可以工作.因此,在构建我的字符串并将其传递给视图后,我得到了这个:

这是来源View page source:
<tr>
<td>alabala<input type='textbox' value=></td>
<td colspan=2><input type='textbox' value=yes></td>
</tr>
<tr>
<td colspan=3>alabala<input type='textbox' value=yes></td>
</tr>
<tr>
<td colspan=3>alabala<input type='textbox' value=yes></td>
</tr>
<tr>
<td>alabala<input type='textbox' value=no></td>
<td>alabala<input type='textbox' value=no></td>
<td>alabala<input type='textbox' value=no></td>
</tr>
<tr>
<td>alabala<input type='textbox' value=no></td>
<td colspan=2><input type='textbox' value=no></td>
</tr>
<tr>
<td colspan=3>alabala<input type='textbox' value=></td>
</tr>
<tr>
<td colspan=3>alabala<input type='textbox' value=no></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
这就是你如何看待它 - 一个包含整个HTML代码的长字符串. …
我正在向服务器提交表单,如果一切正常,我想为用户显示某种确认消息.
问题是我正在使用ASP.NET MVC 4我可以做到的地方:
@if (ViewBag.ConfirmMessage != null)
{
<p>@ViewBag.ConfirmNMessage</p>
}
Run Code Online (Sandbox Code Playgroud)
但这样消息仍然存在,我认为这会让用户感到困惑.如果我使用类似的东西:
@if (ViewBag.ConfirmMessage != null)
{
<script>
alert("Some message");
</script>
}
Run Code Online (Sandbox Code Playgroud)
但首先alert与显示一个段落的工作方式不同,而且ViewBag.ConfirmMessage已经有我要显示的消息,将来如果我显示消息而ViewBag.ConfirmMessage不是在每个视图中写一些消息,它将更易于维护.
所以我的问题是<p>@ViewBag.ConfirmNMessage</p>,X如果显示,我可以在几秒钟后隐藏起来?
我正在寻找高效的随机发生器.我更喜欢成为字符串/ int生成器,即使它并不重要.我需要复制图像并将其名称保存在数据库中.因为我从数据库中提取名称所以不需要有一些有意义的名称,但是因为我需要在目录中复制图像,所以我必须为我的图像名称选择唯一的名称.因为每次检查都是对数据库的新查询,所以我需要一些有效的随机生成器,以便在获取唯一名称之前我需要最少数量的查询.我知道有很多算法,但快速搜索并没有帮助我找到一个.我不想要一些过度杀戮的东西,毕竟我不加密任何我只需要唯一名称的东西,但是当图像数量变大时我仍然不希望生成新的唯一名称很多时间.