小编Gra*_*ICA的帖子

Decorator和Adapter模式之间有什么区别?

在什么条件下使用Adapter或Decorator模式会更好?

欢迎来自真实节目的例子.

c# design-patterns

3
推荐指数
2
解决办法
9017
查看次数

根据其内容的宽度调整标签宽度

我正在使用代码编辑器,并希望随着数字的增加自动调整标签的宽度。例如,对于 1-9(1 位数字),有一个特定的宽度。然后当它达到 10-99(2 位数)时,标签的宽度增加。然后又是 100-999(3 位数)等。

结果应该是这样的:

在此处输入图片说明

这是我的代码:

private void timer_countline_Tick(object sender, EventArgs e)
{
    updateNumberLabel();
}

private void updateNumberLabel()
{
    // we get index of first visible char and number of first visible line
    Point pos = new Point(0, 0);
    int firstIndex = rtb.GetCharIndexFromPosition(pos);
    int firstLine = rtb.GetLineFromCharIndex(firstIndex);

    // now we get index of last visible char and number of last visible line
    pos.X = ClientRectangle.Width;
    pos.Y = ClientRectangle.Height;
    int lastIndex = rtb.GetCharIndexFromPosition(pos);
    int lastLine = rtb.GetLineFromCharIndex(lastIndex);

    // …
Run Code Online (Sandbox Code Playgroud)

c# label width winforms

3
推荐指数
1
解决办法
8449
查看次数

3
推荐指数
1
解决办法
1082
查看次数

LINQ to Entities 中使用的 AddDays 在除一台服务器之外的所有服务器中抛出错误

在开发环境中,以下 SQL 语句有效,但不适用于任何其他环境:

System.NotSupportedException:LINQ to Entities 无法识别“System.DateTime AddDays(Double)”方法,并且此方法无法转换为存储表达式。

相关代码:

bool hasSomethinglast14days = (from a in db.someTable
                               where a.SomeIntColumn == someIntVariable
                                  && a.CreateDate >= DateTime.UtcNow.AddDays(-14)
                               select a.someColumn).Any();
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-entities

3
推荐指数
1
解决办法
3035
查看次数

在String.Join中添加新行?

所以我一直在疯狂地为我的String.Join打印输出添加新行,但似乎不可能或我做错了什么?我已经尝试过使用"/n/r",Enivrorment.NewLine以及创建一个类也创建一个新的行

列表框我也试图打印出来

ListBox1.Items.Add("Calories to lose 0.5kg per week: " +
    string.Join(Environment.NewLine + "Calories to lose 1kg per week:",
        bc.LoseOrGainWeightCalories(bc.MaintainWightCalories(), true)));
Run Code Online (Sandbox Code Playgroud)

拜访这堂课:

public string[] LoseOrGainWeightCalories(double weight, bool lose) {
    string[] array = new string[2];
    double LoseGainWeight = this.weight;
    if(lose==true) {
        array[0] = Convert.ToString(LoseGainWeight - 500);
        array[1] = Convert.ToString(LoseGainWeight - 1000);
    } else {
        array[0] = Convert.ToString(LoseGainWeight + 500);
        array[1] = Convert.ToString(LoseGainWeight + 1000);
    }
    return array;
}
Run Code Online (Sandbox Code Playgroud)

电流输出图片:

c# string

3
推荐指数
1
解决办法
1万
查看次数

选择随机颜色,但某些颜色除外

我目前正在使用此代码:

Random randomColor = new Random();
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
KnownColor randomColorName = names[randomColor.Next(names.Length)];
Color RandomColor = Color.FromKnownColor(randomColorName);
this.BackColor = RandomColor;
Run Code Online (Sandbox Code Playgroud)

我正在使用它来生成随机颜色并将其设置为我的背景,但我不希望它变黑.

有没有办法从可能的随机颜色中拿走黑色?

c# arrays

3
推荐指数
1
解决办法
859
查看次数

我如何模拟作为接口的接口属性

public interface IFoo
{
    IFoo2 Foo2 { get; }
}

public interface IFoo2
{
    string FooMethod();
}
Run Code Online (Sandbox Code Playgroud)

假设我有上面的 2 个接口。我正在尝试模拟在另一个界面上设置为属性的界面。这可能吗?

Mock<IFoo> mockFoo = new Mock<IFoo>();
Mock<IFoo2> mockFoo2 = new Mock<IFoo2>();

mockfoo.SetupGet<IFoo2>(x => x.Foo2).Returns(mockFoo2);
Run Code Online (Sandbox Code Playgroud)

抱怨它无法将 a 转换IFoo2Moq.Mock<IFoo2>

错误 CS1503 参数 1:无法从“Moq.Mock<IFoo2>”转换为“IFoo2”

c# interface moq mocking

3
推荐指数
1
解决办法
3192
查看次数

如何写入文件更多次?

我正在使用C#,WPF并且有一个关于游戏多项选择的作业,它有一个应用程序,为用户提供列表问题和答案.我想在file.text中写一次不仅一次.

我用过这个事件:

这段代码:

private void Button_Click(object sender, RoutedEventArgs e)
{
    using( StreamWriter File = new StreamWriter(@"C:\Users\Admin\Documents\Visual Studio 2013\Projects\?? ÁN 2\?? ÁN 2\bin\Debug\Bo1.txt"))
    {
        File.WriteLine("dsCauHoi*Câu h?i thêm*"+ txbcauhoi.Text.ToString()+ "*"+txbA.Text.ToString()+ "*"+ txbB.Text.ToString()+ "*" +txbC.Text.ToString() +"*"+ txbD.Text+ "*"+ txbAD.Text.ToString()+"/n");
    }
    MessageBox.Show("C?m ?n b?n ?ã ?óng góp ý ki?n");
    this.Close();
}
Run Code Online (Sandbox Code Playgroud)

但它允许写一次.

c# wpf

3
推荐指数
1
解决办法
73
查看次数

在WPF媒体元素中连续重播视频

我有一个媒体元素播放的视频文件.我需要继续玩,所以我试过:

me.play();
me.MediaEnded += new RoutedEventHandler(me_MediaEnded);
Run Code Online (Sandbox Code Playgroud)

使用此事件方法:

//loop to keep video playing continuously
void me_MediaEnded(object sender, EventArgs e)
{
    //play video again
    me.Play();
}
Run Code Online (Sandbox Code Playgroud)

但是,上面没有重播视频文件.为什么?我做错了什么?

c# wpf mediaelement event-handling replay

2
推荐指数
1
解决办法
9867
查看次数

在Winform中打开Crystal报表

我创建了一个水晶报表,然后在创建它之后,创建了一个winform,在其中导入了水晶报表库(以代码显示),并使用报表查看器来查看该报表,但是我无法查看该报表,代码,我是Crytal Reports的新手,我做的代码是:

码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace InventorySoftware
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.reportViewer1.RefreshReport();



        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string ReportSources = "";
            ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load("C:\\Users\\Ahsan\\Desktop\\PROJECT INVENTORY SOFTWARE\\InventorySoftware\\InventorySoftware\\CrystalReport1.rpt");
            reportViewer1.ReportSource = cryRpt;
            reportViewer1.Refresh();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它给出了错误, reportViewer1.ReportSource = cryRpt; 错误是

Error   1   'Microsoft.Reporting.WinForms.ReportViewer' does …
Run Code Online (Sandbox Code Playgroud)

c# crystal-reports visual-studio-2008 winforms

2
推荐指数
1
解决办法
8301
查看次数