我想在每行的末尾有一个删除按钮,DataGridView然后单击我要从绑定列表中删除所需的行,该列是我的网格的数据源.
但我似乎无法做到这一点我在产品类中创建了一个按钮对象,并使用唯一的id实例化它以从列表中删除该对象.但是按钮没有显示在行中.
表单中有TextBox,用户可以输入文本,当他们按下Add按钮时,产品的新对象将使用提供的字段进行实例化,然后将其添加到BindingList.
最后,这个列表被绑定到,DataGridView并且细节显示在网格中.(我已经完成了这一部分).
最后,通过单击"保存"按钮,列表将保存在数据库中.
public class Product{
public string Brand { get; set; }
public int ProductPrice { get; set; }
public int Quantity { get; set; }
public product(string brand,int productPrice, int quantity){
this.Brand = brand;
this.ProductPrice = productPrice;
this.Quantity = quantity;
}
}
public partial class MainForm: Form{
.....
BindingList<Product> lProd = new BindingList<Product>();
private void btnAddProduct_Click(object sender, EventArgs e){
string Brand = txtProBrand.Text;
int Price = Convert.ToInt32(txtPrice.Text);
int Quantity = …Run Code Online (Sandbox Code Playgroud) 我对async/await编程很新,有时我觉得我理解它,然后突然发生了一些事情,并引发了我的循环.
我在测试winforms应用程序中尝试这个,这是我的一个版本的片段.这样做会阻止UI
private async void button1_Click(object sender, EventArgs e)
{
int d = await DoStuffAsync(c);
Console.WriteLine(d);
}
private async Task<int> DoStuffAsync(CancellationTokenSource c)
{
int ret = 0;
// I wanted to simulator a long running process this way
// instead of doing Task.Delay
for (int i = 0; i < 500000000; i++)
{
ret += i;
if (i % 100000 == 0)
Console.WriteLine(i);
if (c.IsCancellationRequested)
{
return ret;
}
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
现在,当我通过在Task.Run中包装"DoStuffAsync()"的主体进行轻微更改时,它的工作完全正常
private async Task<int> DoStuffAsync(CancellationTokenSource c)
{ …Run Code Online (Sandbox Code Playgroud) 视图1 - 只需输入参数
<p> Enter Year: @Html.TextBox("Year")</p>
<p> Enter Quarter: @Html.TextBox("Qtr")</p>
<p> Enter Division: @Html.TextBox("Div")</p>
<p><input id="Submit" type="button" value="button" /></p>
Run Code Online (Sandbox Code Playgroud)视图控制器2
namespace BSIntranet.Controllers
{
public class DivisionIncomeController : Controller
{
private ProjectionsEntities db = new ProjectionsEntities();
// GET: DivisionIncome
public ActionResult Index()
{
return View(db.JobRecaps.ToList());
}
}
}
Run Code Online (Sandbox Code Playgroud)我不知道在这里开始是什么或如何开始.谢谢你的帮助!!
编辑 使用系统; 使用System.Collections.Generic;
public partial class JobRecap
{
public int ID { get; set; }
public string Job_ID { get; set; }
public int Year …Run Code Online (Sandbox Code Playgroud) 我正在使用WinForms.在我的形式我有一个GroupBox.这是一个自定义组框.我想要一个透明的背景groupbox.我遇到问题创建透明背景groupbox问题这个代码是我在将组框设置backcolor为透明时继续出错.
错误:控件不支持透明背景颜色.
g.Clear(BackColor = Color.Transparent); (这是给我问题的那条线)
private void DrawGroupBox(GroupBox box, Graphics g, Color textColor, Color borderColor)
{
if (box != null)
{
Brush textBrush = new SolidBrush(textColor);
Brush borderBrush = new SolidBrush(borderColor);
Pen borderPen = new Pen(borderBrush);
SizeF strSize = g.MeasureString(box.Text, box.Font);
Rectangle rect = new Rectangle(box.ClientRectangle.X,
box.ClientRectangle.Y + (int)(strSize.Height / 2),
box.ClientRectangle.Width - 1,
box.ClientRectangle.Height - (int)(strSize.Height / 2) - 1);
// Clear text and border
g.Clear(BackColor …Run Code Online (Sandbox Code Playgroud) 我的C#WinForms程序中有一个"AxWindowsMediaPlayer"控件.当我正常更改URLWindowsMediaPlayer1 的属性时,它工作正常并自动播放新的mp3文件.
当歌曲结束时,WindowsMediaPlayer1状态变为Stopped,我想下一步URL自动开始播放.
我使用了PlayStatChange事件,所以当玩家状态是Stopped,URL将改变,但不自动播放!
播放器进入Ready状态直到我按下WindowsMediaPlayer1上的播放按钮.
这是代码:
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "6.mp3"; //Works fine
}
private void button1_Click(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "4.mp3"; //Works fine. It changes the music.
}
private void WindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) //1 is for "Stopped" State
WindowsMediaPlayer1.URL = "5.mp3";
// Here is the problem.
// URL Will change but player …Run Code Online (Sandbox Code Playgroud) 我在按钮动作中有一个循环用于删除我的空项目ListView,但问题是,当我按下按钮时,它只删除了单个项目.我的意思是:当一个接一个的时候,它不会删除项目:
例:
a1 = ""
a2 = "qwe"
a3 = ""
a4 = ""
a5 = "qwe"
Run Code Online (Sandbox Code Playgroud)
所以,点击按钮后,结果将是:
a2 = "qwe"
a3(or a4 idk) = ""
a5 = "qwe"
Run Code Online (Sandbox Code Playgroud)
我认为这很容易出现逻辑问题,但我无法弄明白.
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].SubItems[2].Text == "")
{
listView1.Items[i].Remove();
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是循环在找到空值后跳过一次检查.我如何解决它?
我有一个 DataAdapter,它在一个数据集中填充 5 个数据表。
SqlDataAdapter da = new SqlDataAdapter("Select * from testTable",con);
da.Fill(ds, 0, numberOfRowsToPutInEachDataTable, "DT1");
da.Fill(ds, numberOfRowsToPutInEachDataTable , numberOfRowsToPutInEachDataTable , "DT2");
da.Fill(ds, numberOfRowsToPutInEachDataTable* 2, numberOfRowsToPutInEachDataTable, "DT3");
da.Fill(ds, numberOfRowsToPutInEachDataTable * 3, numberOfRowsToPutInEachDataTable, "DT4");
da.Fill(ds, numberOfRowsToPutInEachDataTable * 4, numberOfRowsToPutInEachDataTable, "DT5");
Run Code Online (Sandbox Code Playgroud)
我的目标是让每个
da.Fill...
Run Code Online (Sandbox Code Playgroud)
同时异步运行。
我没有异步运行事物的经验,并且很难通过研究找到解决方案。谁能告诉我如何让这些 DataAdapter.Fill() 异步运行?
我正在尝试使用 c# 在 Windows 窗体中创建一个类似 swype 的键盘
我有两个问题:我无法重现手指滑动动作。b. 我无法识别不同按键下的字母。
对于第一个问题:
我使用了 Graphics 类和 Pen 类,并像这样使用它:
bool draw;
Graphics g;
Pen p;
int prevX;
int prevY;
private void Form1_Load(object sender, EventArgs e)
{
draw = false;
g = CreateGraphics();
p = new Pen(Color.Black, 2);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
draw = true;
prevX = e.X;
prevY = e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if …Run Code Online (Sandbox Code Playgroud) 我有一个总是在顶部的应用程序(基本上是一个状态显示),我想跟随另一个程序,并始终坐在最小化按钮的左侧.
我可以Rect使用以下代码来表示"目标"过程,然后我可以将其与偏移量配对以生成叠加层的初始位置.
获取HWnd IntPtr:
private IntPtr HWnd = Process.GetProcessesByName("targetapplication")[0].MainWindowHandle;
Run Code Online (Sandbox Code Playgroud)
从user32.dll以下函数声明函数:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
Run Code Online (Sandbox Code Playgroud)
适当的struct:
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
Run Code Online (Sandbox Code Playgroud)
然后按需调用它.
但是,我想避免不断轮询这个值,所以我想挂钩到目标应用程序并在目标窗口移动时做出响应.
浏览user32.dll文档,我能看到的唯一方法是使用SetWindowsHookEx().但是,我并不完全确定如何从这里拦截一个事件.
我相信目标应用程序是基于WinForms但我无法确定.因此,让我响应目标Move事件或直接响应某些Windows消息的解决方案都很有用.
关于如何进行的任何想法?
我已经阅读了许多有关 C# 中 Windows 窗体的不透明度/透明度的主题,但这不是我想要获得的效果。我希望Form是100%透明,但是透明度是Panel可调的,并且透明效果转移到了表单后面的元素(Windows桌面、网络浏览器等)。所附照片显示了我想要获得的效果(我在图形程序中制作了它们)。我将感谢您的帮助。
c# ×10
winforms ×9
.net ×6
asynchronous ×2
transparency ×2
ado.net ×1
asp.net-mvc ×1
async-await ×1
bindinglist ×1
datagridview ×1
groupbox ×1
listview ×1
opacity ×1
panel ×1
pinvoke ×1
razor ×1
user32 ×1
winapi ×1