我的双显示器系统上的DataGridView遇到了一个非常奇怪的问题.该问题表现为对控件的极快重复(如完全重绘的30秒),但只有在我的某个屏幕上时才会显示.在另一方面,重绘速度很好.
我有一个Nvidia 8800 GT,带有最新的非beta驱动程序(175件).这是驱动程序错误吗?我会把它留在空中,因为我必须忍受这种特殊的配置.(它不会发生在ATI卡上,但......)
绘画速度与单元格内容无关,自定义绘图根本不会改善性能 - 即使只绘制实心矩形也是如此.
我后来发现在表单上放置一个ElementHost(来自System.Windows.Forms.Integration命名空间)可以解决问题.它不必被搞砸; 它只需要是DataGridView所在的表单的子级.只要Visible属性为true ,就可以将其大小调整为(0,0).
我不想明确地将.NET 3/3.5依赖项添加到我的应用程序中; 我创建了一个方法来在运行时创建此控件(如果可以)使用反射.它工作正常,至少它在没有所需库的机器上优雅地失败 - 它只是变得缓慢.
这个方法还允许我在应用程序运行时应用修复,这样可以更容易地看到WPF库在我的表单上发生了什么变化(使用Spy ++).
经过大量的试验和错误,我注意到在控件本身上启用双缓冲(而不仅仅是表单)可以解决问题!
因此,您只需要根据DataGridView创建一个自定义类,以便启用其DoubleBuffering.而已!
class CustomDataGridView: DataGridView
{
public CustomDataGridView()
{
DoubleBuffered = true;
}
}
Run Code Online (Sandbox Code Playgroud)
只要我的所有网格实例都使用这个自定义版本,一切都很好.如果我遇到由此引起的情况,我无法使用子类解决方案(如果我没有代码),我想我可以尝试将该控件注入表单:)(虽然我'将更有可能尝试使用反射来强制从外部启用DoubleBuffered属性再次避免依赖).
令人遗憾的是,这么简单的事情花了我很多时间......
我试过这个:
string newScript = textBox1.Text;
HtmlElement head = browserCtrl.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = browserCtrl.Document.CreateElement("script");
lblStatus.Text = scriptEl.GetType().ToString();
scriptEl.SetAttribute("type", "text/javascript");
head.AppendChild(scriptEl);
scriptEl.InnerHtml = "function sayHello() { alert('hello') }";
Run Code Online (Sandbox Code Playgroud)
scriptEl.InnerHtml和scriptEl.InnerText都给出了错误:
System.NotSupportedException: Property is not supported on this type of HtmlElement.
at System.Windows.Forms.HtmlElement.set_InnerHtml(String value)
at SForceApp.Form1.button1_Click(Object sender, EventArgs e) in d:\jsight\installs\SForceApp\SForceApp\Form1.cs:line 31
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at …Run Code Online (Sandbox Code Playgroud) 我已经和它斗争了一段时间,并且发现许多其他人也在使用TableLayoutPanel(.net 2.0 Winforms).
问题
我试图采用一个'空白'tablelayoutpanel,它定义了10列,然后在运行时以编程方式添加控件行(即每个单元一个控件).
人们可能认为它应该如此简单
myTableLayoutPanel.Controls.Add(myControl, 0 /* Column Index */, 0 /* Row index */);
Run Code Online (Sandbox Code Playgroud)
但是(对我来说)不添加行.所以也许可以添加一行样式
myTableLayoutPanel.RowStyles.Clear();
myTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.我已经挖掘并发现myTableLayoutPanel.RowCount使用情况从设计时间变为运行时间,因此myTableLayoutPanel.RowCount++;实际上并没有添加另一行,甚至在添加RowStyle条目之前/之后!
我遇到的另一个相关问题是控件将被添加到显示器,但它们都只是在TableLayoutPanel的点0,0处渲染,另外它们甚至不被限制在它们应该是的Cell边界内.显示在内(即使用Dock = DockStyle.Fill它们仍然显得太大/太小).
有人有一个在运行时添加行和控件的工作示例吗?
我有一个ContextMenuStrip分配给几个不同的列表框.我想ContextMenuStrip知道什么时候单击ListBox它使用它.我尝试下面的代码作为开始,但它不起作用.在sender有正确的价值,但是当我试图把它分配给了menuSubmitted它为空.
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ContextMenu menuSubmitted = sender as ContextMenu;
if (menuSubmitted != null)
{
Control sourceControl = menuSubmitted.SourceControl;
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒.谢谢.
使用下面的帮助,我发现了:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
ContextMenuStrip calendarMenu = menuItem.Owner as ContextMenuStrip;
if (calendarMenu != null)
{
Control controlSelected = calendarMenu.SourceControl;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个C#webforms应用程序,直到今天才一直在游泳.
现在,突然间,每次我尝试运行应用程序时,都会出现文件锁定错误:
无法将文件"obj\Debug\MyProject.exe"复制到"bin\Debug\MyProject.exe".该进程无法访问文件"bin\Debug\MyProject.exe",因为它正由另一个进程使用.
谷歌搜索错误没有提出超出明显的任何东西,即VS认为文件被锁定.它肯定是Visual Studio本身锁定文件,因为当我关闭VS并重新打开它时,项目执行正常 - 第一次.当我第二次尝试运行它时,我收到文件锁定错误.
每次我想运行应用程序时关闭VS并重新打开都不是一个可行的解决方法!如何找出锁定文件的内容,并阻止其被锁定?
编辑:另一个有趣的发现:我甚至不必运行该应用程序.只需编译一次就会导致文件锁定; 我不能连续两次编译!
此问题特定于我的解决方案中的一个项目.所有其他项目都可以正常工作,并且可以根据需要执行多次.只有这一个项目才会被锁定.
到目前为止,我只是在程序Application.Run的Program.cs入口点周围放置一个try/catch块.这会在调试模式下很好地捕获所有异常,但是当我在没有调试模式的情况下运行程序时,不再处理异常.我得到了未处理的异常框.
我不希望这种情况发生.我希望在非调试模式下运行时捕获所有异常.该程序有多个线程,最好是来自它们的所有异常都被同一个处理程序捕获; 我想记录数据库中的异常.有没有人对如何做到这一点有任何建议?
我正在向WinForms ListView添加几千个(例如53,709个)项.
尝试1:13,870 ms
foreach (Object o in list)
{
ListViewItem item = new ListViewItem();
RefreshListViewItem(item, o);
listView.Items.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
这非常糟糕.明显的第一个解决方法是打电话BeginUpdate/EndUpdate.
尝试2:3,106 ms
listView.BeginUpdate();
foreach (Object o in list)
{
ListViewItem item = new ListViewItem();
RefreshListViewItem(item, o);
listView.Items.Add(item);
}
listView.EndUpdate();
Run Code Online (Sandbox Code Playgroud)
这样更好,但仍然是一个数量级太慢.让我们将ListViewItems的创建与添加ListViewItems分开,因此我们找到了真正的罪魁祸首:
尝试3:2,631 ms
var items = new List<ListViewItem>();
foreach (Object o in list)
{
ListViewItem item = new ListViewItem();
RefreshListViewItem(item, o);
items.Add(item);
}
stopwatch.Start();
listView.BeginUpdate();
foreach (ListViewItem item in items)
listView.Items.Add(item));
listView.EndUpdate();
stopwatch.Stop() …Run Code Online (Sandbox Code Playgroud) 有没有办法在Windows应用程序中显示控制台?
我想做这样的事情:
static class Program
{
[STAThread]
static void Main(string[] args) {
bool consoleMode = Boolean.Parse(args[0]);
if (consoleMode) {
Console.WriteLine("consolemode started");
// ...
} else {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Run Code Online (Sandbox Code Playgroud) 这些是你可以添加到项目中的三个不同的东西,我不太确定我是否理解这些差异.例如,在使用a时,它们似乎都显示在组件工具箱中Form.每种方法有哪些常见的使用方案?有什么不同?
I need to change the icon in the application I am working on. But simply browsing for other icons from the project property tab -> Application -> Icon, it is not getting the icons stored on the desktop..
What is the right way of doing it?
winforms ×10
c# ×9
.net ×4
performance ×2
components ×1
console ×1
controls ×1
datagridview ×1
debugging ×1
javascript ×1
listview ×1
nvidia ×1
runtime ×1