小编B. *_*non的帖子

为什么我的表单控件都没有被识别为ComboBoxes?

我用这种方式"只读"组合框:

private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
     // for this to work, set the comboboxes' Tag to its SelectedIndex after setting that
    ComboBox cb = sender as ComboBox;
     int validSelection = Convert.ToInt32(cb.Tag);
    if (cb.SelectedIndex != validSelection )
    {
        cb.SelectedIndex = validSelection;
    } 
}
Run Code Online (Sandbox Code Playgroud)

...然后尝试将表单上的所有组合框设置为该处理程序,如下所示:

foreach (Control c in this.Controls)
{
    if (c is ComboBox)
    {
        (c as ComboBox).SelectedValueChanged += comboBox1_SelectedValueChanged;
    }
}
Run Code Online (Sandbox Code Playgroud)

......但if条件永远不等于真; 表格上有几个ComboBox ... ???

c# combobox dynamic event-handling winforms

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

为什么我的绑定失败了?

我正在使用此代码获取" 无法绑定到数据源上的属性或列 "(在附加了" < - here! "注释的行上):

List<QHQuad> listQH = PlatypusData.GetQHForPlatypusAndDay(platypusId, dow);
foreach (var quad in listQH)
{
    int QHCell = quad.QH;
    if ((QHCell >= 1) || (QHCell <= QUARTER_HOUR_COUNT))
    {
        string PH1CellToPopulate = string.Format("textBoxA_{0}", QHCell);
        string PH2CellToPopulate = string.Format("textBoxB_{0}", QHCell);
        string PH3CellToPopulate = string.Format("textBoxC_{0}", QHCell);
        var tb = (TextBox)this.Controls.Find(PH1CellToPopulate, true).First();
        tb.DataBindings.Add(new Binding("Text", quad, "Ph1")); // <-- here!
        tb = (TextBox)this.Controls.Find(PH2CellToPopulate, true).First();
        tb.DataBindings.Add(new Binding("Text", quad, "Ph2"));
        tb = (TextBox)this.Controls.Find(PH3CellToPopulate, true).First();
        tb.DataBindings.Add(new Binding("Text", quad, "Ph3")); 
    }
}
Run Code Online (Sandbox Code Playgroud)

在失败点,quad包含四个值:QHCell,即1; Ph1,这是一个空白字符串; Ph2,这是一个空白字符串; 和Ph3,这是"1" …

c# data-binding dynamic winforms

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

为什么Resharper告诉我我的字符串[]从未被分配过?

在此声明:

string[] TardyEvenEmorys;
Run Code Online (Sandbox Code Playgroud)

...... Resharper告诉我," Field'TardyEvenEmorys'从未被分配过. "

稍后在代码中,对字符串[]进行赋值:

TardyEvenEmorys[1] = string.Empty;
TardyEvenEmorys[2] = string.Empty;
TardyEvenEmorys[3] = string.Empty;
TardyEvenEmorys[4] = string.Empty;
Run Code Online (Sandbox Code Playgroud)

...然后有条件地添加实际值:

foreach (KeyValuePair<int, string> entry in itemNumberTardyPairs)
{
    TardyEvenEmorys[entry.Key] = entry.Value;
Run Code Online (Sandbox Code Playgroud)

...

...最后,这些值以这种方式使用:

string url = GetTardyFilename(TardyEvenEmorys[Number]);
Run Code Online (Sandbox Code Playgroud)

那么Resharper告诉我什么?我应该在声明中实例化字符串[],或者...... ???

c# resharper refactoring automated-refactoring

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

如何在有机会抛出异常之前将null值转换为string.empty?

我有以下代码将以前的值输入DataGridView单元格.如果在第0行和第2列或更大,则向左的val,否则直接在上面的值:

private void dataGridViewPlatypi_CellEnter(object sender, DataGridViewCellEventArgs args)
{
    // TODO: Fails if it sees nothing in the previous cell
    string prevVal = string.Empty;
    if (args.RowIndex > 0)
    {
        prevVal = dataGridViewPlatypi.Rows[args.RowIndex - 1].Cells[args.ColumnIndex].Value.ToString();
    } else if (args.ColumnIndex > 1)
    {
        prevVal = dataGridViewPlatypi.Rows[args.RowIndex].Cells[args.ColumnIndex-1].Value.ToString();
    }
    dataGridViewPlatypi.Rows[args.RowIndex].Cells[args.ColumnIndex].Value = prevVal;
}
Run Code Online (Sandbox Code Playgroud)

只要有值可以查看和复制,这就很有用.如果单元格是空白的,我得到:

用户代码未处理System.NullReferenceException
消息=未将对象引用设置为对象的实例.

我猜这是一个使用null coalesce运算符的机会,但(假设我的猜测很好),我该如何实现呢?

c# datagridview nullreferenceexception winforms datagridviewtextboxcell

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

进度条永远不会显示

我只是想要一个不确定的进度条变得可见,并显示一些"活动"来指示查询正在运行,然后,当查询通过时,返回到不可见.看似简单; 但是这个代码:

try
{
    Cursor.Current = Cursors.WaitCursor;
    progressBarChanges.Value = 50;
    progressBarChanges.Step = 20;
    progressBarChanges.Visible = true;
    ... // the meat of the code, where the query is being done, elided
} finally
{
    progressBarChanges.Visible = false;
    Cursor.Current = Cursors.Default;
}
Run Code Online (Sandbox Code Playgroud)

...进度条永远不会显示,即使查询需要一段时间才能运行.进度条位于DGV上.我意识到我的进度代码有点蹩脚,但首先要做的事情 - 我只是希望这件事能为初学者展示一些东西.

UPDATE

在我调用运行查询的BackgroundWorker proc之前,我将progressBar设置为可见方式:

progressBarChanges.Value = 50;
progressBarChanges.Step = 20;
progressBarChanges.Visible = true;
. . .    
if (args.KeyCode == Keys.Enter)
{
    if (ValidEntryForCRID(textBoxID.Text))
    {
        RetrieveAndBindPlatypusData();
        var tb = (TextBox)Controls.Find("textBoxDuckbill", true).First();
        if (tb != null) …
Run Code Online (Sandbox Code Playgroud)

c# winforms progress-bar

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

如何防止JSON Web服务响应下载到文件?

我有一些几乎相同的WCF服务代码,除了一个方法应该返回一个xml结果,另一个方法是一个json结果:

[ServiceContract]
public interface IRestServiceImpl
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "xml/{id}")]
    string XMLData(string id);

    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/{id}")]
    string JSONData(string id);
}
Run Code Online (Sandbox Code Playgroud)

xml工作正常(例如,当我在浏览器中输入" http:// localhost:4841/RestServiceImpl.svc/xml/2468 "时).

但是,当我输入" http:// localhost:4841/RestServiceImpl.svc/json/2468 "时,我得到一个" 文件下载 - 安全警告 "对话框,它允许我保存文件(在这种情况下名为"2468") ,在记事本中打开时包含以下内容:

{"JSONDataResult":"您请求了产品2468"}

这是"按设计"(将json结果保存到文件中),还是为什么它的行为与xml-o-rama不同?

xml rest wcf json web-services

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

如何将新版本的SQLite与Visual Studio同步?

当我今天启动Visual Studio时,我收到一条消息,指出有更新.原来是SQLite,我更新了它.

但是,现在,我无法编译我的应用程序,因为我收到了msg," 找不到SDK"SQLite.WinRT,Version = 3.7.14.1". "

- 和警告:

找不到引用的组件"SQLite for Windows Runtime".

SQLite for Windows运行时参考现在"装饰",带有黄色的符号(图标/字形).那么我需要做什么 - 声称是版本3.7.14.1,所以我不知道是谁或者在寻找不同版本的SQLite ......?

sqlite visual-studio windows-8 visual-studio-2012 windows-store-apps

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

为什么忽略添加BorderBrush?

我改变了我的超链接:

   <HyperlinkButton x:Name="hyperlinkButtonManageInvitations" Margin="24"
                    Grid.Row="1" Tap="HyperlinkButtonManageInvitations_OnTap">
       Manage Invitations
   </HyperlinkButton>
Run Code Online (Sandbox Code Playgroud)

......对此:

    <HyperlinkButton x:Name="hyperlinkButtonManageInvitations"
                     BorderBrush="DarkOrange" Margin="24" Grid.Row="1"
                     Tap="HyperlinkButtonManageInvitations_OnTap">
        Manage Invitations
    </HyperlinkButton>
Run Code Online (Sandbox Code Playgroud)

......没有视觉变化 - 没有添加边框.所以我尝试调整一些更多的属性,如下所示:

    <HyperlinkButton x:Name="hyperlinkButtonManageInvitations"
                     BorderBrush="DarkOrange" Margin="24" Grid.Row="1"
                     Tap="HyperlinkButtonManageInvitations_OnTap"
                     BorderThickness="4" OpacityMask="Black">
        Manage Invitations
    </HyperlinkButton>
Run Code Online (Sandbox Code Playgroud)

......但仍然没有去.添加边框的哪一部分我不喜欢?

UPDATE

我的超链接按钮现在是:

    <HyperlinkButton x:Name="hyperlinkButtonManageInvitations" Style="{StaticResource BorderedHyperlinkButtonStyle}" BorderThickness="5" BorderBrush="Red" Foreground="DarkOrange" Margin="24" Grid.Row="1" Tap="HyperlinkButtonManageInvitations_OnTap" Content="Manage Invitations">
    </HyperlinkButton>
Run Code Online (Sandbox Code Playgroud)

...我将下面提供的样式添加到App.xaml的部分,但我现在仍然看到有问题的超链接按钮.

更新2

它现在在那里 - 它"突然"出现了; 我想xaml渲染器可能会慢慢吸收.或者在使用新添加的样式时,需要首先重新加载项目,或者...... ???

xaml windows-phone-8

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

为什么基本页面不提供OnNavigatedTo()事件?

试图确定为什么我的应用程序(基于"空白应用程序"模板进入这个勇敢的新世界)不能像我期望的那样工作(http://stackoverflow.com/questions/14467756/why-would-my -event-handler-not-get-called),我启动了一个新的Blank项目,然后删除了MainPage并添加了一个新的Basic(非空白)页面,我将其命名为MainPage以纪念离开的页面(并且作为对传统和懒惰 - 所以我不必更改导航到该页面的app.xaml.cs中的代码.

Blank应用程序创建了这样的原始MainPage.xaml.cs(自动生成的注释省略):

namespace AsYouWish
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

...我用BasicPage(而不是BlankPage)替换它,它生成了这个:

namespace AsYouWish
{
    public sealed partial class MainPage : AsYouWish.Common.LayoutAwarePage
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }

        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }
    }
Run Code Online (Sandbox Code Playgroud)

因此,基本页面获取LoadState()和SaveState(),而空白页面的MainPage具有OnNavigatedTo().为什么基本页面也没有OnNavigatedTo()事件?似乎每个页面都有可能被导航到(并且从那个事件我可以看到更可能是可选的/不必要的).

c# visual-studio-2012 windows-store-apps

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

如何使用枚举作为数据源?

我想使用枚举中的值作为某些组合框的源。这可以编译,但不会填充组合框:

private enum ValueType { Text, Barcode }

private ValueType vt;

private void FormCPCLCodeGenUtil_Load(object sender, EventArgs e)
{
    comboBoxType1.DataSource = vt;
}
Run Code Online (Sandbox Code Playgroud)

...这不应该让我感到惊讶,因为 vt 尚未被分配值;我不希望组合框只有一个值。那么我该如何做到这一点(或者有没有比使用枚举作为数据源更好的方法)?

更新

附带问题:当多个组合框使用相同的数据源时,哪一个更好:

    comboBoxType1.DataSource = Enum.GetNames(typeof(ValueType)); 
    comboBoxFontSize1.DataSource = Enum.GetNames(typeof (FontSizeType));
    comboBoxAlign1.DataSource = Enum.GetNames(typeof(AlignOptions));

    //comboBoxType2.DataSource = comboBoxType1.DataSource; 
    comboBoxType2.DataSource = Enum.GetNames(typeof(ValueType));
Run Code Online (Sandbox Code Playgroud)

(使用之前分配的组合框数据源作为自己的数据源,或者以与前一个相同的方式连接?)

c# enums combobox datasource

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